結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー Dr_GeometryDr_Geometry
提出日時 2020-07-25 16:42:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,095 bytes
コンパイル時間 1,694 ms
コンパイル使用メモリ 170,000 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-29 14:01:17
合計ジャッジ時間 2,430 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 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>
using namespace std;
typedef long long ll;
int challenge = 100;

ll powmod(ll a, ll x, ll p){
    ll res=1;
    while(x>0){
        if(x%2 == 1){
            res = (res * a) % p;
            x--;
        }
        a = (a*a)%p;
        x >>= 1;
    }
    return res;
}

bool is_prime(ll n){
    if(n == 2) return(true);
    if(n == 1 || (n&1) == 0) return (false);
    
    mt19937 mt;
    ll s=1;
    ll d=n;
    do{
        d >>= 1;
    }while((d&1) == 0);

    for(int i=0;i<challenge;i++){
        ll a = 1 + mt() % (n-1);
        ll t = d;
        ll y = powmod(a,t,n);
        while(t != n-1 && y != 1 && y != n-1){
            y = powmod(y,2,n);
            t <<= 1;
        }
        if(y != n-1 && (t&1) == 0) return false;
    }
    
    return(true);
}

int main() {
    int n;
    cin >> n;
    vector<ll> x(n);
    for(int i=0;i<n;i++){
        cin >> x[i];
    }

    for(int i=0;i<n;i++){
        cout << x[i] << " ";
        if(is_prime(x[i])){
            cout << 1 << endl;
        }else{
            cout << 0 << endl;
        }
    }

    return(0);
}
0