結果
問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
ユーザー |
![]() |
提出日時 | 2020-12-24 01:01:19 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 419 ms / 9,973 ms |
コード長 | 1,100 bytes |
コンパイル時間 | 2,128 ms |
コンパイル使用メモリ | 192,740 KB |
最終ジャッジ日時 | 2025-01-17 06:33:32 |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 10 |
ソースコード
#include <bits/stdc++.h> using namespace std; long long pow_mod(long long x, long long n, long long m) { assert(0 <= n && 1 <= m); if (m == 1) return 0; __int128 r = 1, y = (x % m + m) % m; while (n > 0) { if (n & 1) r = r * y % m; y = y * y % m; n >>= 1; } return r; } bool witness(int a, long long n) { long long u = n - 1; int t = __builtin_ctzll(u); u >>= t; __int128 x = pow_mod(a, u, n); for (int i = 0; i < t; ++i) { __int128 y = x * x % n; if (y == 1 && x != 1 && x != n - 1) return true; x = y; } if (x != 1) return true; return false; } bool is_prime(long long n) { if (n <= 1) return false; constexpr int bases[12] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}; for (int a : bases) if (n == a) return true; if (n % 2 == 0) return false; for (int a : bases) if (witness(a, n)) return false; return true; } int main() { int n; cin >> n; while (n--) { long long x; cin >> x; cout << x << ' ' << is_prime(x) << '\n'; } }