結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー tabrtabr
提出日時 2020-05-05 17:19:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 222 ms / 9,973 ms
コード長 1,141 bytes
コンパイル時間 1,366 ms
コンパイル使用メモリ 168,460 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 09:30:11
合計ジャッジ時間 2,460 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif

bool is_prime(unsigned long long n) {
    if (n < 2) {
        return false;
    }
    vector<unsigned long long> bases = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
    unsigned long long s = __builtin_ctzll(n - 1), d = n >> s;
    for (auto a : bases) {
        unsigned long long p = 1, i = s, j = d, k = a;
        while (j > 0) {
            if (j & 1) {
                p = (__int128)p * k % n;
            }
            k = (__uint128_t)k * k % n;
            j >>= 1;
        }
        while (p != 1 && p != n - 1 && a % n && i--) {
            p = (__uint128_t)p * p % n;
        }
        if (p != n - 1 && i != s) {
            return false;
        }
    }
    return true;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        ll x;
        cin >> x;
        cout << x << " ";
        int j = 0;
        if (is_prime(x)) {
            j = 1;
        }
        cout << j << '\n';
    }
    return 0;
}
0