X问题
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6716 Accepted Submission(s): 2340
Problem Description
求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], X mod a[1] = b[1], X mod a[2] = b[2], …, X mod a[i] = b[i], … (0 < a[i] <= 10)。
Input
输入数据的第一行为一个正整数T,表示有T组测试数据。每组测试数据的第一行为两个正整数N,M (0 < N <= 1000,000,000 , 0 < M <= 10),表示X小于等于N,数组a和b中各有M个元素。接下来两行,每行各有M个正整数,分别为a和b中的元素。
Output
对应每一组输入,在独立一行中输出一个正整数,表示满足条件的X的个数。
Sample Input
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
Sample Output
1 0 3
Author
lwg
Source
Recommend
linle | We have carefully selected several similar problems for you:
大水题哦,不过他没说互不互质,再用互质的做法来做的话就会连样例都弄不出来!!!!!
代码:
#include#include #include #include #include #define N 20#define ll long longusing 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;}