标签:输出 tle ++ getch ted [1] name algorithm cep
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5893 Accepted Submission(s): 2018
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; typedef long long ll; const int N=15; inline ll read(){ char c=getchar();ll x=0,f=1; while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();} while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();} return x*f; } ll r,n,a[N],m[N]; void exgcd(ll a,ll b,ll &d,ll &x,ll &y){ if(b==0) d=a,x=1,y=0; else exgcd(b,a%b,d,y,x),y-=(a/b)*x; } void exCRT(){ ll m1,a1,m2,a2,flag=0; m1=m[1];a1=a[1]; for(int i=2;i<=n;i++){ m2=m[i];a2=a[i]; ll d,t1,t2; exgcd(m1,m2,d,t1,t2); if((a2-a1)%d) {flag=1;break;} t1*=(a2-a1)/d; m2/=d; t1=(t1%m2+m2)%m2; a1=t1*m1+a1; m1*=m2; } if(flag||a1>r) puts("0"); else printf("%lld\n",(r-a1)/m1+1+(a1==0?-1:0)); } int main(){ freopen("in","r",stdin); int T=read(); while(T--){ r=read();n=read(); for(int i=1;i<=n;i++) m[i]=read(); for(int i=1;i<=n;i++) a[i]=read(); exCRT(); } }
标签:输出 tle ++ getch ted [1] name algorithm cep
原文地址:http://www.cnblogs.com/candy99/p/6399043.html