QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#31935#249. Miller Rabin 算法whereismyteammate#TL 0ms0kbC++20961b2022-05-13 23:21:052022-05-13 23:21:08

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-05-13 23:21:08]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2022-05-13 23:21:05]
  • 提交

answer

#include <bits/stdc++.h>

std::mt19937 s(std::chrono::steady_clock::now().time_since_epoch().count());
int64_t mul(int64_t x, int64_t y, int64_t mod) {
	return (__int128) x * y % mod;	
}
int64_t fastpow(int64_t x, int64_t p, int64_t mod) {
	int64_t r = 1;
	while (p) {
		if (p & 1) r = mul(r, x, mod);
		x = mul(x, x, mod);
		p >>= 1;
	}
	return r;
}

bool test(int64_t n) {
	if (n == 1) return false;
	if (n == 2 || n == 3) return true;
	if (n % 2 == 0) return false;
	int a = n - 1, b = 0;
	while (a % 2 == 0) a >>= 1, ++b;
	for (int i = 0; i < 50; ++i) {
		int64_t x = s() % (n - 2) + 2;
		int64_t v = fastpow(x, a, n);
		if (v == 1) continue;
		bool ok = false;
		for (int j = 0; j < b; ++j) {
			if (v == n - 1) {
				ok = true;
				break;
			}
			v = mul(v, v, n);
		}
		if (!ok) return false;
	}
	return true;
}

int main() {
	int64_t n;
	while (scanf("%lld", &n) == 1)  {
		printf("%c\n", "NY"[test(n)]);
	}
}

详细

Test #1:

score: 0
Time Limit Exceeded

input:

996581938833575363
971646509461160317
773155992127361153
161603952726515268
540879144500456953
476831214764178553
784255927154201144
671096087619405061
805545190025269709
339546334309245137
337726347922962343
222956293307015293
809183111090275843
799050063298926344
691718101820598109
646220213113313...

output:


result: