結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー FlkanjinFlkanjin
提出日時 2022-09-28 16:33:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 594 ms / 9,973 ms
コード長 955 bytes
コンパイル時間 2,148 ms
コンパイル使用メモリ 199,496 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-24 04:28:12
合計ジャッジ時間 4,757 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 349 ms
4,376 KB
testcase_05 AC 352 ms
4,380 KB
testcase_06 AC 192 ms
4,376 KB
testcase_07 AC 190 ms
4,376 KB
testcase_08 AC 190 ms
4,376 KB
testcase_09 AC 594 ms
4,380 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