結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー kappybarkappybar
提出日時 2020-04-29 15:38:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,206 bytes
コンパイル時間 1,555 ms
コンパイル使用メモリ 170,484 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-29 13:54:52
合計ジャッジ時間 3,078 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
using ll =  long long ;
using P = pair<int,int> ;
using pll = pair<ll,ll>;
const ll INF = 1e18;
const int MOD = 1000000007;

long long modpow(long long x, long long n,long long mod) {
    long long ret = 1;
    while (n > 0) {
        if (n & 1) ret = (ret * x) % mod;  
        x = (x * x) % mod;
        n >>= 1;  
    }
    return ret;
}


bool Miller_Rabin(long long x){
    if(x == 0) return false;
    if(x == 1) return false;
    if(x == 2) return true;
    if(x%2 == 0) return false;

    long long d = x - 1;
    while(d%2 == 0) d /= 2;

    random_device rand;
    mt19937 mt(rand());
    uniform_int_distribution<long long> random_maker(1,x-1);

    
    for(int i = 0;i < 20; ++i){
        long long a = modpow(random_maker(mt),d,x);
        long long s = d;

        while(s != x-1 && a != 1 && a != x-1){ 
            a = (a * a)%x;
            s <<= 1;
        }

        if(a != x-1 && s%2 == 0) return false;

    }
    return true;
}

int main(){
    int n;
    cin >> n;
    while(n--){
        ll x; cin >> x; cout << x << " " ;
        cout << (Miller_Rabin(x) ? 1 : 0) << endl; 
    }
    return 0;
}
0