結果
問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
ユーザー |
|
提出日時 | 2017-10-22 14:17:39 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 281 ms / 9,973 ms |
コード長 | 1,007 bytes |
コンパイル時間 | 1,069 ms |
コンパイル使用メモリ | 85,316 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-16 23:01:50 |
合計ジャッジ時間 | 2,167 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 10 |
ソースコード
#include <iostream> #include <algorithm> #include <string> #include <vector> #include <cassert> #include <cstring> #include <random> #include <ctime> using namespace std; bool miller_rabin(unsigned long long n) { if (n <= 1) return false; if (n == 2) return true; static mt19937 mt(time(NULL)); using u64 = unsigned long long; using u128 = __uint128_t; u64 s = n - 1; int e = 0; for (; s % 2 == 0; s /= 2) e++; auto mul = [&](u64 a, u64 b, u64 m) { return u128(a) * b % m; }; for (int ii = 0; ii < 8; ii++) { u64 x = std::uniform_int_distribution<u64>(2, n - 1)(mt); u64 r = 1; for (u64 i = s; i > 0; i >>= 1, x = mul(x, x, n)) { if (i & 1) r = mul(r, x, n); } if (r == 1) continue; for (int i = 1; i < e && r != n - 1; i++) { r = mul(r, r, n); } if (r != n - 1) return false; } return true; } int main() { int n; cin >> n; while (n--) { long long x; scanf("%lld", &x); printf("%lld %d\n", x, miller_rabin(x)); } }