标签:
http://acm.hdu.edu.cn/showproblem.php?pid=1573
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4439 Accepted Submission(s): 1435
#include<stdio.h> #include<algorithm> #include<string.h> #include<stdlib.h> using namespace std; const int M = 20; typedef long long ll; int r; ll n[M], b[M], N; void gcd(ll a, ll b, ll &x, ll &y) { if(b == 0) { x = 1; y = 0; r = a; return ; } gcd(b, a % b, x, y); ll t = x; x = y; y = t - a / b * y; } ll CRT2(ll b[], ll n[], int m) { int f = 0; ll n1 = n[0], n2, b1 = b[0], b2, c, t, k, x, y; for(int i = 1 ; i < m ; i++) { n2 = n[i]; b2 = b[i]; c = b2 - b1; gcd(n1, n2, x, y); if(c % r != 0) { f = 1; break; } k = c / r * x; t = n2 / r; k = (k % t + t) % t; b1 = b1 + n1 * k; n1 = n1 * t; } if(f)//无解 return 0; if(b1 == 0) b1 = n1; if(b1 > N) return 0; return (N - b1) / n1 + 1; } int main() { int t, m; scanf("%d", &t); while(t--) { scanf("%lld%d", &N, &m); for(int i = 0 ; i < m ; i++) scanf("%lld", &n[i]); for(int i = 0 ; i < m ; i++) scanf("%lld", &b[i]); printf("%lld\n", CRT2(b, n, m)); } return 0; }
标签:
原文地址:http://www.cnblogs.com/qq2424260747/p/4962598.html