标签:href hit sub exgcd tin lan i++ return target
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4208 Accepted Submission(s): 1617
1 #include <algorithm> 2 #include <cstdio> 3 4 using namespace std; 5 6 int n,m[23],a[23]; 7 8 int exgcd(int a,int b,int &x,int &y) 9 { 10 if(!b) 11 { 12 x=1; y=0; 13 return a; 14 } 15 int ret=exgcd(b,a%b,x,y),tmp=x; 16 x=y; y=tmp-a/b*y; 17 return ret; 18 } 19 int CRT(int m[],int a[]) 20 { 21 int ret=a[1],mm=m[1]; 22 for(int i=2;i<=n;i++) 23 { 24 int gcd,x,y,b=m[i],tmp=a[i]; 25 int c=tmp-ret; gcd=exgcd(mm,b,x,y); 26 if(c%gcd) return -1; 27 x=x*c/gcd; 28 int mod=b/gcd; 29 x=(x%mod+mod)%mod; 30 ret+=mm*x; mm*=mod; 31 } 32 if(!ret) ret+=mm; 33 return ret; 34 } 35 36 int main() 37 { 38 int t; scanf("%d",&t); 39 for(int k=1;k<=t;k++) 40 { 41 scanf("%d",&n); 42 for(int i=1;i<=n;i++) scanf("%d",m+i); 43 for(int i=1;i<=n;i++) scanf("%d",a+i); 44 printf("Case %d: %d\n",k,CRT(m,a)); 45 } 46 return 0; 47 }
标签:href hit sub exgcd tin lan i++ return target
原文地址:http://www.cnblogs.com/Shy-key/p/7351362.html