結果

問題 No.8030 ミラー・ラビン素数判定法のテスト
ユーザー Flkanjin
提出日時 2022-09-28 16:33:24
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 997 ms / 9,973 ms
コード長 947 bytes
コンパイル時間 1,944 ms
コンパイル使用メモリ 194,716 KB
最終ジャッジ日時 2025-02-07 17:55:26
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 10
権限があれば一括ダウンロードができます

ソースコード

diff #

#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, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}){
        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;
}
0