标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
#include<iostream> #include<cstdio> #include<cmath> #include<string> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<vector> #include<list> #include<set> #include<map> using namespace std; #define ll long long #define mod 1000000007 #define inf 999999999 //#pragma comment(linker, "/STACK:102400000,102400000") int scan() { int res = 0 , ch ; while( !( ( ch = getchar() ) >= ‘0‘ && ch <= ‘9‘ ) ) { if( ch == EOF ) return 1 << 30 ; } res = ch - ‘0‘ ; while( ( ch = getchar() ) >= ‘0‘ && ch <= ‘9‘ ) res = res * 10 + ( ch - ‘0‘ ) ; return res ; } ll a[100010]; ll b[100010]; ll gcd(ll x,ll y) { if(x%y==0) return y; else return gcd(y,x%y); } void exgcd(ll a, ll b, ll &x, ll &y) { if(b == 0) { x = 1; y = 0; return; } exgcd(b, a % b, x, y); ll tmp = x; x = y; y = tmp - (a / b) * y; } int main() { ll x,y,z,i,t; ll flag=1; scanf("%lld",&x); while(x--) { scanf("%lld",&z); for(i=0;i<z;i++) scanf("%lld",&b[i]); for(i=0;i<z;i++) scanf("%lld",&a[i]); ll a1=a[0],b1=b[0]; ll jie=1; for(i=1;i<z;i++) { ll a2=a[i],b2=b[i]; ll xx,yy; ll gys=gcd(b1,b2); if((a2-a1)%gys) { jie=0; break; } exgcd(b1,b2,xx,yy); xx=(xx*(a2-a1))/gys; ll gbs=b1*b2/gys; a1=(((xx*b1+a1)%gbs)+gbs)%gbs; b1=gbs; } printf("Case %lld: ",flag++); if(!jie) printf("-1\n"); else if(a1!=0) printf("%lld\n",a1); else printf("%lld\n",b1); } return 0; }
hdu 3579 Hello Kiki 不互质的中国剩余定理
标签:
原文地址:http://www.cnblogs.com/jhz033/p/5417816.html