标签:read while several long efi lan include lin std
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6716 Accepted Submission(s): 2340
#include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #define N 20 #define ll long long using namespace std; int t,n,a[N],ans; ll s,q,m[N],tot,w,m1; ll read() { ll x=0,f=1; char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1; ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘; ch=getchar();} return x*f; } ll exgcd(int a,int b,int &x,int &y) { if(b==0) { x=1,y=0; return a; } int r=exgcd(b,a%b,x,y),tmp; tmp=x,x=y,y=tmp-a/b*y; return r; } int crt() { int a1=a[1],a2,m2,c,d;m1=m[1]; for(int i=2;i<=n;++i) { int x=0,y=0; a2=a[i],m2=m[i]; c=a2-a1; d=exgcd(m1,m2,x,y); int mod=m2/d; if(c%d) return -1; x=x*c/d; x=(x%mod+mod)%mod; a1+=m1*x; m1*=mod; } if(a1==0) a1+=m1; return a1; } int main() { t=read(); while(t--) { q=read(),n=read();ans=0; for(int i=1;i<=n;i++) m[i]=read(); for(int i=1;i<=n;i++) a[i]=read(); w=crt();if(w==-1||w>q) printf("0\n"); else { ans=1; while(w+m1<=q) w+=m1,ans++; printf("%d\n",ans); } } return 0; }
标签:read while several long efi lan include lin std
原文地址:http://www.cnblogs.com/z360/p/7351308.html