結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー kusanokusano
提出日時 2023-01-08 20:54:58
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,085 bytes
コンパイル時間 558 ms
コンパイル使用メモリ 64,804 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-05-09 11:46:35
合計ジャッジ時間 3,196 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

long long powmod(long long a, long long b, long long m)
{
    long long r = 1;
    for (; b>0; b>>=1, a=a*a%m)
        if (b&1)
            r = r*a%m;
    return r;
}

bool is_prime(long long n)
{
    if (n==1)
        return false;
    if (n==2)
        return true;
    if (n%2==0)
        return false;

    long long d = n-1;
    long long s = 0;
    while (d%2==0)
    {
        d /= 2;
        s++;
    }

    // https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test#Testing_against_small_sets_of_bases
    long long A[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
    for (long long a: A)
    {
        bool composite = powmod(a, d, n)!=1;
        long long t = d;
        for (int i=0; i<s && composite; i++, t*=2)
            if (powmod(a, t, n)!=n-1)
                composite = false;
        if (composite)
            return false;
    }
    return true;
}

#include <iostream>
using namespace std;

int main()
{
    int n;
    cin>>n;
    for (int i=0; i<n; i++)
    {
        long long x;
        cin>>x;
        cout<<x<<" "<<is_prime(x)<<endl;
    }
}
0