結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー legosukelegosuke
提出日時 2020-11-13 01:59:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 684 ms / 9,973 ms
コード長 1,217 bytes
コンパイル時間 1,968 ms
コンパイル使用メモリ 202,616 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 09:39:04
合計ジャッジ時間 4,068 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

class MillerRabin {
    __int128_t mod_pow(__int128_t a, long long n, long long m) {
        a %= m;
        __int128_t res = 1;
        while (n) {
            if (n & 1) (res *= a) %= m;
            (a *= a) %= m;
            n >>= 1;
        }
        return res;
    }

public:
    mt19937_64 mt;

    MillerRabin() {
        mt.seed(chrono::steady_clock::now().time_since_epoch().count());
    }

    bool is_prime(long long n, int k = 20) {
        if (n == 2) return true;
        if (n < 2 || !(n & 1)) return false;
        uniform_int_distribution<long long> dist(1, n - 1);
        long long d = n - 1;
        while (!(d & 1)) d >>= 1;
        for (int i = 0; i < k; ++i) {
            long long a = dist(mt), t = d, y = mod_pow(a, t, n);
            while (t != n - 1 && y != 1 && y != n - 1) {
                y = mod_pow(y, 2, n);
                t <<= 1;
            }
            if (y != n - 1 && !(t & 1)) return false;
        }
        return true;
    }
};

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