#include <set>
#include <cstdio>
#include <cctype>
#include <algorithm>
#define MAXIN 1000000
#define Failed return (void)puts("-1")
#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
#define set_It std::multiset<LL>::iterator
#define LL long long
const int N=1e5+7;
int n,m,rew[100009];
LL hp[100007],p[100007],a[100007],md[100007];
std::multiset<LL> st;
char IN[MAXIN],*SS=IN,*TT=IN;
inline int read() {
int now=0;
register char c=gc();
for(; !isdigit(c); c=gc());
for(; isdigit(c); now=now*10+c-'0',c=gc());
return now;
}
inline LL readll() {
LL now=0;
register char c=gc();
for(; !isdigit(c); c=gc());
for(; isdigit(c); now=now*10+c-'0',c=gc());
return now;
}
inline LL Mult(LL a,LL b,LL p) {
LL tmp=a*b-(LL)((long double)a/p*b+1e-8)*p;
return tmp<0?tmp+p:tmp;
}
LL Gcd(LL a,LL b) {
return b?Gcd(b,a%b):a;
}
void Exgcd(LL a,LL b,LL &g,LL &x,LL &y) {
if(!b) g=a,x=1,y=0;
else Exgcd(b,a%b,g,y,x),y-=a/b*x;
}
void Spec1() {
int ans=0;
set_It it;
for(int i=1; i<=n; ++i) {
it=st.upper_bound(hp[i]);
if(it!=st.begin()) --it;
if(!(*it)) return (void)puts("-1");
ans=std::max(ans,(int)((hp[i]+(*it)-1)/(*it)));
st.erase(it), st.insert(rew[i]);
}
printf("%d\n",ans);
}
void Solve() {
bool f1=1;
for(int i=1; i<=n; ++i) if(p[i]!=1) {
f1=0;
break;
}
if(f1) {
Spec1();
return;
}
set_It it;
int cnt=0;
for(int i=1; i<=n; ++i) {
it=st.upper_bound(hp[i]);
if(it!=st.begin()) --it;
if(hp[i]&&!(*it)) return (void)puts("-1");
int atk=*it;
LL gcd,x0,y0,P;
Exgcd(atk,p[i],gcd,x0,y0);
if(hp[i]%gcd) return (void)puts("-1");
P=p[i]/gcd, x0=(x0%P+P)%P;
a[++cnt]=Mult(x0,hp[i]/gcd,P), md[cnt]=P;
st.erase(it), st.insert(rew[i]);
}
LL A=a[1],M=md[1],g,x,y,t,Mod;
for(int i=2; i<=cnt; ++i) {
Exgcd(M,md[i],g,x,y);
if((a[i]-A)%g) return (void)puts("-1");
t=md[i]/g, x=Mult(x,(a[i]-A)/g,t), x=(x%t+t)%t;
Mod=M*t, A+=Mult(x,M,Mod), A%=Mod, M=Mod;
}
printf("%lld\n",A);
}
int main() {
for(int Case=read(); Case--; ) {
st.clear();
n=read(), m=read();
for(int i=1; i<=n; ++i) hp[i]=readll();
for(int i=1; i<=n; ++i) p[i]=readll();
for(int i=1; i<=n; ++i) rew[i]=read();
for(int i=1; i<=m; ++i) st.insert(read());
Solve();
}
return 0;
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 19.17 ms | 2 MB + 908 KB | Accepted | Score: 5 | 显示更多 |
Testcase #2 | 18.957 ms | 2 MB + 908 KB | Accepted | Score: 5 | 显示更多 |
Testcase #3 | 21.26 ms | 2 MB + 908 KB | Accepted | Score: 5 | 显示更多 |
Testcase #4 | 21.171 ms | 2 MB + 908 KB | Accepted | Score: 5 | 显示更多 |
Testcase #5 | 1.834 ms | 200 KB | Accepted | Score: 5 | 显示更多 |
Testcase #6 | 1.785 ms | 200 KB | Accepted | Score: 5 | 显示更多 |
Testcase #7 | 1.728 ms | 200 KB | Accepted | Score: 5 | 显示更多 |
Testcase #8 | 16.2 us | 48 KB | Accepted | Score: 5 | 显示更多 |
Testcase #9 | 16.68 us | 48 KB | Accepted | Score: 5 | 显示更多 |
Testcase #10 | 15.17 us | 48 KB | Accepted | Score: 5 | 显示更多 |
Testcase #11 | 14.94 us | 48 KB | Accepted | Score: 5 | 显示更多 |
Testcase #12 | 15.32 us | 48 KB | Accepted | Score: 5 | 显示更多 |
Testcase #13 | 16.69 us | 48 KB | Accepted | Score: 5 | 显示更多 |
Testcase #14 | 225.228 ms | 7 MB + 476 KB | Accepted | Score: 5 | 显示更多 |
Testcase #15 | 225.24 ms | 7 MB + 476 KB | Accepted | Score: 5 | 显示更多 |
Testcase #16 | 393.073 ms | 8 MB + 1012 KB | Accepted | Score: 5 | 显示更多 |
Testcase #17 | 392.338 ms | 8 MB + 1012 KB | Accepted | Score: 5 | 显示更多 |
Testcase #18 | 386.503 ms | 8 MB + 1012 KB | Accepted | Score: 5 | 显示更多 |
Testcase #19 | 387.797 ms | 8 MB + 1012 KB | Accepted | Score: 5 | 显示更多 |
Testcase #20 | 385.521 ms | 8 MB + 1012 KB | Accepted | Score: 5 | 显示更多 |