QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#670382 | #249. Miller Rabin 算法 | cpchenpi | TL | 0ms | 0kb | C++20 | 1.3kb | 2024-10-23 21:31:07 | 2024-10-23 21:31:09 |
answer
// https://www.youtube.com/watch?v=CrymicX875M
// Angel of mercy
// How did you move me
// Why am I on my feet again
#ifndef ONLINE_JUDGE
#include "templates/debug.hpp"
#else
#define debug(...)
#endif
#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
using u64 = uint64_t;
using u128 = __uint128_t;
i64 pow64(i64 a, i64 b, i64 p) {
i64 res = 1;
for (; b; b >>= 1) {
if (b & 1) res = u128(res) * a % p;
a = u128(a) * a % p;
}
return res;
}
bool millerRabin(i64 p) {
if (p == 1) return false;
i64 d = __builtin_ctzll(p - 1), s = (p - 1) >> d;
for (i64 a: {2, 325, 9375, 28178, 450775, 9780504, 1795265022}) {
if (a % p == 0) continue;
i64 x = pow64(a, s, p), i = 0;
if (x == 1) continue;
for (i64 y = 0; i < d; i++, x = y) {
if (x == p - 1) break;
y = u128(x) * x % p;
if (y == 1 && x != 1) return false;
}
if (i == d) return false;
}
return true;
}
void solve() {
i64 n; cin >> n;
cout << (millerRabin(n) ? "Y" : "N") << "\n";
}
#undef int
// Make bold hypotheses and verify carefully
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int t = 1;
cin >> t;
while (t--) {
solve();
};
}
Details
Tip: Click on the bar to expand more detailed information
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:
N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N ...