标签:
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3921 Accepted Submission(s): 1253
#include<stdio.h> #define LL __int64 LL A[15],B[15]; LL d,x,y,ans; LL dg;//最大公约数 void exgcd(LL a,LL b,LL& d,LL& x,LL& y) { if(!b){d=a;x=1;y=0;} else { exgcd(b,a%b,d,y,x); y-=x*(a/b); } } LL gcd(LL a,LL b) { if(!b) return a; else gcd(b,a%b); } LL China(LL n) { LL dm,a,b,d,x,y; LL c1,c2,c; a=A[0]; c1=B[0]; for(int i=1;i<n;i++) { b=A[i]; c2=B[i]; exgcd(a,b,d,x,y); dm=b/d; c=c2-c1; if(c%d) return -1; x=((x*c/d)%dm+dm)%dm; c1=a*x+c1; a=a*b/d; } dg=a; if(c1==0) { c1=1; for(int i=0;i<n;i++) { c1=c1*A[i]/gcd(c1,A[i]); } dg=c1; } return c1; } int main() { LL T,N,M; scanf("%I64d",&T); while(T--) { scanf("%I64d%I64d",&N,&M); for(LL i=0;i<M;i++) scanf("%I64d",&A[i]); for(LL i=0;i<M;i++) scanf("%I64d",&B[i]); ans=China(M); //printf("ans==%I64d\tdg=%I64d\n",ans,dg); if(ans==-1||ans>N) printf("0\n"); else { printf("%I64d\n",(N-ans)/dg+1); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/yuyixingkong/p/4472107.html