結果
問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
ユーザー |
|
提出日時 | 2021-04-03 01:05:24 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 410 ms / 9,973 ms |
コード長 | 838 bytes |
コンパイル時間 | 718 ms |
コンパイル使用メモリ | 67,668 KB |
最終ジャッジ日時 | 2025-01-20 10:16:55 |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 10 |
ソースコード
constexpr bool is_prime(long long n) { if(n <= 1) return false; constexpr long long witnesses[12] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}; for(long long a : witnesses) if(n == a) return true; if(~n & 1) return false; long long d = n-1; d >>= __builtin_ctzll(d); for(long long a : witnesses) { long long t = d, y = 1; { long long x = a%n, k = t, m = n; while(k){ if(k & 1) y = (__int128_t)y * x % m; x = (__int128_t)x * x % m; k >>= 1; } } while (t != n-1 && y != 1 && y != n-1) { y = (__int128_t)y * y % n; t <<= 1; } if (y != n-1 && ~t & 1) return false; } return true; } #include<iostream> using namespace std; int main(){ int n; cin >> n; while(n--){ long long x; cin >> x; cout << x << " " << is_prime(x) << endl; } }