最后三个点WA掉了,
全是输出-1
求各位大佬帮帮蒟蒻
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <set>
#define ll long long
using namespace std;
const int maxn = 100010;
bool flag = 0;
int t,n,mm;
ll a[maxn],p[maxn],gai[maxn];
//1,用exgcd处理出同余方程组
//a表示原血量,p回复量,屠龙后获得新剑
ll s[maxn],m[maxn];
//2,excrt解同余方程组
//同余的数,模数
multiset<ll> atk;
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 x1 = x,y1 = y;
x = y1;
y = x1 - a/b * y1;
return g;
}
bool workgcd(){
multiset<ll> :: iterator it;
for(int i = 1;i <= n; i++){
ll sx,at,y;
it = ( ((*atk.begin()) > a[i]) ? (atk.begin()) : (--atk.upper_bound(a[i])) );
at = *it;
atk.erase(it);
atk.insert((ll)gai[i]);
ll g = exgcd(at,p[i],sx,y);
if(a[i] % g != 0) return false;
while(sx < 0) sx += p[i] / g;
m[i] = p[i]/g;
s[i] = sx * (a[i]/g) % m[i];
}
return true;
}
void get_in(){
scanf("%d%d",&n,&mm);
for(int i = 1;i <= n; i++)
scanf("%lld",&a[i]);
for(int i = 1;i <= n; i++)
scanf("%lld",&p[i]);
for(int i = 1;i <= n; i++){
if(p[i] < a[i]) flag = 1;
}
for(int i = 1;i <= n; i++)
scanf("%lld",&gai[i]);
for(int i = 1;i <= mm; i++){
ll at;
scanf("%lld",&at);
atk.insert(at);
}
return;
}
ll mul(ll a,ll b,ll mod){
ll base = 0;
while(b){
if(b & 1) base += a,base %=mod;
a <<= 1;
a %= mod;
b >>= 1;
}
return base;
}
ll ans,M;
bool workcrt(){
ll x,y;
ans = s[1],M = m[1];
for(int i = 2;i <= n; i++){
ll c = (s[i] - ans%m[i] + m[i]) %m[i];
ll g = exgcd(M,m[i],x,y);
ll mo = m[i]/g;
if(c % g != 0) return false;
x = mul(x,c/g,mo);//cout<<i<<endl;
ans += x * M;
M *= mo;
ans = (ans % M + M) % M;
}
return true;
}
void clear(){
for(int i = 1;i <= n; i++) {
s[i] = a[i] = p[i] = m[i] = 0;
}
atk.clear();
flag = 0;
return;
}
void solve(){
ans = 0;
multiset<ll> :: iterator it;
for(int i = 1;i <= n; i++){
ll sx,at,y;
it = ( ((*atk.begin()) > a[i]) ? (atk.begin()) : (--atk.upper_bound(a[i])) );
at = *it;
atk.erase(it);
atk.insert((ll)gai[i]);
if(a[i] % at == 0) ans = max(ans,a[i] / at);
else ans = max(ans,a[i] / at + 1);
}
return ;
}
int main(){
//freopen("1.in","r",stdin);
scanf("%d",&t);
while(t--){
get_in();
if(flag){
solve();
printf("%lld\n",ans);
clear();
continue;
}
if(!workgcd()||!workcrt()){
printf("-1\n");
clear();
continue;
}
printf("%lld\n",ans);
clear();
}
return 0;
}