#include<cstdio>
#include<set>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn=100010;
multiset<ll> mts;
int t,n,m;
bool spec,exist,same;
ll hp[maxn],rec[maxn],a[maxn],b[maxn];
int award[maxn],atk[maxn];
ll qmul(ll a,ll b,ll mod){
ll ans=0;
for(;b;b>>=1,a=(a+a)%mod) if(b&1) ans=(ans+a)%mod;
return ans;
}
ll exgcd(ll a,ll b,ll &x,ll &y){
if(!b){x=1;y=0;return a;}
ll d=exgcd(b,a%b,y,x);y-=a/b*x;return d;
}
ll gcd(ll a,ll b){
if(!b) return a;
return gcd(b,a%b);
}
int main(){
scanf("%d",&t);
while(t--){
spec=false;same=exist=true;
mts.clear();
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%lld",hp+i);
for(int i=1;i<=n;i++){scanf("%lld",rec+i);if(rec[i]<hp[i]) spec=true;}
for(int i=1;i<=n;i++) scanf("%d",award+i);
for(int i=1;i<=m;i++){
int x;
scanf("%d",&x);
mts.insert(x);
}
for(int i=1;i<=n;i++){
multiset<ll>::iterator it;
if(hp[i]<*mts.begin()) it=mts.begin();
else it=mts.upper_bound(hp[i]),it--;
atk[i]=*it;
mts.erase(it);
mts.insert(award[i]);
}
if(spec){
ll ans=0;
for(int i=1;i<=n;i++) ans=max(ans,ll(ceil(1.0*hp[i]/atk[i])));
printf("%lld\n",ans);
continue;
}
for(int i=1;i<=n;i++){
if(!atk[i]){
if(hp[i]==rec[i]){a[i]=0;b[i]=1;continue;}
else{puts("-1");exist=false;break;}
}
if(hp[i]!=rec[i]) same=false;
ll x,y,d=exgcd(atk[i],rec[i],x,y);
if(hp[i]%d){puts("-1");exist=false;break;}
x=(x+rec[i])%rec[i];b[i]=rec[i]/d;a[i]=qmul(x,hp[i]/d,b[i]);
}
if(same){
ll ans=ll(ceil(1.0*hp[1]/atk[1]));
for(int i=2;i<=n;i++){
ll x=ceil(1.0*hp[i]/atk[i]),d=gcd(ans,x);
ans=ans/d*x;
}
printf("%lld\n",ans);continue;
}
if(!exist) continue;
ll ans=a[1],mod=b[1];
for(int i=2;i<=n;i++){
ll x,y,d=exgcd(mod,b[i],x,y),r=((a[i]-ans)%b[i]+b[i])%b[i],tmp=mod/d*b[i];
if(r%d){puts("-1");exist=false;break;}
x=(x+b[i])%b[i];x=qmul(x,r/d,b[i]);
ans=(qmul(mod,x,tmp)+ans)%tmp;
mod=tmp;
}
if(!exist) continue;
printf("%lld\n",ans);
}
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 46.921 ms | 2 MB + 324 KB | Accepted | Score: 5 | 显示更多 |
Testcase #2 | 46.64 ms | 2 MB + 324 KB | Accepted | Score: 5 | 显示更多 |
Testcase #3 | 48.77 ms | 2 MB + 324 KB | Accepted | Score: 5 | 显示更多 |
Testcase #4 | 48.435 ms | 2 MB + 324 KB | Accepted | Score: 5 | 显示更多 |
Testcase #5 | 2.557 ms | 140 KB | Accepted | Score: 5 | 显示更多 |
Testcase #6 | 2.517 ms | 140 KB | Accepted | Score: 5 | 显示更多 |
Testcase #7 | 2.562 ms | 140 KB | Accepted | Score: 5 | 显示更多 |
Testcase #8 | 19.61 us | 52 KB | Accepted | Score: 5 | 显示更多 |
Testcase #9 | 19.51 us | 52 KB | Accepted | Score: 5 | 显示更多 |
Testcase #10 | 18.69 us | 52 KB | Accepted | Score: 5 | 显示更多 |
Testcase #11 | 18.29 us | 52 KB | Accepted | Score: 5 | 显示更多 |
Testcase #12 | 19.11 us | 52 KB | Accepted | Score: 5 | 显示更多 |
Testcase #13 | 19.78 us | 52 KB | Accepted | Score: 5 | 显示更多 |
Testcase #14 | 270.216 ms | 6 MB + 916 KB | Accepted | Score: 5 | 显示更多 |
Testcase #15 | 270.749 ms | 6 MB + 916 KB | Accepted | Score: 5 | 显示更多 |
Testcase #16 | 538.909 ms | 8 MB + 428 KB | Accepted | Score: 5 | 显示更多 |
Testcase #17 | 537.969 ms | 8 MB + 428 KB | Accepted | Score: 5 | 显示更多 |
Testcase #18 | 560.902 ms | 8 MB + 428 KB | Accepted | Score: 5 | 显示更多 |
Testcase #19 | 559.637 ms | 8 MB + 428 KB | Accepted | Score: 5 | 显示更多 |
Testcase #20 | 556.668 ms | 8 MB + 428 KB | Accepted | Score: 5 | 显示更多 |