結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
|
| 提出日時 | 2020-11-13 01:59:53 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 763 ms / 9,973 ms |
| コード長 | 1,217 bytes |
| コンパイル時間 | 2,301 ms |
| コンパイル使用メモリ | 196,380 KB |
| 最終ジャッジ日時 | 2025-01-15 22:17:39 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 10 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
class MillerRabin {
__int128_t mod_pow(__int128_t a, long long n, long long m) {
a %= m;
__int128_t res = 1;
while (n) {
if (n & 1) (res *= a) %= m;
(a *= a) %= m;
n >>= 1;
}
return res;
}
public:
mt19937_64 mt;
MillerRabin() {
mt.seed(chrono::steady_clock::now().time_since_epoch().count());
}
bool is_prime(long long n, int k = 20) {
if (n == 2) return true;
if (n < 2 || !(n & 1)) return false;
uniform_int_distribution<long long> dist(1, n - 1);
long long d = n - 1;
while (!(d & 1)) d >>= 1;
for (int i = 0; i < k; ++i) {
long long a = dist(mt), t = d, y = mod_pow(a, t, n);
while (t != n - 1 && y != 1 && y != n - 1) {
y = mod_pow(y, 2, n);
t <<= 1;
}
if (y != n - 1 && !(t & 1)) return false;
}
return true;
}
};
int main() {
int n;
cin >> n;
MillerRabin mr;
for (int i = 0; i < n; ++i) {
long long x;
cin >> x;
cout << x << " " << mr.is_prime(x) << endl;
}
}