結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
Flkanjin
|
| 提出日時 | 2022-09-28 16:33:57 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 585 ms / 9,973 ms |
| コード長 | 955 bytes |
| コンパイル時間 | 1,883 ms |
| コンパイル使用メモリ | 194,248 KB |
| 最終ジャッジ日時 | 2025-02-07 17:55:36 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 10 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
__int128_t modpow(__int128_t a, __int128_t b, __int128_t m){
__int128_t ret{1 % m};
a %= m;
while(b){
if(b & 1) (ret *= a) %= m;
(a *= a) %= m;
b >>= 1;
}
return ret;
}
bool millerRabin(long long N){
if(!(N % 2) || N == 1) return N == 2;
int s{};
long long d{N-1};
while(!(d % 2)){
++s; d >>= 1;
}
for(int a: {2, 325, 9375, 28178, 450775, 9780504, 1795265022}){
if(!(a % N)) break;
if(modpow(a, d, N) == 1) continue;
bool okay{true};
for(int r{}; r < s; ++r) if(modpow(a, d*(1LL<<r), N) == N-1){
okay = false; break;
}
if(okay) return false;
}
return true;
}
int main(){
int N; cin >> N;
{
long long A;
for(int i{}; i < N; ++i){
cin >> A;
cout << A << " " << millerRabin(A) << "\n";
}
}
return 0;
}
Flkanjin