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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll; typedef long double ld; const ll maxn = 1e5 + 10; const ll mod = 1e9 + 7;
inline ll __read() { ll x(0), t(1); char o (getchar()); while (o < '0' || o > '9') { if (o == '-') t = -1; o = getchar(); } for (; o >= '0' && o <= '9'; o = getchar()) x = (x << 1) + (x << 3) + (o ^ 48); return x * t; }
ll a[maxn], p[maxn], b[maxn]; ll x, y, g; multiset <ll> s;
void exgcd(ll a, ll b, ll &x, ll &y) { if (!b) { x = 1, y = 0; g = a; return ; } exgcd(b, a % b, y, x); y -= a / b * x; }
inline ll mul(ll a, ll b, ll mod) { ll res = a * b - (ll)((ld)a / mod * b + 1e-8) * mod; return res < 0 ? res + mod : res % mod; }
signed main() { ll T = __read(); multiset <ll> :: iterator it; __next: while (T--){ ll n = __read(), m = __read(); for (ll i = 1; i <= n; ++i) a[i] = __read(); for (ll i = 1; i <= n; ++i) p[i] = __read(); for (ll i = 1; i <= n; ++i) b[i] = __read();
s.clear();
for (ll i = 1; i <= m; ++i) s.insert(__read());
ll mx(0), c(0); m = 1;
for (ll i = 1; i <= n; ++i) { it = s.upper_bound(a[i]); if (it != s.begin()) --it; ll atk = *it; s.erase(it); s.insert(b[i]); mx = max(mx, (a[i] - 1) / atk + 1); atk %= p[i], a[i] %= p[i]; if (!atk && a[i]) { puts("-1"); goto __next; } else if (!atk && !a[i]) { continue; } exgcd(atk, p[i], x, y); if (a[i] % g) { puts("-1"); goto __next; }
p[i] /= g; a[i] = mul(a[i] / g, (x % p[i] + p[i]) % p[i], p[i]);
exgcd(m, p[i], x, y);
if ((a[i] - c) % g) { puts("-1"); goto __next; }
m = m / g * p[i]; c = (c + mul(mul(m / p[i], (x % m + m) % m, m), ((a[i] - c) % m + m) % m, m)) % m; }
printf ("%lld\n", c >= mx ? c : c + m * ((mx - c - 1) / m + 1)); } system("pause"); }
|