提交记录 6013


用户 题目 状态 得分 用时 内存 语言 代码长度
daklqw noi18d. 【NOI2018】屠龙勇士 Accepted 100 315.466 ms 22588 KB C++ 2.83 KB
提交时间 评测时间
2018-09-20 08:48:38 2020-08-01 00:37:50
#include <cstdio>
#include <algorithm>
#include <map>
#include <cctype>
using namespace std;
#define MAXN 100010
#define int long long
map<int, int> sws;
int a[MAXN], p[MAXN], d[MAXN], sw[MAXN], n, m, t, li[MAXN], rk[MAXN], rk2[MAXN], bak, p2[MAXN], li2[MAXN];
int gcd(int a, int b) {return b ? gcd(b, a % b): a;}
int lcm(int a, int b) {a /= gcd(a, b); return a * b;}
inline int mul(int a, int b, int p) {
	int res = 0;
	while (b) {
		if (b & 1) (res += a) %= p;
		(a <<= 1) %= p;
		b >>= 1;
	}
	return res;
}
void exgcd(int a, int b, int & x, int & y) {
	if (b) {exgcd(b, a % b, y, x); y -= a / b * x;}
	else {x = 1; y = 0;}
}
int inv(int a, int p) {int x, y; exgcd(a, p, x, y); return (x + p) % p;}
inline bool cmp(int a, int b) {
	return p[a] < p[b];
}
#define N 100000000
char bufin[N], *cur;
inline int getint() {
    register char ch;
    while(!isdigit(ch=*cur++));
    register int x=ch^'0';
    while(isdigit(ch=*cur++)) x=(((x<<2)+x)<<1)+(ch^'0');
    return x;
}
signed main() {
	fread(bufin, 1, N, stdin);
	cur = bufin;
	int T; T = getint();
	while (T--) {
		sws.clear(); bak = 0;
		n = getint(); m = getint();
		bool all1 = true;
		for (int i = 1; i <= n; ++i) a[i] = getint();
		for (int i = 1; i <= n; ++i) all1 &= (p[i] = getint()) == 1;
		for (int i = 1; i <= n; ++i) d[i] = getint();
		for (int i = 1; i <= m; ++i) ++sws[getint()];
		map<int, int>::iterator it;
		int mincnt = 0, LCM = 1;
		for (int i = 1; i <= n; ++i) {
			if (sws.begin() -> first > a[i]) it = sws.begin();
			else it = sws.upper_bound(a[i]), --it;
			sw[i] = it -> first;
			if (it -> second == 1) sws.erase(it);
			else --it->second;
			++sws[d[i]];
			mincnt = max(mincnt, a[i] / sw[i] + (a[i] % sw[i] != 0));
			int G = gcd (p[i], sw[i]), I = inv(sw[i] / G, p[i] / G);
			if (a[i] % G != 0) {
				puts("-1");
				goto finallive;
			} else a[i] /= G;
			p[i] /= G; a[i] = mul(a[i], I, p[i]);;	
			li[i] = a[i] % p[i];
			LCM = lcm(LCM, p[i]);
			rk[i] = i;
		}
		if (all1) printf("%d\n", mincnt);
		else if (n == 1) printf("%d\n", mincnt + (li[1] - mincnt % p[1] + p[1]) % p[1]);
		else {
			int fff = 0;
			sort(rk + 1, rk + 1 + n, cmp);
			rk2[++bak] = rk[1];
			for (int i = 2; i <= n; ++i)
				if (p[rk[i]] == p[rk[i - 1]]) {
					if (li[rk[i]] != li[rk[i - 1]]) {
						puts("-1");
						goto finallive;
					}
				} else rk2[++bak] = rk[i];
			for (int i = bak; i; --i)
				p2[bak - i + 1] = p[rk2[i]] , li2[bak - i + 1] = li[rk2[i]];
			register int L = p2[1], last = 2;
			for (register int i = mincnt + (li2[1] - mincnt % p2[1] + p2[1]) % p2[1], j, flag; i <= LCM << 2; i += L) {
				flag = true;
				if (++fff > 20000000) break;
				for (j = last; j <= bak; ++j)
					if (i % p2[j] != li2[j]) {
						flag = false;
						break;
					} else L = lcm(L, p2[last++]);
				if (flag) {
					printf("%lld\n", i);
					goto finallive;
				}
			}
            puts("-1");
		}
		finallive:;
	}
	return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #154.864 ms8 MB + 820 KBAcceptedScore: 5

Testcase #254.692 ms8 MB + 840 KBAcceptedScore: 5

Testcase #360.338 ms10 MB + 652 KBAcceptedScore: 5

Testcase #459.845 ms10 MB + 676 KBAcceptedScore: 5

Testcase #52.203 ms204 KBAcceptedScore: 5

Testcase #62.068 ms196 KBAcceptedScore: 5

Testcase #72.116 ms196 KBAcceptedScore: 5

Testcase #820.24 us56 KBAcceptedScore: 5

Testcase #919.53 us56 KBAcceptedScore: 5

Testcase #1018.58 us56 KBAcceptedScore: 5

Testcase #1118.7 us56 KBAcceptedScore: 5

Testcase #1218.84 us56 KBAcceptedScore: 5

Testcase #1319.8 us56 KBAcceptedScore: 5

Testcase #14278.206 ms22 MB + 60 KBAcceptedScore: 5

Testcase #15277.508 ms22 MB + 36 KBAcceptedScore: 5

Testcase #16301.112 ms13 MB + 580 KBAcceptedScore: 5

Testcase #17301.126 ms13 MB + 576 KBAcceptedScore: 5

Testcase #18315.466 ms14 MB + 1008 KBAcceptedScore: 5

Testcase #19314.797 ms14 MB + 940 KBAcceptedScore: 5

Testcase #20312.658 ms14 MB + 844 KBAcceptedScore: 5


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