結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー FlkanjinFlkanjin
提出日時 2022-09-28 16:33:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 569 ms / 9,973 ms
コード長 955 bytes
コンパイル時間 2,076 ms
コンパイル使用メモリ 202,880 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-02 01:33:41
合計ジャッジ時間 4,468 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 3 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 344 ms
6,940 KB
testcase_05 AC 327 ms
6,940 KB
testcase_06 AC 181 ms
6,940 KB
testcase_07 AC 177 ms
6,944 KB
testcase_08 AC 175 ms
6,940 KB
testcase_09 AC 569 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

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, 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;
}
0