結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
|
| 提出日時 | 2022-09-09 20:24:26 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 238 ms / 9,973 ms |
| コード長 | 1,233 bytes |
| コンパイル時間 | 1,518 ms |
| コンパイル使用メモリ | 168,664 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-11-25 20:01:11 |
| 合計ジャッジ時間 | 3,236 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 10 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
// 素数かどうかを判定します(n <= 2^64 なら必ず成功します) : O(logn)
bool rabin_miller(long long n){
switch(n){
case 0: // fall-through
case 1: return false;
case 2: return true;
}
if(n % 2 == 0) return false;
vector<long long> A = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
long long s = 0, d = n - 1;
while(d % 2 == 0){
s++;
d >>= 1;
}
auto modpow = [](__int128_t x, __int128_t e, const __int128_t m) -> __int128_t {
__int128_t ret = 1 % m;
x %= m;
while(e > 0){
if(e & 1) (ret *= x) %= m;
(x *= x) %= m;
e >>= 1;
}
return ret;
};
for(auto a : A){
if(a % n == 0) return true;
long long t, x = modpow(a, d, n);
if(x != 1){
for(t = 0; t < s; ++t){
if (x == n - 1) break;
x = __int128_t(x) * x % n;
}
if(t == s) return false;
}
}
return true;
}
int main(){
int n; cin >> n;
while(n--){
long long x; cin >> x;
cout << x << " " << 0 + rabin_miller(x) << "\n";
}
}