标签:scan stream note VID was run HERE ota can
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5489 Accepted Submission(s):
2164
1 #include <iostream> 2 #include<stdio.h> 3 #include <algorithm> 4 #include<string.h> 5 #include<cstring> 6 #include<math.h> 7 #define inf 0x3f3f3f3f 8 #define ll long long 9 using namespace std; 10 11 int m[15]; 12 int r[15]; 13 int n,x,y; 14 int gcd; 15 16 int exgcd(int a,int b,int &x,int &y) 17 { 18 if(b==0) 19 { 20 x=1; 21 y=0; 22 return a; 23 } 24 int q=exgcd(b,a%b,y,x); 25 y=y-(a/b)*x; 26 return q; 27 } 28 29 int main() 30 { 31 int t; 32 scanf("%d",&t); 33 for(int cnt=1;cnt<=t;cnt++) 34 { 35 bool flag=true; 36 scanf("%d",&n); 37 for(int i=0;i<n;i++) 38 scanf("%d",&m[i]); 39 for(int i=0;i<n;i++) 40 scanf("%d",&r[i]); 41 int a1=m[0]; 42 int r1=r[0]; 43 for(int i=1;i<n;i++) 44 { 45 int b1=m[i]; 46 int r2=r[i]; 47 int d=r2-r1; 48 gcd=exgcd(a1,b1,x,y); 49 if(d%gcd) {flag=false;break;} 50 int multiple=d/gcd; 51 int p=b1/gcd; 52 x=( (x*multiple)%p+p )%p; 53 r1=r1+x*a1; 54 a1=a1*b1/gcd; 55 } 56 if(flag) 57 { 58 if(r1==0) r1=a1+r1;///坑:如果余数是0则加一个最小公倍数 59 printf("Case %d: %d\n",cnt,r1); 60 } 61 else printf("Case %d: -1\n",cnt); 62 } 63 return 0; 64 }
hdu3579-Hello Kiki-(扩展欧几里得定理+中国剩余定理)
标签:scan stream note VID was run HERE ota can
原文地址:https://www.cnblogs.com/shoulinniao/p/10363663.html