提交记录 4437


用户 题目 状态 得分 用时 内存 语言 代码长度
Simpson561 noi18d. 【NOI2018】屠龙勇士 Accepted 100 393.073 ms 9204 KB C++11 2.19 KB
提交时间 评测时间
2018-07-23 09:44:16 2020-07-31 23:01:55
#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 OKScore: N/A

Testcase #119.17 ms2 MB + 908 KBAcceptedScore: 5

Testcase #218.957 ms2 MB + 908 KBAcceptedScore: 5

Testcase #321.26 ms2 MB + 908 KBAcceptedScore: 5

Testcase #421.171 ms2 MB + 908 KBAcceptedScore: 5

Testcase #51.834 ms200 KBAcceptedScore: 5

Testcase #61.785 ms200 KBAcceptedScore: 5

Testcase #71.728 ms200 KBAcceptedScore: 5

Testcase #816.2 us48 KBAcceptedScore: 5

Testcase #916.68 us48 KBAcceptedScore: 5

Testcase #1015.17 us48 KBAcceptedScore: 5

Testcase #1114.94 us48 KBAcceptedScore: 5

Testcase #1215.32 us48 KBAcceptedScore: 5

Testcase #1316.69 us48 KBAcceptedScore: 5

Testcase #14225.228 ms7 MB + 476 KBAcceptedScore: 5

Testcase #15225.24 ms7 MB + 476 KBAcceptedScore: 5

Testcase #16393.073 ms8 MB + 1012 KBAcceptedScore: 5

Testcase #17392.338 ms8 MB + 1012 KBAcceptedScore: 5

Testcase #18386.503 ms8 MB + 1012 KBAcceptedScore: 5

Testcase #19387.797 ms8 MB + 1012 KBAcceptedScore: 5

Testcase #20385.521 ms8 MB + 1012 KBAcceptedScore: 5


Judge Duck Online | 评测鸭在线
Server Time: 2024-05-05 17:08:11 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用