#include <set>
#include <cstdio>
#include <cctype>
#include <algorithm>
//#define gc() getchar()
#define MAXIN 1000000
#define Failed {puts("-1"); return;}
#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
#define set_It std::multiset<LL>::iterator
typedef long long LL;
const int N=1e5+7;
int n,m,rew[N]/*reward*/;
LL hp[N],p[N],a[N],md[N];//Ans = a[i] (mod md[i])
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)) Failed;
ans=std::max(ans,(int)((hp[i]+(*it)-1)/(*it)));//(int)ceil(1.0*hp[i]/(*it)));
st.erase(it), st.insert(rew[i]);
}
printf("%d\n",ans);
}
//void Spec2()
//{
// int ans=1; set_It it;
// for(int i=1,need; i<=n; ++i)
// {
// it=st.upper_bound(hp[i]);
// if(it!=st.begin()) --it;
// if(!(*it)) Failed;
// need=(hp[i]+(*it)-1)/(*it), ans=ans/Gcd(ans,need)*need;
// 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;}
// f1=1;//然而数据没有这种情况...还是要有的
// for(int i=1; i<=n; ++i) if(p[i]!=hp[i]) {f1=0; break;}
// if(f1) {Spec2(); 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)) Failed;
int atk=*it;
LL gcd,x0,y0,P;
Exgcd(atk,p[i],gcd,x0,y0);
if(hp[i]%gcd) Failed;
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) Failed;
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)/*直接x*M会炸?...*/, 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.016 ms | 2 MB + 908 KB | Accepted | Score: 5 | 显示更多 |
Testcase #2 | 18.869 ms | 2 MB + 908 KB | Accepted | Score: 5 | 显示更多 |
Testcase #3 | 21.326 ms | 2 MB + 908 KB | Accepted | Score: 5 | 显示更多 |
Testcase #4 | 21.078 ms | 2 MB + 908 KB | Accepted | Score: 5 | 显示更多 |
Testcase #5 | 1.827 ms | 200 KB | Accepted | Score: 5 | 显示更多 |
Testcase #6 | 1.782 ms | 200 KB | Accepted | Score: 5 | 显示更多 |
Testcase #7 | 1.73 ms | 200 KB | Accepted | Score: 5 | 显示更多 |
Testcase #8 | 15.77 us | 48 KB | Accepted | Score: 5 | 显示更多 |
Testcase #9 | 15.87 us | 48 KB | Accepted | Score: 5 | 显示更多 |
Testcase #10 | 14.91 us | 48 KB | Accepted | Score: 5 | 显示更多 |
Testcase #11 | 15.27 us | 48 KB | Accepted | Score: 5 | 显示更多 |
Testcase #12 | 15.04 us | 48 KB | Accepted | Score: 5 | 显示更多 |
Testcase #13 | 15.77 us | 48 KB | Accepted | Score: 5 | 显示更多 |
Testcase #14 | 224.592 ms | 7 MB + 476 KB | Accepted | Score: 5 | 显示更多 |
Testcase #15 | 225.244 ms | 7 MB + 476 KB | Accepted | Score: 5 | 显示更多 |
Testcase #16 | 393.115 ms | 8 MB + 1012 KB | Accepted | Score: 5 | 显示更多 |
Testcase #17 | 392.837 ms | 8 MB + 1012 KB | Accepted | Score: 5 | 显示更多 |
Testcase #18 | 386.027 ms | 8 MB + 1012 KB | Accepted | Score: 5 | 显示更多 |
Testcase #19 | 388.307 ms | 8 MB + 1012 KB | Accepted | Score: 5 | 显示更多 |
Testcase #20 | 385.017 ms | 8 MB + 1012 KB | Accepted | Score: 5 | 显示更多 |