- 原题链接:Here!
- 思路:
如转载请标明链接
下面是证明过程
证明过程参考自http://972169909-qq-com.iteye.com/blog/1266328,如果有错误一定请指出,不胜感激
- 代码:
#include<bits/stdc++.h> using namespace std; int N,m; int exgcd(int a,int b,int &x,int &y){ if(b==0){ x = 1; y = 0; return a; } int d = exgcd(b,a%b,x,y); int tmp = x; x = y; y = tmp - a/b*x; return d; } int solve(int A[],int B[],int num){ bool ok=true; int a1,b1,a2,b2,k1,kk,d,c,t,x,y; a1 = A[0] , b1 = B[0]; for(int i=1;i<num;i++){ a2 = A[i] , b2 = B[i]; c = b2 - b1; d = exgcd(a1,a2,x,y); if( c % d ){ ok = false; break; } k1 = x*c/d; t = a2/d; kk = (k1%t+t)%t; b1 = b1 + a1*kk; a1 = a1*a2/d; } if(!ok || b1>N) return 0; int ans = (N-b1)/a1+1; if(b1==0) ans--; return ans; } int main(){ int T; int A[100],B[100]; cin>>T; while(T--){ cin>>N>>m; for(int i=0;i<m;i++) cin>>A[i]; for(int i=0;i<m;i++) cin>>B[i]; int ans = solve(A,B,m); cout<<ans<<endl; } return 0; }