题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1788
其实有两种解法一种是转化为求同余方程组然后再解,第二种方法是根据同余定理推出 N+a≡0(mod Mi)有了这个式子,就知道这个题目意思就是求N个Mi的最小公倍数了
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <stack> 5 #include <queue> 6 #include <map> 7 #include <algorithm> 8 #include <vector> 9 10 using namespace std; 11 12 const int maxn = 1000005; 13 14 typedef long long LL; 15 16 LL ex_gcd(LL a,LL b,LL &x,LL &y) 17 { 18 if(b == 0){ 19 x = 1; 20 y = 0; 21 return a; 22 } 23 LL r = ex_gcd(b,a%b,x,y); 24 LL t = x; 25 x = y; 26 y = t - a/b*y; 27 return r; 28 } 29 int main() 30 { 31 LL i,n,a1,aa,r1,a2,r2,ans,a,b,c,d,x0,y0; 32 while(scanf("%lld%lld",&n,&aa)!=EOF){ 33 bool flag = 1; 34 if(n == 0 && aa==0) break; 35 scanf("%lld",&a1); 36 r1 = a1-aa; 37 for( i=1;i<n;i++){ 38 scanf("%lld",&a2); 39 r2 = a2 - aa; 40 a = a1; 41 b = a2; 42 c = r2-r1; 43 LL d = ex_gcd(a,b,x0,y0); 44 45 int t = b/d; 46 x0 = (x0*(c/d)%t+t)%t; 47 r1 = a1*x0 + r1; 48 a1 = a1*(a2/d); 49 50 } 51 52 printf("%lld ",r1); 53 } 54 return 0; 55 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <stack> 5 #include <queue> 6 #include <map> 7 #include <algorithm> 8 #include <vector> 9 10 using namespace std; 11 12 const int maxn = 1000005; 13 14 typedef long long LL; 15 16 LL gcd( LL a,LL b) 17 { 18 if(b == 0) return a; 19 else return gcd(b,a%b); 20 } 21 int main() 22 { 23 LL i,n,a1,aa,r1,a2,r2,ans,a,b,c,d,x0,y0,lcm; 24 while(scanf("%lld%lld",&n,&aa)!=EOF){ 25 bool flag = 1; 26 if(n == 0 && aa==0) break; 27 lcm = 1; 28 for(int i=0;i<n;i++){ 29 cin>>a; 30 lcm = (lcm*a)/gcd(lcm,a); 31 } 32 printf("%lld ",lcm-aa); 33 } 34 return 0; 35 }