Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2724 Accepted Submission(s): 1008
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 22 LL ex_gcd(LL a,LL b,LL &x,LL &y) 23 { 24 if(b == 0){ 25 x = 1; 26 y = 0; 27 return a; 28 } 29 LL r = ex_gcd(b,a%b,x,y); 30 LL t = x; 31 x = y; 32 y = t - a/b*y; 33 return r; 34 } 35 LL aa[20],r[20]; 36 37 int main() 38 { 39 LL i,ans,a,b,c,d,k,x,y,n,m; 40 LL T; 41 cin>>T; 42 int cas = 0; 43 while(T--){ 44 cin>>m; 45 cas++; 46 bool flag = 1; 47 LL lcm = 1; 48 for(i=1;i<=m;i++){ 49 cin>>aa[i]; 50 lcm = lcm/gcd(lcm,aa[i])*aa[i]; 51 } 52 for( i=1;i<=m;i++){ 53 cin>>r[i]; 54 } 55 printf("Case %d: ",cas); 56 for( i=2;i<=m;i++){ 57 a = aa[1]; 58 b = aa[i]; 59 c = r[i] - r[1]; 60 d = ex_gcd(a,b,x,y); 61 if(c%d!=0){ 62 flag = 0; 63 break; 64 } 65 LL t = b/d; 66 x = (x*(c/d)%t+t)%t; 67 r[1] = aa[1]*x + r[1]; 68 aa[1] = aa[1]*(aa[i]/d); 69 } 70 if(!flag){ 71 puts("-1"); 72 continue; 73 } 74 75 if(r[1]){ 76 printf("%lld\n",r[1]); 77 continue; 78 } 79 80 printf("%lld\n",lcm); 81 } 82 83 return 0; 84 }
这道题跟hdu1573差不多,代码改改就能过
原文:http://www.cnblogs.com/lmlyzxiao/p/4934245.html