提交记录 4354


用户 题目 状态 得分 用时 内存 语言 代码长度
applese noi18d. 【NOI2018】屠龙勇士 Accepted 100 503.282 ms 7848 KB C++11 2.44 KB
提交时间 评测时间
2018-07-20 14:16:15 2020-07-31 22:54:22
#pragma GCC optimize(3)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
bool Finish_read;
template<class T>inline void read(T &x){Finish_read=0;x=0;int f=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-')f=-1;if(ch==EOF)return;ch=getchar();}while(isdigit(ch))x=x*10+ch-'0',ch=getchar();x*=f;Finish_read=1;}
template<class T>inline void print(T x){if(x/10!=0)print(x/10);putchar(x%10+'0');}
template<class T>inline void writeln(T x){if(x<0)putchar('-');x=abs(x);print(x);putchar('\n');}
template<class T>inline void write(T x){if(x<0)putchar('-');x=abs(x);print(x);}
/*================Header Template==============*/
const int maxn=100005;
multiset<ll>sword;
ll T,n,m,a[maxn],p[maxn],d[maxn],u[maxn];
inline ll gcd(ll a,ll b) {
	return b?gcd(b,a%b):a;
}
inline void exgcd(ll a,ll b,ll &d,ll &x,ll &y) {
	if(!b)
		d=a,x=1,y=0;
	else {
		exgcd(b,a%b,d,y,x);
		y-=x*(a/b);
	}
}
inline ll inv(ll v,ll p) {
	ll d,x,y;
	exgcd(v,p,d,x,y);
	return d==1?(x+p)%p:-1;
}
inline ll Mul(ll a,ll b,ll p) {
	ll res=0;
	while(b) {
		if(b&1) {
			res=res+a;
			if(res>=p)
				res-=p;
		}
		a=a+a;
		if(a>=p)
			a-=p;
		b>>=1;
	}
	return res;
}
inline bool work(ll a1,ll p1,ll a2,ll p2,ll &a3,ll &p3) {
	ll g=gcd(p1,p2),c=a2-a1;
	if(c%g)
		return 0;
	c=(c%p2+p2)%p2,c/=g,p1/=g,p2/=g,c=Mul(c,inv(p1,p2),p2),p3=p1*p2*g,a3=((Mul(c,p1*g,p3)+a1)%p3+p3)%p3;
	return 1;
}
inline ll solve() {
	ll a1=a[1],p1=p[1],a2,p2;
	for(int i=2;i<=n;++i) {
		ll a3,p3;
		a2=a[i],p2=p[i];
		if(!work(a1,p1,a2,p2,a3,p3))
			return -1;
		a1=a3,p1=p3;
	}
	return (a1%p1+p1)%p1;
}
int main() {
	read(T);
	while(T--) {
		sword.clear();
		read(n),read(m);
		int flg=1;
		for(int i=1;i<=n;++i)
			read(a[i]);
		for(int i=1;i<=n;++i)
			read(p[i]),flg&=(p[i]==1);
		for(int i=1;i<=n;++i)
			read(d[i]);
		for(int i=1,x;i<=m;++i)
			read(x),sword.insert(x);
		bool noans=0;
		for(int i=1;i<=n;++i) {
			multiset<ll>::iterator it=sword.upper_bound(a[i]);
			if(it==sword.begin())
				u[i]=*it,sword.erase(it);
			else
				--it,u[i]=*it,sword.erase(it);
			if(!u[i]) {
				puts("-1");
				noans=1;
				break;
			} 
			sword.insert(d[i]);
			ll g=gcd(p[i],u[i]);
			if(a[i]%g!=0) {
				puts("-1");
				noans=1;
				break;
			}
			a[i]/=g,u[i]/=g,p[i]/=g;
		}
		if(noans)
			continue;
		if(flg) {
			ll mx=0;
			for(int i=1;i<=n;++i)
				mx=max(mx,a[i]/u[i]+(a[i]%u[i]!=0));
			printf("%lld\n",mx);
			continue;
		}
		for(int i=1;i<=n;++i) {
			ll v=inv(u[i],p[i]);
			a[i]=Mul(a[i],v,p[i]);
		}
		printf("%lld\n",solve());
	}
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #137.914 ms3 MB + 88 KBAcceptedScore: 5

Testcase #237.814 ms3 MB + 88 KBAcceptedScore: 5

Testcase #346.81 ms3 MB + 88 KBAcceptedScore: 5

Testcase #446.699 ms3 MB + 88 KBAcceptedScore: 5

Testcase #52.355 ms132 KBAcceptedScore: 5

Testcase #62.281 ms132 KBAcceptedScore: 5

Testcase #72.293 ms132 KBAcceptedScore: 5

Testcase #841.92 us52 KBAcceptedScore: 5

Testcase #942.03 us52 KBAcceptedScore: 5

Testcase #1039.14 us52 KBAcceptedScore: 5

Testcase #1139.99 us52 KBAcceptedScore: 5

Testcase #1239.47 us52 KBAcceptedScore: 5

Testcase #1339.04 us52 KBAcceptedScore: 5

Testcase #14256.685 ms7 MB + 680 KBAcceptedScore: 5

Testcase #15257.34 ms7 MB + 680 KBAcceptedScore: 5

Testcase #16502.069 ms7 MB + 680 KBAcceptedScore: 5

Testcase #17503.282 ms7 MB + 680 KBAcceptedScore: 5

Testcase #18496.624 ms7 MB + 680 KBAcceptedScore: 5

Testcase #19495.444 ms7 MB + 680 KBAcceptedScore: 5

Testcase #20494.201 ms7 MB + 680 KBAcceptedScore: 5


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