結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー MichirakaraMichirakara
提出日時 2024-05-17 12:38:57
言語 C++23(gcc13)
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,164 bytes
コンパイル時間 3,693 ms
コンパイル使用メモリ 276,192 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-05-17 12:39:04
合計ジャッジ時間 5,010 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#line 1 "tests/math/is_prime.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/3030"

#line 1 "math/montgometry-reduction.hpp"
namespace libmcr {
class montgometry_reduction {
    __uint128_t N;
    __uint128_t N_dash;
    __uint128_t R2;

    unsigned long long redc(__uint128_t t) {
        unsigned long long m = N_dash * t;
        __uint128_t to_ret = (t + N * __uint128_t(m)) >> 64;
        return to_ret < N ? to_ret : to_ret - N;
    }

  public:
    montgometry_reduction(unsigned long long mod) : N(mod) {
        N_dash = 0;
        __uint128_t t = 0;
        __uint128_t vi = 1;
        for (int i = 0; i < 64; i++) {
            if ((t & 1) == 0) {
                t += N;
                N_dash += vi;
            }
            t >>= 1;
            vi <<= 1;
        }
        R2 = (0ull - N) * (0ull - N) % N;
    }

    unsigned long long mult(__uint128_t a, __uint128_t b) {
        return redc(redc(a * b) * R2);
    }

    unsigned long long pow(__uint128_t a, __uint128_t b) {
        __uint128_t p = redc(R2 * a);
        __uint128_t x = redc(R2);
        __uint128_t y = b;
        while (y) {
            if (y & 1) {
                x = redc(x * p);
            }
            p = redc(p * p);
            y >>= 1;
        }
        return redc(x);
    }
};
} // namespace libmcr
#line 1 "random/xorshift.hpp"
#include <ctime>
namespace libmcr {
unsigned int xorshift128_32() {
    static unsigned int x = 123456789, y = 362436069, z = 521288629,
                        w = std::time(nullptr);
    unsigned int t = x ^ (x << 11);
    x = y;
    y = z;
    z = w;
    w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
    return w;
}

unsigned long long xorshift128_64() {
    static unsigned long long x = 123456789, y = 362436069, z = 521288629,
                              w = std::time(nullptr);
    unsigned long long t = x ^ (x << 11);
    x = y;
    y = z;
    z = w;
    w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
    return w;
}
} // namespace libmcr
#line 3 "math/prime.hpp"
#include <algorithm>
#include <concepts>
#include <numeric>

namespace libmcr {
namespace internal {

bool fermat_test(unsigned long long n, const size_t REP_NUM) {
    montgometry_reduction MR(n);
    for (size_t rep = 0; rep < REP_NUM; rep++) {
        unsigned long long a = xorshift128_64() % (n - 1) + 1;
        if (MR.pow(a, n - 1) != 1)
            return false;
    }
    return true;
}

bool miller_rabin(unsigned long long n, const size_t REP_NUM) {
    if (n <= 1) {
        return false;
    }
    unsigned long long k = 0;
    unsigned long long m = n - 1;
    while (!(m & 1)) {
        k += 1;
        m >>= 1;
    }
    montgometry_reduction MR(n);
    for (size_t rep = 0; rep < REP_NUM; rep++) {
        unsigned long long a = xorshift128_64() % (n - 2) + 2;
        unsigned long long b = MR.pow(a, m);

        bool flag = false;
        if (b == 1) {
            flag = true;
        } else {
            for (size_t rep2 = 0; rep2 < k; rep2++) {
                if (b == n - 1) {
                    flag = true;
                    break;
                }
                b = MR.mult(b, b);
            }
        }
        if (!flag)
            return false;
    }
    return true;
}

} // namespace internal
bool is_prime(unsigned long long num) {
    const unsigned long long SMALL_PRIMES[] = {2,  3,  5,  7,  11,
                                               13, 17, 19, 23, 29};
    if (num <= 29) {
        for (unsigned long long i : SMALL_PRIMES) {
            if (num == i)
                return true;
        }
        return false;
    }
    if (!(num & 1))
        return false;
    const unsigned long long PRIME_PRODUCT = 3234846615;
    if (std::gcd(PRIME_PRODUCT, num) != 1)
        return false;
    if (internal::fermat_test(num, 5) && internal::miller_rabin(num, 20)) {
        return true;
    }
    return false;
}
} // namespace libmcr
#line 4 "tests/math/is_prime.test.cpp"

#include <bits/stdc++.h>
using namespace std;
int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        unsigned long long x;
        cin >> x;
        cout << x << ' ' << int(libmcr::is_prime(x)) << endl;
    }
}
0