結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
yuruhiya
|
| 提出日時 | 2020-07-28 22:12:58 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 875 bytes |
| コンパイル時間 | 2,760 ms |
| コンパイル使用メモリ | 192,260 KB |
| 最終ジャッジ日時 | 2025-01-12 07:19:30 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | WA * 10 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
__int128_t Powmod(__int128_t a, __int128_t n, __int128_t m) {
__int128_t r = 1;
while (n > 0) {
if (n & 1)
r = r * a % m, n--;
else
a = a * a % m, n /= 2;
}
return r;
}
bool MillerRabin(const __int128_t n) {
assert(n > 0);
if (n == 2) {
return true;
} else if (n == 1 || n % 2 == 0) {
return false;
}
__int128_t d = n - 1;
while (d % 2 == 0) {
d /= 2;
}
for (__int128_t a : {2, 325, 9375, 28178, 450775, 9780504, 1795265022}) {
__int128_t t = d, y = Powmod(a, t, n);
while (t != n - 1 && y != 1 && y != n - 1) {
y = (y * y) % n;
t /= 1;
}
if (y != n - 1 && t % 2 == 0) {
return false;
}
}
return true;
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n;
cin >> n;
while (n--) {
long long x;
cin >> x;
cout << x << ' ' << MillerRabin(x) << '\n';
}
}
yuruhiya