/*
苟活者在淡红的血色中,会依稀看到微茫的希望
*/
#include<bits/stdc++.h>
using namespace std;
inline long long read() {
long long x = 0,f = 1;
char c = getchar();
while(c < '0' || c > '9'){if(c == '-')f = - 1; c = getchar(); }
while(c <= '9' && c >= '0')x = x * 10 + c - '0',c = getchar();
return x * f;
}
int n,m;
#define LL long long
const int maxn = 5000007;
LL a[maxn],p[maxn]; // x atk[i] = a[i] ( % p[i])
LL atk[maxn],tatk[maxn];
vector<LL>v;
namespace Sp {
void insert(LL x) { v.insert(lower_bound(v.begin(),v.end(),x),x); }
void erase(LL x) { v.erase (lower_bound(v.begin(),v.end(),x)); }
LL pre(LL x) {
return v[max(lower_bound(v.begin(),v.end(),x)-v.begin() - 1,0)];
}
} ;
LL gcd(LL a,LL b) {return b == 0 ? a : gcd(b,a % b);}
LL exgcd(LL a,LL b,LL &x,LL &y) {
if(b == 0) {x = 1,y = 0;return a; }
LL ret = exgcd(b,a % b,x,y);
LL tmp = x;x = y;y = tmp - (a / b) * y;
return ret;
}
LL inv(LL a,LL b) {
LL x,y;
exgcd(a,b,x,y);
while(x < 0) x += b;
return x;
}
void work() {
LL ans = 0;
for(int i = 1;i <= n;++ i) {
ans = std::max(ans,a[i] % atk[i] == 0 ? a[i] / atk[i] : a[i] / atk[i] + 1);
}
printf("%lld\n",ans);
}
LL M[maxn],C[maxn];
inline LL add(LL x,LL y,LL mod) { return x + y >= mod ? x + y - mod : x + y; }
LL mul(LL x,LL k,LL mod) {
LL ret = 0 ;
x %= mod;
for(;k;k >>= 1,x = add(x,x,mod))
if(k & 1) ret = add(ret,x,mod);
return ret;
}
void init() {
v.clear();
n = read(),m = read();
bool flag = 0;
for(int i = 1;i <= n;++ i) a[i] = read();
for(int i = 1;i <= n;++ i){ p[i] = read();if(p[i] != 1) flag = 1; }
for(int j = 1;j <= n;++ j) tatk[j] = read();
for(int k,i = 1;i <= m;++ i)
k = read(),Sp::insert(k);
for(int i = 1;i <= n;++ i) {
atk[i] = Sp::pre(a[i]);
Sp::erase(atk[i]);
Sp::insert(tatk[i]);
}
if(!flag) {work();return; }
int num = 0;
for(int i = 1;i <= n;++ i) {
a[i] %= p[i],atk[i] %= p[i];
if(!a[i] && !atk[i]) continue;
else if(!atk[i]){puts("-1");return;}
LL d = gcd(atk[i],p[i]);
if(a[i] % d != 0) {continue; }
a[i] /= d,atk[i] /= d,p[i] /= d;
C[++ num] = mul(a[i] , ((inv(atk[i],p[i]) % p[i] + p[i]) % p[i]),p[i]);
M[num] = p[i];
}
LL m = M[1],A = C[1],x,y,t;
for(int i = 2;i <= num;++ i) {
LL d = exgcd(m,M[i],x,y);
t = (M[i] / d);
//if((C[i]-A)%d == 0 || (a[i] - A) % d == -0 ) {
x = mul((x % t + t) % t,(((C[i] - A)/d) % t + t) % t,t);
LL MOD = (m / d) * (M[i]);
A = (mul(m , x, MOD) + A % MOD) % MOD;
m = MOD;
// else {puts("-1"); return;};
}
A = (A % m + m) % m;
//if(A < 0) puts("wt?0") ;
printf("%lld\n",A);
}
main() {
//freopen("dragon2.in","r",stdin);
//freopen("dragon.out","w",stdin);
int t = read();
while(t --) {
init();
}
return 0;
}
/*
2
3 3
3 5 7
4 6 10
7 3 9
1 9 1000
3 2
3 5 6
4 8 7
1 1 1
1 1
*/
| Compilation | N/A | N/A | Compile Error | Score: N/A | 显示更多 |