提交记录 4358


用户 题目 状态 得分 用时 内存 语言 代码长度
zhouzhendong noi18d. 【NOI2018】屠龙勇士 Accepted 100 602.715 ms 8620 KB C++ 1.86 KB
提交时间 评测时间
2018-07-20 15:34:14 2020-07-31 22:54:40
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <vector>
#include <set>
#include <queue>
using namespace std;
typedef long long LL;
bool isd(char ch){
	return '0'<=ch&&ch<='9';
}
LL read(){
	LL x=0;
	char ch=getchar();
	while (!isd(ch))
		ch=getchar();
	while (isd(ch))
		x=(x<<1)+(x<<3)+ch-48,ch=getchar();
	return x;
}
const int N=100005;
int T,n,m;
LL a[N],p[N],h[N],v[N],x[N];
multiset <LL> S;
LL ex_gcd(LL a,LL b,LL &x,LL &y){
	if (!b){
		x=1,y=0;
		return a;
	}
	LL res=ex_gcd(b,a%b,y,x);
	y-=x*(a/b);
	return res;
}
LL gcd(LL a,LL b){
	return b?gcd(b,a%b):a;
}
LL inv(LL v,LL p){
	LL x,y,g=ex_gcd(v,p,x,y);
	if (g>1)
		return -1;
	return (x+p)%p;
}
LL Mul(LL a,LL b,LL p){
	a=(a%p+p)%p;
	b=(b%p+p)%p;
	LL ans=0;
	for (;a;a>>=1,b=(b<<1)%p)
		if (a&1LL)
			ans=(ans+b)%p;
	return ans;
}
bool CRT(LL w1,LL p1,LL w2,LL p2,LL &w,LL &p){
	LL x,y,z=w2-w1,g=ex_gcd(p1,p2,x,y);
	if (z%g)
		return 0;
	LL t=z/g;
	x=Mul(x,t,p2/g);
	p=p1/g*p2;
	w=((w1+Mul(x,p1,p))%p+p)%p;
	return 1;
}
LL Solve(){
	n=read(),m=read();
	for (int i=1;i<=n;i++)
		a[i]=read();
	for (int i=1;i<=n;i++)
		p[i]=read();
	for (int i=1;i<=n;i++)
		h[i]=read();
	S.clear();
	while (m--)
		S.insert(read());
	for (int i=1;i<=n;i++){
		multiset <LL> :: iterator p=S.begin();
		if ((*p)<a[i])
			p=--S.upper_bound(a[i]);
		v[i]=*p,S.erase(p);
		S.insert(h[i]);
	}
	for (int i=1;i<=n;i++){
		LL g=gcd(a[i],gcd(v[i],p[i]));
		v[i]/=g,p[i]/=g,a[i]/=g;
		LL Inv=inv(v[i],p[i]);
		if (Inv<0)
			return -1LL;
		x[i]=Mul(a[i],Inv,p[i]);
	}
	LL W=x[1],P=p[1];
	for (int i=2;i<=n;i++)
		if (!CRT(W,P,x[i],p[i],W,P))
			return -1LL;
	// x = W  (mod P)
	for (int i=1;i<=n;i++){
		LL val=(a[i]+v[i]-1)/v[i];
		if (val<=W)
			continue;
		LL k=(val-W+P-1)/P;
		W+=k*P;
	}
	return W;
}
int main(){
	scanf("%d",&T);
	while (T--)
		printf("%lld\n",Solve());
	fclose(stdin);fclose(stdout);
	return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #1109.781 ms3 MB + 860 KBAcceptedScore: 5

Testcase #2109.515 ms3 MB + 860 KBAcceptedScore: 5

Testcase #3112.703 ms3 MB + 860 KBAcceptedScore: 5

Testcase #4112.476 ms3 MB + 860 KBAcceptedScore: 5

Testcase #52.95 ms136 KBAcceptedScore: 5

Testcase #62.88 ms136 KBAcceptedScore: 5

Testcase #72.932 ms136 KBAcceptedScore: 5

Testcase #821.26 us48 KBAcceptedScore: 5

Testcase #920.38 us48 KBAcceptedScore: 5

Testcase #1019.84 us48 KBAcceptedScore: 5

Testcase #1119.41 us48 KBAcceptedScore: 5

Testcase #1219.91 us48 KBAcceptedScore: 5

Testcase #1320.17 us48 KBAcceptedScore: 5

Testcase #14328.451 ms8 MB + 428 KBAcceptedScore: 5

Testcase #15329.003 ms8 MB + 428 KBAcceptedScore: 5

Testcase #16602.715 ms8 MB + 428 KBAcceptedScore: 5

Testcase #17602.082 ms8 MB + 428 KBAcceptedScore: 5

Testcase #18595.65 ms8 MB + 428 KBAcceptedScore: 5

Testcase #19590.518 ms8 MB + 428 KBAcceptedScore: 5

Testcase #20571.128 ms8 MB + 428 KBAcceptedScore: 5


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