提交记录 4454


用户 题目 状态 得分 用时 内存 语言 代码长度
SovietPower noi18d. 【NOI2018】屠龙勇士 Accepted 100 393.115 ms 9204 KB C++ 2.58 KB
提交时间 评测时间
2018-07-23 16:22:03 2020-07-31 23:02:41
#include <set>
#include <cstdio>
#include <cctype>
#include <algorithm>
//#define gc() getchar()
#define MAXIN 1000000
#define Failed {puts("-1"); return;}
#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
#define set_It std::multiset<LL>::iterator
typedef long long LL;
const int N=1e5+7;

int n,m,rew[N]/*reward*/;
LL hp[N],p[N],a[N],md[N];//Ans = a[i] (mod md[i])
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)) Failed;
		ans=std::max(ans,(int)((hp[i]+(*it)-1)/(*it)));//(int)ceil(1.0*hp[i]/(*it)));
		st.erase(it), st.insert(rew[i]);
	}
	printf("%d\n",ans);
}
//void Spec2()
//{
//	int ans=1; set_It it;
//	for(int i=1,need; i<=n; ++i)
//	{
//		it=st.upper_bound(hp[i]);
//		if(it!=st.begin()) --it;
//		if(!(*it)) Failed;
//		need=(hp[i]+(*it)-1)/(*it), ans=ans/Gcd(ans,need)*need;
//		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;}
//	f1=1;//然而数据没有这种情况...还是要有的 
//	for(int i=1; i<=n; ++i) if(p[i]!=hp[i]) {f1=0; break;}
//	if(f1) {Spec2(); 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)) Failed;

		int atk=*it;
		LL gcd,x0,y0,P;
		Exgcd(atk,p[i],gcd,x0,y0);
		if(hp[i]%gcd) Failed;
		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) Failed;
		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)/*直接x*M会炸?...*/, 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.016 ms2 MB + 908 KBAcceptedScore: 5

Testcase #218.869 ms2 MB + 908 KBAcceptedScore: 5

Testcase #321.326 ms2 MB + 908 KBAcceptedScore: 5

Testcase #421.078 ms2 MB + 908 KBAcceptedScore: 5

Testcase #51.827 ms200 KBAcceptedScore: 5

Testcase #61.782 ms200 KBAcceptedScore: 5

Testcase #71.73 ms200 KBAcceptedScore: 5

Testcase #815.77 us48 KBAcceptedScore: 5

Testcase #915.87 us48 KBAcceptedScore: 5

Testcase #1014.91 us48 KBAcceptedScore: 5

Testcase #1115.27 us48 KBAcceptedScore: 5

Testcase #1215.04 us48 KBAcceptedScore: 5

Testcase #1315.77 us48 KBAcceptedScore: 5

Testcase #14224.592 ms7 MB + 476 KBAcceptedScore: 5

Testcase #15225.244 ms7 MB + 476 KBAcceptedScore: 5

Testcase #16393.115 ms8 MB + 1012 KBAcceptedScore: 5

Testcase #17392.837 ms8 MB + 1012 KBAcceptedScore: 5

Testcase #18386.027 ms8 MB + 1012 KBAcceptedScore: 5

Testcase #19388.307 ms8 MB + 1012 KBAcceptedScore: 5

Testcase #20385.017 ms8 MB + 1012 KBAcceptedScore: 5


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