#include<bits/stdc++.h>
using namespace std;
#define debug
const int N = 1e5+5;
typedef long long ll;
ll read(){
ll d=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))d=d*10+ch-'0',ch=getchar();
return d*f;
}
// input data
int n,m;
ll HP[N],PLUS[N],ATK[N];
multiset<ll>st;
void csh(){
st.clear();
}
void input(){
n=read(),m=read();
for(int i=0;i<n;i++)
HP[i] = read();
for(int i=0;i<n;i++)
PLUS[i] = read();
for(int i=0;i<n;i++)
ATK[i] = read();
for(int i=1;i<=m;i++)
st.insert(read());
}
ll mul(ll a,ll b,ll mod){
ll res=0;
b=b%mod;if(b<0)b+=mod;
while(b>0){
if(b&1) res=(res+a)%mod;
a=(a+a)%mod;
b>>=1;
}
return (res+mod)%mod;
}
// excrt
ll exgcd(ll a,ll b,ll& x,ll& y){
if(b==0) {
x=1;y=0;
return a;
}
ll g = exgcd(b,a%b,x,y);
ll tmp = x;
x=y;
y=tmp-a/b*y;
return g;
}
ll inv(ll a,ll p){
ll x,y;
exgcd(a,p,x,y);
return (x%p+p)%p;
}
ll a[N],r[N];
ll get_aa(ll H) {
auto it = st.begin();
if((*it) >= H) {
ll res = *it;
st.erase(it);
return res;
}
it = st.upper_bound(H);
it--;
ll res = *it;
st.erase(it);
return res;
}
ll solve(ll a,ll b,ll c) {
ll x,y;
ll d = exgcd(a,b,x,y);
if(c%d) return -1;
ll mod = b/d;
x = mul(x,c/d,mod);
return x;
}
void tp1(){
ll ans = 0;
for(int i=0;i<n;i++){
ll aa = HP[i];
ll cc = ATK[i];
ll tmp = aa / cc;
if(aa%cc) tmp++;
ans = max(ans, tmp);
}
printf("%lld\n",ans);
}
bool init(){
for(int i=0;i<n;i++){
ll tmp = get_aa(HP[i]);
st.insert(ATK[i]);
ATK[i] = tmp;
}
bool flag = 0;
for(int i=0;i<n;i++){
if(PLUS[i]>1) {
flag=1;
break;
}
}
if(flag == 0) {
tp1();
return 0;
}
for(int i=0;i<n;i++) {
if(ATK[i]==0){
if(PLUS[i] == HP[i]) {
ATK[i] = PLUS[i] = 1;
HP[i] = 0;
}
else {
puts("-1");
return 0;
}
}
}
for(int i=0;i<n;i++) {
ll sx,sy;
ll g = exgcd(ATK[i],PLUS[i],sx,sy);
if(HP[i]%g) {
puts("-1");
return 0;
}
PLUS[i] = PLUS[i]/g;
sx = (sx%PLUS[i]+PLUS[i])%PLUS[i];
HP[i] = mul(sx,HP[i]/g,PLUS[i]);
a[i] = PLUS[i];
r[i] = HP[i];
}
return 1;
}
ll excrt()
{
ll x,y;
ll M=a[0],ans=r[0];
for(int i=1;i<n;i++)
{
ll _a=M,b=a[i],c=(r[i]-ans%b+b)%b;
ll gcd=exgcd(_a,b,x,y),bg=b/gcd;
if(c%gcd!=0) return -1;
x=mul(x,c/gcd,bg);
ans+=x*M;
M*=bg;
ans=(ans%M+M)%M;
}
return ans;
}
int main(){
int _ = read();
while(_--) {
csh();
input();
if(init() == 0) continue;
ll x = excrt();
printf("%lld\n",x);
}
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 26.157 ms | 2 MB + 340 KB | Accepted | Score: 5 | 显示更多 |
Testcase #2 | 26.015 ms | 2 MB + 340 KB | Accepted | Score: 5 | 显示更多 |
Testcase #3 | 30.1 ms | 2 MB + 340 KB | Accepted | Score: 5 | 显示更多 |
Testcase #4 | 29.863 ms | 2 MB + 340 KB | Accepted | Score: 5 | 显示更多 |
Testcase #5 | 2.417 ms | 148 KB | Accepted | Score: 5 | 显示更多 |
Testcase #6 | 2.35 ms | 148 KB | Accepted | Score: 5 | 显示更多 |
Testcase #7 | 2.402 ms | 148 KB | Accepted | Score: 5 | 显示更多 |
Testcase #8 | 41.91 us | 60 KB | Accepted | Score: 5 | 显示更多 |
Testcase #9 | 42.15 us | 60 KB | Accepted | Score: 5 | 显示更多 |
Testcase #10 | 39.83 us | 60 KB | Accepted | Score: 5 | 显示更多 |
Testcase #11 | 40.95 us | 60 KB | Accepted | Score: 5 | 显示更多 |
Testcase #12 | 40.24 us | 60 KB | Accepted | Score: 5 | 显示更多 |
Testcase #13 | 40.5 us | 60 KB | Accepted | Score: 5 | 显示更多 |
Testcase #14 | 241.967 ms | 6 MB + 932 KB | Accepted | Score: 5 | 显示更多 |
Testcase #15 | 242.615 ms | 6 MB + 932 KB | Accepted | Score: 5 | 显示更多 |
Testcase #16 | 527.103 ms | 8 MB + 440 KB | Accepted | Score: 5 | 显示更多 |
Testcase #17 | 526.693 ms | 8 MB + 440 KB | Accepted | Score: 5 | 显示更多 |
Testcase #18 | 526.857 ms | 8 MB + 440 KB | Accepted | Score: 5 | 显示更多 |
Testcase #19 | 520.142 ms | 8 MB + 440 KB | Accepted | Score: 5 | 显示更多 |
Testcase #20 | 501.764 ms | 8 MB + 440 KB | Accepted | Score: 5 | 显示更多 |