#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <vector>
#include <set>
#include <queue>
using namespace std;
typedef long long LL;
bool isd(char ch){
return '0'<=ch&&ch<='9';
}
LL read(){
LL x=0;
char ch=getchar();
while (!isd(ch))
ch=getchar();
while (isd(ch))
x=(x<<1)+(x<<3)+ch-48,ch=getchar();
return x;
}
const int N=100005;
int T,n,m;
LL a[N],p[N],h[N],v[N],x[N];
multiset <LL> S;
LL ex_gcd(LL a,LL b,LL &x,LL &y){
if (!b){
x=1,y=0;
return a;
}
LL res=ex_gcd(b,a%b,y,x);
y-=x*(a/b);
return res;
}
LL gcd(LL a,LL b){
return b?gcd(b,a%b):a;
}
LL inv(LL v,LL p){
LL x,y,g=ex_gcd(v,p,x,y);
if (g>1)
return -1;
return (x+p)%p;
}
LL Mul(LL a,LL b,LL p){
a=(a%p+p)%p;
b=(b%p+p)%p;
LL ans=0;
for (;a;a>>=1,b=(b<<1)%p)
if (a&1LL)
ans=(ans+b)%p;
return ans;
}
bool CRT(LL w1,LL p1,LL w2,LL p2,LL &w,LL &p){
LL x,y,z=w2-w1,g=ex_gcd(p1,p2,x,y);
if (z%g)
return 0;
LL t=z/g;
x=Mul(x,t,p2/g);
p=p1/g*p2;
w=((w1+Mul(x,p1,p))%p+p)%p;
return 1;
}
LL Solve(){
n=read(),m=read();
for (int i=1;i<=n;i++)
a[i]=read();
for (int i=1;i<=n;i++)
p[i]=read();
for (int i=1;i<=n;i++)
h[i]=read();
S.clear();
while (m--)
S.insert(read());
for (int i=1;i<=n;i++){
multiset <LL> :: iterator p=S.begin();
if ((*p)<a[i])
p=--S.upper_bound(a[i]);
v[i]=*p,S.erase(p);
S.insert(h[i]);
}
for (int i=1;i<=n;i++){
LL g=gcd(a[i],gcd(v[i],p[i]));
v[i]/=g,p[i]/=g,a[i]/=g;
LL Inv=inv(v[i],p[i]);
if (Inv<0)
return -1LL;
x[i]=Mul(a[i],Inv,p[i]);
}
LL W=x[1],P=p[1];
for (int i=2;i<=n;i++)
if (!CRT(W,P,x[i],p[i],W,P))
return -1LL;
// x = W (mod P)
for (int i=1;i<=n;i++){
LL val=(a[i]+v[i]-1)/v[i];
if (val<=W)
continue;
LL k=(val-W+P-1)/P;
W+=k*P;
}
return W;
}
int main(){
scanf("%d",&T);
while (T--)
printf("%lld\n",Solve());
fclose(stdin);fclose(stdout);
return 0;
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 109.781 ms | 3 MB + 860 KB | Accepted | Score: 5 | 显示更多 |
Testcase #2 | 109.515 ms | 3 MB + 860 KB | Accepted | Score: 5 | 显示更多 |
Testcase #3 | 112.703 ms | 3 MB + 860 KB | Accepted | Score: 5 | 显示更多 |
Testcase #4 | 112.476 ms | 3 MB + 860 KB | Accepted | Score: 5 | 显示更多 |
Testcase #5 | 2.95 ms | 136 KB | Accepted | Score: 5 | 显示更多 |
Testcase #6 | 2.88 ms | 136 KB | Accepted | Score: 5 | 显示更多 |
Testcase #7 | 2.932 ms | 136 KB | Accepted | Score: 5 | 显示更多 |
Testcase #8 | 21.26 us | 48 KB | Accepted | Score: 5 | 显示更多 |
Testcase #9 | 20.38 us | 48 KB | Accepted | Score: 5 | 显示更多 |
Testcase #10 | 19.84 us | 48 KB | Accepted | Score: 5 | 显示更多 |
Testcase #11 | 19.41 us | 48 KB | Accepted | Score: 5 | 显示更多 |
Testcase #12 | 19.91 us | 48 KB | Accepted | Score: 5 | 显示更多 |
Testcase #13 | 20.17 us | 48 KB | Accepted | Score: 5 | 显示更多 |
Testcase #14 | 328.451 ms | 8 MB + 428 KB | Accepted | Score: 5 | 显示更多 |
Testcase #15 | 329.003 ms | 8 MB + 428 KB | Accepted | Score: 5 | 显示更多 |
Testcase #16 | 602.715 ms | 8 MB + 428 KB | Accepted | Score: 5 | 显示更多 |
Testcase #17 | 602.082 ms | 8 MB + 428 KB | Accepted | Score: 5 | 显示更多 |
Testcase #18 | 595.65 ms | 8 MB + 428 KB | Accepted | Score: 5 | 显示更多 |
Testcase #19 | 590.518 ms | 8 MB + 428 KB | Accepted | Score: 5 | 显示更多 |
Testcase #20 | 571.128 ms | 8 MB + 428 KB | Accepted | Score: 5 | 显示更多 |