显然multiset求出每次用哪把剑。注意到除了p=1的情况,其他数据都保证了ai<pi,于是先特判一下p=1。比较坑的是还可能存在ai=pi,稍微考虑一下。
剩下的部分即解bix≡ai(mod pi)方程组。没有保证模数互质,于是excrt一发。excrt实际上就是不停exgcd合并两个方程。
这次是重开这题,调了半天还是一堆-1觉得这个题可能是搞不会了,最后才发现某个地方没开long long。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<set>
#include<cassert>
using namespace std;
#define ll long long
#define N 100010
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<''||c>'')) c=getchar();return c;}
ll gcd(ll n,ll m){return m==?n:gcd(m,n%m);}
ll lcm(ll n,ll m){return n*(m/gcd(n,m));}
ll read()
{
ll x=,f=;char c=getchar();
while (c<''||c>'') {if (c=='-') f=-;c=getchar();}
while (c>=''&&c<='') x=(x<<)+(x<<)+(c^),c=getchar();
return x*f;
}
int T,n,m;
ll b[N],p[N],a[N],rwd[N];
multiset<ll> q;
ll ksc(ll a,ll b,ll p)
{
ll t=a*b-(ll)((long double)a*b/p+0.5)*p;
return t<?t+p:t;
}
void exgcd(ll a,ll b,ll &x,ll &y)
{
if (b==)
{
x=,y=;
return;
}
exgcd(b,a%b,x,y);
ll t=x;x=y;y=t-a/b*x;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("bzoj5418.in","r",stdin);
freopen("bzoj5418.out","w",stdout);
const char LL[]="%I64d\n";
#else
const char LL[]="%lld\n";
#endif
T=read();
while (T--)
{
n=read(),m=read();
for (int i=;i<=n;i++) a[i]=read();
for (int i=;i<=n;i++) p[i]=read();
for (int i=;i<=n;i++) rwd[i]=read();
q.clear();
for (int i=;i<=m;i++) q.insert(read());
for (int i=;i<=n;i++)
{
multiset<ll>::iterator it=q.upper_bound(a[i]);
if (it!=q.begin()) it--;
b[i]=*it;q.erase(it);q.insert(rwd[i]);
}
bool issp=;
for (int i=;i<=n;i++) if (p[i]!=) {issp=;break;}
ll ans=;
if (issp) for (int i=;i<=n;i++) ans=max(ans,(a[i]-)/b[i]+);
else
{
issp=;
for (int i=;i<=n;i++) if (a[i]!=p[i]) {issp=;break;}
if (issp)
{
ans=;
for (int i=;i<=n;i++)
if (b[i]%p[i]) b[i]%=p[i],ans=lcm(ans,p[i]/gcd(b[i],p[i]));
}
else
{
for (int i=;i<=n;i++)
if (b[i]%p[i]==&&a[i]!=b[i]||a[i]%gcd(b[i],p[i])) {ans=-;break;}
else
{
b[i]%=p[i];
int x=gcd(b[i],p[i]);
a[i]/=x,b[i]/=x,p[i]/=x;
}
if (~ans)
{
ll tmp;exgcd(b[],p[],ans,tmp);ans=(ans%p[]+p[])%p[];ans=ksc(ans,a[],p[]);
for (int i=;i<=n;i++)
{
ll A=ksc(b[i],p[i-],p[i]),B=(a[i]-ksc(b[i],ans,p[i])+p[i])%p[i];
ll x=gcd(p[i],p[i-]);if (B%x) {ans=-;break;}
A/=x,B/=x,p[i]/=x;
ll k;exgcd(A,p[i],k,tmp);k=(k%p[i]+p[i])%p[i];k=ksc(k,B,p[i]);
p[i]*=p[i-];ans=(ksc(k,p[i-],p[i])+ans)%p[i];
}
}
}
}
printf(LL,ans);
}
return ;
}