标签:font str tchar main rip csharp 暴力 out input
X problem
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4358 Accepted Submission(s): 1399
3 10 3 1 2 3 0 1 2 100 7 3 4 5 6 7 8 9 1 2 3 4 5 6 7 10000 10 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9
1 0 3
#include<iostream> #include<cstring> #include<cstdio> using namespace std; inline int read(){ int x=0,f=1;char c=getchar(); for(;!isdigit(c);c=getchar()) if(c==‘-‘) f=-1; for(;isdigit(c);c=getchar()) x=x*10+c-‘0‘; return x*f; } int gcd(int a,int b){ if(b==0) return a; return gcd(b,a%b); } int ex_gcd(int a,int b,int &x,int &y){//扩展欧几里得 if(b==0){ x=1,y=0; return a; } int k=ex_gcd(b,a%b,x,y); int tmp=x; x=y; y=tmp-a/b*y; return k; } int T; int N,M; int w[11],r[11]; int China(int N){ int M=w[1],R=r[1]; int x,y; for(int i=2;i<=N;i++){ int d=gcd(M,w[i]); int c=r[i]-R; if(c%d) {return -1;} ex_gcd(M/d,w[i]/d,x,y); x=(c/d*x)%(w[i]/d); R+=x*M; M=M/d*w[i]; R%=M; } if(R<0) return R+M; else return R; } int main(){ T=read(); while(T--){ N=read(),M=read(); for(int i=1;i<=M;i++) w[i]=read(); for(int i=1;i<=M;i++) r[i]=read(); int ret=China(M); if(ret==-1||ret>N) {puts("0");continue;} //特判 int lcm=1,ans=1; for(int i=1;i<=M;i++) lcm=lcm*w[i]/gcd(lcm,w[i]);//求所有数的最小公倍数,有个式子是a*b=gcd(a,b)*lcm(a*b); while(ret+lcm<N){ ans++; ret+=lcm; } cout<<ans<<endl; } }
标签:font str tchar main rip csharp 暴力 out input
原文地址:http://www.cnblogs.com/wxjor/p/6180849.html