結果
| 問題 | No.8030 ミラー・ラビン素数判定法のテスト | 
| ユーザー |  Michirakara | 
| 提出日時 | 2024-05-17 07:29:14 | 
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 907 ms / 9,973 ms | 
| コード長 | 3,129 bytes | 
| コンパイル時間 | 3,820 ms | 
| コンパイル使用メモリ | 275,144 KB | 
| 実行使用メモリ | 6,820 KB | 
| 最終ジャッジ日時 | 2024-12-20 11:30:48 | 
| 合計ジャッジ時間 | 6,519 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 10 | 
ソースコード
#line 1 "math/modpow.hpp"
namespace libmcr {
template <typename T> T modpow(T x, T n, T mod) {
    __uint128_t ans = 1;
    __uint128_t xx = x;
    while (n) {
        if (n & 1) {
            ans = ans * xx % mod;
        }
        xx = xx * xx % mod;
        n >>= 1;
    }
    return ans;
}
} // 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) {
    for (size_t rep = 0; rep < REP_NUM; rep++) {
        unsigned long long a = xorshift128_64() % (n - 1) + 1;
        if (modpow(a, n - 1, n) != 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;
    }
    for (size_t rep = 0; rep < REP_NUM; rep++) {
        unsigned long long a = xorshift128_64() % (n - 2) + 2;
        unsigned long long b = modpow(a, m, n);
        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 = __uint128_t(b) * __uint128_t(b) % n;
            }
        }
        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 2 "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;
    }
}
            
            
            
        