結果

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

テストケース

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