QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#300820#5045. KingOutlierTL 0ms3996kbC++141.1kb2024-01-08 21:00:402024-01-08 21:00:40

Judging History

你现在查看的是最新测评结果

  • [2024-01-08 21:00:40]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3996kb
  • [2024-01-08 21:00:40]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PII;
const int N = 2e5 + 10;

ll n, p, b[N];
ll ksm(ll a, ll b) {
	ll res = 1;
	while (b) {
		if (b & 1) res = res * a % p;
		a = a * a % p;
		b >>= 1;
	}
	return res;
}
unordered_map<ll, ll> mp;
void solve() {
	scanf("%lld%lld", &n, &p);
	for (int i = 1; i <= n; i++) scanf("%lld", &b[i]);
	for (int i = 1; i < n; i++) {
		ll q = b[i + 1] * ksm(b[i], p - 2) % p;
		mp[q]++;
		if (i < n - 1) q = b[i + 2] * ksm(b[i], p - 2) % p, mp[q]++;
	}
	vector<ll> v;
	for (auto it = mp.begin(); it != mp.end(); it++) {
		if (it->second >= n / 4) v.push_back(it->first);
	}
	ll ans = -1;
	for (int i = 0; i < v.size(); i++) {
		ll q = v[i], inv = ksm(q, p - 2);
		unordered_map<ll, ll> mmp;
		ll tmp = 0;
		for (int j = 1; j <= n; j++) {
			ll t = mmp[b[j]];
			t = max(t, mmp[b[j] * inv % p] + 1);
			mmp[b[j]] = t;
			tmp = max(tmp, t);
		}
		if (tmp >= (n + 1) / 2) ans = max(ans, tmp);
	}
	printf("%lld\n", ans);
}
int main() {
	int _ = 1;
	cin >> _;
	while (_--) solve();
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3996kb

input:

4
6 1000000007
1 1 2 4 8 16
6 1000000007
597337906 816043578 617563954 668607211 89163513 464203601
5 1000000007
2 4 5 6 8
5 1000000007
2 4 5 6 7

output:

5
-1
3
-1

result:

ok 4 number(s): "5 -1 3 -1"

Test #2:

score: -100
Time Limit Exceeded

input:

1000
200 495189361
193302375 262009153 248101278 250233641 303504256 426913173 23261177 206011896 214770731 286184509 492688635 207979481 282629026 450810670 41818047 359796006 445343921 241742611 249404909 41291916 392252331 125287519 92825425 162555413 371172157 420486666 270651384 309213995 11709...

output:


result: