1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| #include <bits/stdc++.h> using LL = long long; using I128 = __int128; using DB = double; const int N = 1e5 + 100; int n, m; LL hp[N], p[N], sw[N], atk[N], dw, ans; std::multiset<LL> s; LL exgcd(LL a, LL b, LL &x, LL&y) { if (!b){ return x = 1, y = 0, a; } LL d = exgcd(b, a % b, y, x); y -= (a / b) * x; return d; } LL excrt() { LL x, y, k, P = p[1], res = hp[1]; for (int i = 2; i <= n; ++i) { LL a = P, b = p[i], c = (hp[i] - res % b + b) % b; LL d = exgcd(a, b, x, y), bg = b / d; if (c % d != 0) return -1; x = I128(c / d) * x % bg; res += x * P; P *= bg; res = (res % P + P) % P; } res = (res % P + P) % P; while (res < dw) res += P; return res; } int main() { int T; for (scanf("%d", &T); T--; ) { scanf("%d %d", &n, &m), s.clear(), dw = 0; for (int i = 1; i <= n; ++i) scanf("%lld", hp + i); for (int i = 1; i <= n; ++i) scanf("%lld", p + i); for (int i = 1; i <= n; ++i) scanf("%lld", sw + i); while (m--) { LL t; scanf("%lld", &t); s.insert(t); } for (int i = 1; i <= n; ++i) { auto it = s.upper_bound(hp[i]); if (it != s.begin()) --it; atk[i] = *it; s.erase(it); s.insert(sw[i]); } for (int i = 1; i <= n; ++i) { LL x, y; LL d = exgcd(atk[i], p[i], x, y); if (hp[i] % d) { puts("-1"); goto E_O_W_T; } hp[i] /= d, atk[i] /= d, p[i] /= d; exgcd(atk[i], p[i], x, y); x = (x % p[i] + p[i]) % p[i]; dw = std::max(dw, (LL)std::ceil(DB(hp[i]) / atk[i])); hp[i] = I128(hp[i]) * x % p[i]; } printf("%lld\n", excrt()); E_O_W_T:; } return 0; }
|