結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー AC2KAC2K
提出日時 2023-03-31 14:58:11
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,221 bytes
コンパイル時間 3,084 ms
コンパイル使用メモリ 244,684 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 02:11:26
合計ジャッジ時間 4,063 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
template <class T, class U = T>
constexpr inline U mod_pow(T base, T exp, T mod) {
    T ans = 1;
    base %= mod;
    while (exp > 0) {
        if (exp & 1) {
            ans *= base;
            ans %= mod;
        }
        base *= base;
        base %= mod;
        exp >>= 1;
    }
    return ans;
}

namespace prime {
    namespace miller {
        using i128 = __int128_t;
        using u128 = __uint128_t;
        using u64 = uint64_t;
        using u32 = uint32_t;

        bool miller_rabin(uint64_t n,const uint64_t bases[],int siz) {
            if (n == 2) { return true; }
            if (n < 2 || (n & 1) == 0) { return false; }
            uint64_t n1 = n - 1, d = n - 1;
            uint32_t s = 0;
            for (; (d & 1) == 0; d >>= 1) { s += 1; }
            for (int i = 0; i < siz; i++) {
                uint64_t a = bases[i];
                if (a >= n) { a %= n; if (a == 0) { continue; } }
                uint64_t t = mod_pow<u128>(a, d, n);
                if (t == 1) { continue; }
                for (uint32_t j = 1; t != n1; ++j) {
                    if (j >= s) { return false; }
                    (t *= t) %= n;
                }
            }
            return true;
        }


        constexpr u64 bases_int[3] = { 2, 7, 61 };  // intだと、2,7,61で十分
        constexpr u64 bases_ll[7] = { 2, 325, 9375, 28178, 450775, 9780504, 1795265022 };
        bool is_prime(u64 n) {
            if (n < 2) {
                return false;
            }
            else if (n == 2) {
                return true;
            }
            else if (~n & 1) {
                return false;
            }
            if (n < (1ul << 31)) {
                return miller_rabin(n, bases_int, 3);
            }
            else {
                return miller_rabin(n, bases_ll, 7);
            }
        }
    };
};
///@brief fast prime check(MillerRabinの素数判定)


int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        uint64_t xi;
        scanf("%lld", &xi);
        printf("%lld ", xi);
        if (prime::miller::is_prime(xi)) {
            puts("1");
        }
        else {
            puts("0");
        }
    }
}
0