提交记录 4436


用户 题目 状态 得分 用时 内存 语言 代码长度
Simpson561 noi18d. 【NOI2018】屠龙勇士 Compile Error 0 0 ns 0 KB C 2.19 KB
提交时间 评测时间
2018-07-23 09:43:42 2020-07-31 23:00:51
#include <set>
#include <cstdio>
#include <cctype>
#include <algorithm>
#define MAXIN 1000000
#define Failed return (void)puts("-1")
#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
#define set_It std::multiset<LL>::iterator
#define LL long long
const int N=1e5+7;

int n,m,rew[100009];
LL hp[100007],p[100007],a[100007],md[100007];
std::multiset<LL> st;
char IN[MAXIN],*SS=IN,*TT=IN;

inline int read() {
	int now=0;
	register char c=gc();
	for(; !isdigit(c); c=gc());
	for(; isdigit(c); now=now*10+c-'0',c=gc());
	return now;
}
inline LL readll() {
	LL now=0;
	register char c=gc();
	for(; !isdigit(c); c=gc());
	for(; isdigit(c); now=now*10+c-'0',c=gc());
	return now;
}
inline LL Mult(LL a,LL b,LL p) {
	LL tmp=a*b-(LL)((long double)a/p*b+1e-8)*p;
	return tmp<0?tmp+p:tmp;
}
LL Gcd(LL a,LL b) {
	return b?Gcd(b,a%b):a;
}
void Exgcd(LL a,LL b,LL &g,LL &x,LL &y) {
	if(!b) g=a,x=1,y=0;
	else Exgcd(b,a%b,g,y,x),y-=a/b*x;
}
void Spec1() {
	int ans=0;
	set_It it;
	for(int i=1; i<=n; ++i) {
		it=st.upper_bound(hp[i]);
		if(it!=st.begin()) --it;
		if(!(*it)) return (void)puts("-1");
		ans=std::max(ans,(int)((hp[i]+(*it)-1)/(*it)));
		st.erase(it), st.insert(rew[i]);
	}
	printf("%d\n",ans);
}
void Solve() {
	bool f1=1;
	for(int i=1; i<=n; ++i) if(p[i]!=1) {
			f1=0;
			break;
		}
	if(f1) {
		Spec1();
		return;
	}
	set_It it;
	int cnt=0;
	for(int i=1; i<=n; ++i) {
		it=st.upper_bound(hp[i]);
		if(it!=st.begin()) --it;
		if(hp[i]&&!(*it)) return (void)puts("-1");

		int atk=*it;
		LL gcd,x0,y0,P;
		Exgcd(atk,p[i],gcd,x0,y0);
		if(hp[i]%gcd) return (void)puts("-1");
		P=p[i]/gcd, x0=(x0%P+P)%P;
		a[++cnt]=Mult(x0,hp[i]/gcd,P), md[cnt]=P;
		st.erase(it), st.insert(rew[i]);
	}

	LL A=a[1],M=md[1],g,x,y,t,Mod;
	for(int i=2; i<=cnt; ++i) {
		Exgcd(M,md[i],g,x,y);
		if((a[i]-A)%g) return (void)puts("-1");
		t=md[i]/g, x=Mult(x,(a[i]-A)/g,t), x=(x%t+t)%t;
		Mod=M*t, A+=Mult(x,M,Mod), A%=Mod, M=Mod;
	}
	printf("%lld\n",A);
}

int main() {
	for(int Case=read(); Case--; ) {
		st.clear();
		n=read(), m=read();
		for(int i=1; i<=n; ++i) hp[i]=readll();
		for(int i=1; i<=n; ++i) p[i]=readll();
		for(int i=1; i<=n; ++i) rew[i]=read();
		for(int i=1; i<=m; ++i) st.insert(read());
		Solve();
	}
	return 0;
}

CompilationN/AN/ACompile ErrorScore: N/A


Judge Duck Online | 评测鸭在线
Server Time: 2026-04-13 14:28:10 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠