結果

問題 No.8030 ミラー・ラビン素数判定法のテスト
ユーザー lorent_kyopro
提出日時 2020-12-24 01:43:59
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,210 bytes
コンパイル時間 1,680 ms
コンパイル使用メモリ 192,340 KB
最終ジャッジ日時 2025-01-17 06:36:04
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 9 WA * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

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

__attribute__((constructor))
void fast_io() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

long long pow_mod(long long x, long long n, long long m) {
    assert(0 <= n && 1 <= m);
    if (m == 1) return 0;
    long long r = 1, y = (x % m + m) % m;
    while (n > 0) {
        if (n & 1) r = (__int128)r * y % m;
        y = (__int128)y * y % m;
        n >>= 1;
    }
    return r;
}

bool is_prime(long long n) {
    if (n <= 1) return false;
    if (n == 2) return true;
    if (n % 2 == 0) return false;
    constexpr long long bases[7] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
    long long u = n - 1;
    int t = __builtin_ctzll(u);
    u >>= t;
    for (long long a : bases) {
        if (a >= n) break;
        long long x = pow_mod(a, u, n);
        for (int i = 0; i < t; ++i) {
            long long y = (__int128)x * x % n;
            if (y == 1 && x != 1 && x != n - 1) return false;
            x = y;
        }
        if (x != 1) return false;
    }
    return true;
}

int main() {
    int n;
    cin >> n;
    while (n--) {
        long long x;
        cin >> x;
        cout << x << ' ' << is_prime(x) << '\n';
    }
}
0