标签:
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
#include<iostream> #include<cstdio> #include<cmath> #include<string> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<vector> #include<list> #include<set> #include<map> using namespace std; #define ll long long #define mod 1000000007 #define inf 999999999 //#pragma comment(linker, "/STACK:102400000,102400000") int scan() { int res = 0 , ch ; while( !( ( ch = getchar() ) >= ‘0‘ && ch <= ‘9‘ ) ) { if( ch == EOF ) return 1 << 30 ; } res = ch - ‘0‘ ; while( ( ch = getchar() ) >= ‘0‘ && ch <= ‘9‘ ) res = res * 10 + ( ch - ‘0‘ ) ; return res ; } int a[100010]; int b[100010]; int gcd(int x,int y) { if(x%y==0) return y; else return gcd(y,x%y); } void exgcd(int a, int b, int &x, int &y) { if(b == 0) { x = 1; y = 0; return; } exgcd(b, a % b, x, y); int tmp = x; x = y; y = tmp - (a / b) * y; } int main() { int x,y,z,i,t; scanf("%d",&x); while(x--) { scanf("%d%d",&y,&z); for(i=0;i<z;i++) scanf("%d",&b[i]); for(i=0;i<z;i++) scanf("%d",&a[i]); int a1=a[0],b1=b[0]; int jie=1; for(i=1;i<z;i++) { int a2=a[i],b2=b[i]; int xx,yy; int gys=gcd(b1,b2); if((a2-a1)%gys) { jie=0; break; } exgcd(b1,b2,xx,yy); xx=(xx*(a2-a1))/gys; int gbs=b1*b2/gys; a1=(((xx*b1+a1)%gbs)+gbs)%gbs; b1=gbs; } if(!jie||y<a1) printf("0\n"); else printf("%d\n",(y-a1)/b1+1-((a1==0)?1:0)); } return 0; }
标签:
原文地址:http://www.cnblogs.com/jhz033/p/5417459.html