結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
|
| 提出日時 | 2017-11-07 14:11:25 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,070 bytes |
| コンパイル時間 | 2,300 ms |
| コンパイル使用メモリ | 163,048 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-11-18 16:07:34 |
| 合計ジャッジ時間 | 2,757 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 4 WA * 6 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
bool is_SPRP(uint64_t n, uint32_t a) {
uint64_t d = n - 1, s = 0;
while ((d & 1) == 0) {
++s;
d >>= 1;
}
__uint128_t cur = 1, pw = d;
while (pw) {
if (pw & 1) {
cur = (cur * a) % n;
}
a = ((__uint128_t)a * a) % n;
pw >>= 1;
}
if (cur == 1) {
return true;
}
for (uint32_t r = 0; r < s; r++) {
if (cur == n - 1) {
return true;
}
cur = (cur * cur) % n;
}
return false;
}
bool is_prime(uint64_t x) {
if (x <= 1) return false;
if (x < 1ULL << 32) {
const vector<uint32_t> bases{2, 7, 61};
for (const uint32_t base : bases) {
if (x != base && !is_SPRP(x, base)) return false;
}
} else {
const vector<uint64_t> bases{2, 325, 9375, 28178, 450775, 9780504, 1795265022};
for (const uint64_t base : bases) {
if (x != base && !is_SPRP(x, base)) return false;
}
}
return true;
}
int main() {
int q;
cin >> q;
while (q--) {
long long n;
cin >> n;
cout << n << " " << is_prime(n) << endl;
}
}