#include <iostream>
#include <cstdlib>
#include <cstdio>
#define maxn 1000006
#define INF 1LL << 60
#define ll long long
using namespace std;
struct Node{
Node *ch[2];
int val, cnt, sze;
ll key;
Node(ll V) : key(V){
ch[0] = ch[1] = NULL;
val = rand();
sze = cnt = 1;
}
ll cmp(ll x) const{
if (x == key) return -1;
return x < key ? 0 : 1;
}
void maintain(){
sze = cnt;
if (ch[0] != NULL)
sze += ch[0] -> sze;
if (ch[1] != NULL)
sze += ch[1] -> sze;
}
};
void Rotate(Node* &o, int d){
Node* k = o -> ch[d ^ 1];
o -> ch[d ^ 1] = k -> ch[d];
k -> ch[d] = o;
o -> maintain();
k -> maintain();
o = k;
}
void ins(Node* &o, ll x){
if (o == NULL)
o = new Node(x);
else{
int d = o -> cmp(x);
if (d == -1){
o -> sze ++;
o -> cnt ++;
return;
}
ins(o -> ch[d], x);
if (o -> ch[d] -> val > o -> val)
Rotate(o, d ^ 1);
}
o -> maintain();
}
void del(Node* &o, ll x){
if (o == NULL)
return;
int d = o -> cmp(x);
if (d == -1){
if (o -> cnt > 1){
o -> cnt --;
o -> sze --;
return;
}
if (o -> ch[0] != NULL && o -> ch[1] != NULL){
int d2 = (o -> ch[0] -> val < o -> ch[1] -> val ? 0 : 1);
Rotate(o, d2);
del(o -> ch[d2], x);
}
else{
Node* u = o;
if (o -> ch[0] == NULL)
o = o -> ch[1];
else if (o -> ch[1] == NULL)
o = o -> ch[0];
delete u;
}
}
else
del(o -> ch[d], x);
if (o != NULL)
o -> maintain();
}
ll pre(Node* o, ll x){
if (o == NULL)
return -INF;
if (o -> key == x)
return x;
if (o -> key < x)
return max(o -> key, pre(o -> ch[1], x));
else
return pre(o -> ch[0], x);
}
ll suc(Node* o, ll x){
if (o == NULL)
return INF;
if (o -> key > x)
return min(o -> key, suc(o -> ch[0], x));
else
return suc(o -> ch[1], x);
}
void exgcd(ll a, ll b, ll &x, ll &y){
if (b == 0){
x = 1;
y = 0;
return;
}
ll tx, ty;
exgcd(b, a % b, tx, ty);
x = ty;
y = tx - a / b * ty;
}
ll gcd(ll x, ll y){
if (y == 0)
return x;
return gcd(y, x % y);
}
ll mul(ll x, ll y, ll mod){
ll ans = 0;
for (ll i = 1; i <= y; i <<= 1, x = (x + x) % mod)
if (y & i)
ans = (ans + x) % mod;
return ans;
}
ll a[maxn], m[maxn], get[maxn];
bool merg(ll a1, ll m1, ll a2, ll m2, ll& a3, ll& m3){
ll d = gcd(m1, m2), c = a2 - a1;
if (c % d)
return false;
c = (c % m2 + m2) % m2;
c /= d;
m1 /= d;
m2 /= d;
ll tmp1 = 0, tmp2 = 0;
exgcd(m1, m2, tmp1, tmp2);
tmp1 = (tmp1 + m2) % m2;
c = mul(c, tmp1, m2);
m3 = m1 * m2 * d;
c = mul(c, m1, m3);
c = mul(c, d, m3);
a3 = ((c + a1) % m3 + m3) % m3;
return true;
}
ll read(){
ll cnt = 0;
char ch = getchar();
while (ch < '0' || '9' < ch)
ch = getchar();
while ('0' <= ch && ch <= '9'){
cnt = cnt * 10 + ch - '0';
ch = getchar();
}
return cnt;
}
Node* o[10];
int main(){
// freopen("dragon.in", "r", stdin);
// freopen("dragon.out", "w", stdout);
int T = read();
for (int cas = 1; cas <= T; ++ cas){
int n = read(), M = read();
for (int i = 1; i <= n; ++ i)
a[i] = read();
long long mx = 0;
for (int i = 1; i <= n; ++ i)
m[i] = read();
for (int i = 1; i <= n; ++ i)
get[i] = read();
for (int i = 1; i <= M; ++ i)
ins(o[cas], read());
bool nosol = false;
int fir = -1;
long long mn = -INF;
for (int i = 1; i <= n; ++ i){
ll x = pre(o[cas], a[i]);
if (x == -INF)
x = suc(o[cas], a[i]);
ll tmp = x;
ll d = gcd(x, m[i]);
if (x % m[i] == 0 && a[i] % m[i] == 0){
if (a[i] % x == 0)
mn = max(mn, a[i] / x);
else
mn = max(mn, a[i] / x + 1);
a[i] = -1;
del(o[cas], tmp);
ins(o[cas], get[i]);
continue;
}
if (a[i] % d){
nosol = true;
cout << -1 << endl;
break;
}
if (fir == -1)
fir = i;
x /= d, m[i] /= d, a[i] /= d;
ll tmp1 = 0, tmp2 = 0;
exgcd(x, m[i], tmp1, tmp2);
tmp1 = (tmp1 + m[i]) % m[i];
a[i] = mul(a[i] % m[i], tmp1, m[i]);
del(o[cas], tmp);
ins(o[cas], get[i]);
}
if (nosol)
continue;
if (fir == -1){
cout << mn << endl;
continue;
}
ll a0 = a[fir], m0 = m[fir];
for (int i = fir + 1; i <= n; ++ i){
if (a[i] == -1)
continue;
ll _a, _m;
if (merg(a0, m0, a[i], m[i], _a, _m) == false){
cout << -1 << endl;
nosol = true;
break;
}
a0 = _a, m0 = _m;
}
if (! nosol){
if (mn == -INF || a0 >= mn)
cout << a0 << endl;
else{
long long k;
if ((mn - a0) % m0 == 0)
k = (mn - a0) / m0;
else
k = (mn - a0) / m0 + 1;
cout << a0 + k * m0 << endl;
}
}
}
// fclose(stdin);
// fclose(stdout);
return 0;
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 35.21 ms | 2 MB + 340 KB | Accepted | Score: 5 | 显示更多 |
Testcase #2 | 35.097 ms | 2 MB + 340 KB | Accepted | Score: 5 | 显示更多 |
Testcase #3 | 42.791 ms | 2 MB + 340 KB | Accepted | Score: 5 | 显示更多 |
Testcase #4 | 42.636 ms | 2 MB + 340 KB | Accepted | Score: 5 | 显示更多 |
Testcase #5 | 4.455 ms | 124 KB | Accepted | Score: 5 | 显示更多 |
Testcase #6 | 4.144 ms | 92 KB | Accepted | Score: 5 | 显示更多 |
Testcase #7 | 4.118 ms | 84 KB | Accepted | Score: 5 | 显示更多 |
Testcase #8 | 42.01 us | 48 KB | Accepted | Score: 5 | 显示更多 |
Testcase #9 | 42 us | 48 KB | Accepted | Score: 5 | 显示更多 |
Testcase #10 | 40.7 us | 48 KB | Accepted | Score: 5 | 显示更多 |
Testcase #11 | 40.86 us | 48 KB | Accepted | Score: 5 | 显示更多 |
Testcase #12 | 39.64 us | 48 KB | Accepted | Score: 5 | 显示更多 |
Testcase #13 | 39.56 us | 48 KB | Accepted | Score: 5 | 显示更多 |
Testcase #14 | 449.874 ms | 22 MB + 716 KB | Accepted | Score: 5 | 显示更多 |
Testcase #15 | 450.814 ms | 22 MB + 548 KB | Accepted | Score: 5 | 显示更多 |
Testcase #16 | 593.987 ms | 2 MB + 340 KB | Accepted | Score: 5 | 显示更多 |
Testcase #17 | 601.949 ms | 2 MB + 340 KB | Accepted | Score: 5 | 显示更多 |
Testcase #18 | 614.186 ms | 2 MB + 340 KB | Accepted | Score: 5 | 显示更多 |
Testcase #19 | 611.664 ms | 2 MB + 340 KB | Accepted | Score: 5 | 显示更多 |
Testcase #20 | 611.826 ms | 2 MB + 340 KB | Accepted | Score: 5 | 显示更多 |