結果
問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
ユーザー |
![]() |
提出日時 | 2024-12-04 11:47:26 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 274 ms / 9,973 ms |
コード長 | 1,337 bytes |
コンパイル時間 | 2,149 ms |
コンパイル使用メモリ | 196,048 KB |
最終ジャッジ日時 | 2025-02-26 10:53:22 |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 10 |
ソースコード
#include <bits/stdc++.h> //#include <atcoder/modint> using namespace std; //using namespace atcoder; using ll = long long; //using mint = modint998244353; using cll = __int128_t; cll mod_exp(cll b, cll e, cll m){ if (e > 0 && b == 0) return 0; cll ans = 1 % m; b %= m; while (e > 0){ if (e & 1) ans = (ans * b) % m; e >>= 1; b = (b*b) % m; } return ans; } bool _miller_rabin(ll N, vector<ll> A){ ll s = 0, t = N-1; while(!(t & 1)){ s++; t >>= 1; } for (auto a : A){ if (N <= a) return 1; ll x = mod_exp(a, t, N); if (x == 1) continue; bool f=0; for (int r=0; r<s; r++){ if (x == N-1){ f = 1; break; } x = (cll)x * x % N; } if (!f) return 0; } return 1; } bool miller_rabin(ll N){ if (N == 2) return 1; if (N <= 1) return 0; if (N % 2 == 0) return 0; if (N < 4759123141LL) return _miller_rabin(N, {2, 7, 61}); return _miller_rabin(N, {2, 325, 9375, 28178, 450775, 9780504, 1795265022}); } int main(){ cin.tie(nullptr); ios_base::sync_with_stdio(false); ll N, x; cin >> N; while(N--){ cin >> x; cout << x << " " << miller_rabin(x) << endl; } return 0; }