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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| #include <cstdio> #include <set> #include <algorithm> #include <cassert> using namespace std;
#define int --int128 const int N = 1e6 + 5; int -, n, k, atk[N], nw[N]; int r[N], m[N]; int a[N], p[N]; bool pd = 1; multiset<int> s;
int read() { int w = 0, f = 1; char ch = getchar(); while(ch > '9' || ch < '0') { if(ch == '-') f = -1; ch = getchar(); } while(ch >= '0' && ch <= '9') { w = w * 10 + ch - 48; ch = getchar(); } return w * f; } void write(int x) { if(x < 0) { x = -x; putchar('-'); } if(x > 9) write(x / 10); putchar(x % 10 + 48); } void prework() { for (int i = 1; i <= n; i++) { int res = 0; auto it = s.upper-bound(a[i]); if (it == s.begin()) res = *it; else res = *(--it); s.erase(it); atk[i] = res; s.insert(nw[i]); } } int mul(int a, int b, int p) { a = (a % p + p) % p; b = (b % p + p) % p; return (a * b % p + p) % p;
}
int solve1() { int res = 1; for (int i = 1; i <= n; i++) res = max(res, (a[i] + atk[i] - 1) / atk[i]); return res; }
void exgcd(int a, int b, int& d, int& x, int& y) { if(b == 0) {d = a, x = 1, y = 0; return;} exgcd(b, a % b, d, y, x); y -= x * (a / b); } int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int China() { int M = m[1], R = r[1]; for(int i = 2; i <= n; i++) { int a = M, b = m[i], c = r[i] - R, x, y, d; exgcd(a, b, d, x, y); if(c % d != 0) return -1; x = (mul(x, c / d, b) + b) % b; R += x * M, M *= m[i] / d, R = (R + M) % M; } return R; } int inv(int a, int p) { int d, x, y; exgcd(a, p, d, x, y); return (x % p + p) % p; }
bool build() { int d; for (int i = 1; i <= n; i++) { d = gcd(atk[i], p[i]); d = gcd(d, a[i]); atk[i] /= d, p[i] /= d, a[i] /= d; if (gcd(atk[i], p[i]) != 1) return 0; m[i] = p[i], r[i] = mul(a[i], inv(atk[i], p[i]), p[i]); } return 1; }
signed main() { freopen("dragon.in", "r", stdin); freopen("dragon.out", "w", stdout); for ( - = read(); -; ---) { s.clear(); pd = 1; n = read(), k = read(); for (int i = 1; i <= n; i++) a[i] = read(); for (int i = 1; i <= n; i++) {p[i] = read(); if (p[i] != 1) pd = 0;} for (int i = 1; i <= n; i++) nw[i] = read(); for (int i = 1, x; i <= k; i++) x = read(), s.insert(x); prework(); if (pd == 1) {write(solve1()); puts("");continue;} if (!build()) {puts("-1"); continue;} write(China()); puts(""); } return 0; }
|