HDU 1788 Chinese remainder theorem again

题目链接

题意 : 中文题不详述。

思路 : 由N%Mi=(Mi-a)可得(N+a)%Mi=0;要取最小的N即找Mi的最小公倍数即可。

 1 //1788

 2 #include <cstdio>

 3 #include <cstring>

 4 #include <cmath>

 5 #include <iostream>

 6 #define LL long long

 7 

 8 using namespace std ;

 9 

10 LL gcd(LL x,LL y)

11 {

12     return y == 0 ? x : gcd(y,x%y) ;

13 }

14 int main()

15 {

16     int I,a ;

17     while(~scanf("%d %d",&I,&a))

18     {

19         if(I == 0 && a == 0) break ;

20         int x ;

21         LL ans = 1;

22         while(I--)

23         {

24             scanf("%d",&x) ;

25             ans = (ans * x)/gcd(ans,x) ;

26         }

27         printf("%I64d\n",ans-a) ;

28     }

29     return 0 ;

30 }
View Code

 

你可能感兴趣的:(chinese)