QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#31935 | #249. Miller Rabin 算法 | whereismyteammate# | TL | 0ms | 0kb | C++20 | 961b | 2022-05-13 23:21:05 | 2022-05-13 23:21:08 |
Judging History
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...