Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7081 Accepted Submission(s): 2493
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; typedef long long ll; ll a[1010], b[1010], n, T, m, lcm, ans; ll exgcd(ll a, ll b, ll &x, ll &y) { if (!b) { x = 1; y = 0; return a; } ll temp = exgcd(b, a%b, x, y), t = x; x = y; y = t - (a / b) * y; return temp; } ll niyuan(ll x, ll mod) { ll px, py, t; t = exgcd(x, mod, px, py); if (t != 1) return -1; return (px % mod + mod) % mod; } ll gcd(ll a, ll b) { if (!b) return a; return gcd(b, a%b); } bool hebing(ll a1, ll b1, ll a2, ll b2, ll &a3, ll &b3) { ll d = gcd(b1, b2), c = a2 - a1; if (c % d != 0) return false; c = (c % b2 + b2) % b2; b1 /= d; b2 /= d; c /= d; c *= niyuan(b1, b2); c %= b2; c *= b1 * d; c += a1; b3 = b1 * b2 * d; a3 = (c % b3 + b3) % b3; return true; } ll China() { ll a1 = a[1], a2, a3, b1 = b[1], b2, b3; for (ll i = 2; i <= m; i++) { a2 = a[i], b2 = b[i]; if (!hebing(a1, b1, a2, b2, a3, b3)) return -1; a1 = a3; b1 = b3; } return (a1 % b1 + b1) % b1; } int main() { scanf("%lld", &T); while (T--) { lcm = 0; cin >> n >> m; for (ll i = 1; i <= m; i++) { cin >> b[i]; if (i == 1) lcm = b[i]; else lcm = lcm / gcd(lcm, b[i]) * b[i]; } for (ll i = 1; i <= m; i++) cin >> a[i]; ll temp = China(); if (temp != -1) { while (temp <= 0) temp += lcm; } if (temp == -1 || temp > n) cout << 0 << endl; else cout << (n - temp) / lcm + 1 << endl; } return 0; }