結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー sekiya9311sekiya9311
提出日時 2017-10-22 10:22:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,212 bytes
コンパイル時間 1,559 ms
コンパイル使用メモリ 167,240 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-29 12:38:40
合計ジャッジ時間 2,327 ms
ジャッジサーバーID
(参考情報)
judge1 / 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;

using value_type = unsigned long long;

value_type pow_mod(value_type a, value_type p, value_type mod) {
    if (p == 0) {
        return 1;
    } else if (p & 1) {
        return a * pow_mod(a, p - 1, mod) % mod;
    } else {
        const value_type t = pow_mod(a, p >> 1, mod);
        return t * t % mod;
    }
}

bool miller_labin(value_type n, int loopNum = 1000) {
    static random_device rnd;
    if (n == 2) {
        return true;
    }
    if (n < 2 || (!(n & 1))) {
        return false;
    }
    value_type d = n - 1;
    while (!(d & 1)) {
        d >>= 1;
    }
    while (loopNum--) {
        value_type a = (((((value_type) rnd()) << 32) | (value_type) rnd()) % (n - 2)) + 1;
        value_type t = d;
        value_type y = pow_mod(a, t, n);
        while (t != n - 1 && y != 1 && y != n - 1) {
            y = (y * y) % n;
            t <<= 1;
        }
        if (y != n - 1 && (!(t & 1))) {
            return false;
        }
    }
    return true;
}

int main() {
    int n;
    scanf("%d", &n);
    while (n--) {
        long long x;
        scanf("%lld", &x);
        printf("%lld %d\n", x, (int) miller_labin(x));
    }
    return 0;
}
0