这题到底是什么东西……数学题太珂怕了……
1 //minamoto 2 #include3 #include 4 #include 5 #include 6 #define ll long long 7 #define GG {puts("-1");return;} 8 using namespace std; 9 const int N=1e5+5;10 #define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)11 char buf[1<<21],*p1=buf,*p2=buf;12 template inline bool cmax(T&a,const T&b){ return a '9'||ch<'0')17 (ch=='-')&&(flag=true);18 for(res=num;(ch=getc())<='9'&&ch>='0';res=res*10+num);19 (flag)&&(res=-res);20 #undef num21 return res;22 }23 multiset s;int n,m;ll mod[N],xs[N],cs[N],st[N],gif[N];int T;24 inline void exgcd(ll a,ll &x,ll b,ll &y){25 if(!b) return (void)(x=1,y=0);26 exgcd(b,x,a%b,y);ll t=x;x=y,y=t-(a/b)*y;27 }28 inline ll gcd(ll a,ll b){29 if(a 0?b:b+mod;35 for(;b;b>>=1,a=(a+a)%mod) (res+=a*(b&1))%=mod;36 return res;37 }38 inline void spj(){39 ll res=0;40 for(int i=1;i<=n;++i) cmax(res,(cs[i]+xs[i]-1)/xs[i]);41 printf("%lld\n",res);42 }43 inline void spj2(){44 ll lc=1;45 for(int i=1;i<=n;++i){46 ll ds=mod[i]/gcd(mod[i],xs[i]);47 lc=lc/gcd(lc,ds)*ds;48 }49 printf("%lld\n",lc);50 }51 void solve(){52 n=read(),m=read();53 for(int i=1;i<=n;++i) cs[i]=read();54 for(int i=1;i<=n;++i) mod[i]=read();55 for(int i=1;i<=n;++i) gif[i]=read();56 for(int i=1;i<=m;++i) st[i]=read();57 for(int i=1;i<=m;++i) s.insert(st[i]);58 multiset ::iterator ii;59 for(int i=1;i<=n;++i){60 ii=(cs[i]<(*s.begin()))?s.begin():(--s.upper_bound(cs[i]));61 xs[i]=*ii,s.erase(ii),s.insert(gif[i]);62 }63 for(int i=1;i<=m;++i)64 if(mod[i]==cs[i]){spj2();return;}65 for(int i=1;i<=n;++i)66 if(mod[i]==1){spj();return;}67 for(int i=1;i<=n;++i) xs[i]%=mod[i];68 for(int i=1;i<=n;++i)69 if(xs[i]==0){70 if(mod[i]==cs[i]) xs[i]=1,mod[i]=1,cs[i]=0;71 else GG;72 }73 for(int i=1;i<=n;++i){74 ll sx,sy,g=gcd(xs[i],mod[i]);75 if(cs[i]%g!=0) GG;76 exgcd(xs[i],sx,mod[i],sy);77 mod[i]=mod[i]/g;sx=(sx%mod[i]+mod[i])%mod[i],cs[i]=mul(sx,cs[i]/g,mod[i]);78 }79 for(int i=1;i