样例没过,但AC了?
查看原帖
样例没过,但AC了?
1124323
A_small_WA楼主2024/12/29 09:07

rt,样例第二组输出 27,但是 AC

代码如下,求大佬帮看看。

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define LL __int128
const int N=100020;
int at[N],pr[N],m[N];
LL r[N],ax,ay,p[N],a[N];
void exgcd(LL x,LL y){
	if(y==0){
		ax=1,ay=0;
		return;
	}
	LL z=x%y,w=x/y;
	exgcd(y,z);
	LL c=ax;
	ax=ay,ay=c-w*ay;
}
multiset <LL> ms;
multiset <LL>::iterator it;
signed main(){
	int t;
	cin>>t;
	while(t--){
		int n,m;
		LL ans=0;
		bool bj1=0,bj2=0;
		cin>>n>>m;
		for(int i=1;i<=n;i++){
			int tmp;
			scanf("%lld",&tmp);
			a[i]=tmp;
		}
		for(int i=1;i<=n;i++){
			int tmp;
			scanf("%lld",&tmp);
			p[i]=tmp;
			if(p[i]<a[i]) bj1=1;
		}
		for(int i=1;i<=n;i++) scanf("%lld",&pr[i]);
		for(int i=1;i<=m;i++){
			scanf("%lld",&at[i]);
			ms.insert(at[i]);
		}
		for(int i=1;i<=n;i++){
			it=ms.upper_bound(a[i]);
			if(it!=ms.begin()) it--;
			LL atk=*it;
			ans=max(ans,(a[i]+atk-1)/atk);
			LL G=__gcd(atk,p[i]);
			atk=atk/G,p[i]=p[i]/G;
			if(a[i]%G!=0){
				cout<<-1<<'\n';
				bj2=1;
				break;
			}
			a[i]=a[i]/G;
			exgcd(atk,p[i]);
			r[i]=ax*a[i];
			ms.erase(it);
			ms.insert(pr[i]);
		}
		int tmp=ans;
		if(bj2){
			ms.clear();
			continue;
		}
		if(bj1) cout<<tmp<<"\n";
		else{
			LL m1=p[1],r1=r[1];
			for(int i=2;i<=n;i++){
				LL G=__gcd(m1,p[i]),m2=p[i]/G;
				exgcd(m1/G,m2);
				LL P=ax*(r[i]-r1)/G,m3=(m1*p[i])/G;
				P=(P%m2+m2)%m2;
				r1=m1*P+r1;
				m1=m3;
			}
			r1=r1%m1;
			int tmp1=r1;
			while(tmp1<ans) tmp1+=m1;
			cout<<tmp1<<"\n";
		}
		ms.clear();
	}
	return 0;
}
2024/12/29 09:07
加载中...