結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー legosukelegosuke
提出日時 2020-12-21 08:59:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 659 ms / 9,973 ms
コード長 2,056 bytes
コンパイル時間 1,968 ms
コンパイル使用メモリ 202,648 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 09:40:44
合計ジャッジ時間 3,876 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#line 1 "test/01_Math/01_NumberTheory/02.01.03_yukicoder-3030.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/3030"
#line 1 "template/template.hpp"
#include <bits/stdc++.h>
#define int int64_t
using namespace std;
#line 3 "01_Math/02_Combinatorics/01.01.01_big-mod.hpp"

__int128_t mod_pow(__int128_t a, long long n, long long m) {
    a %= m;
    __int128_t res = 1;
    while (n) {
        if (n & 1) (res *= a) %= m;
        (a *= a) %= m;
        n >>= 1;
    }
    return res;
}

#line 5 "06_Others/04_Random/01_random-number.hpp"
#include <type_traits>

struct Random {
    std::mt19937_64 mt;
    Random() { mt.seed(std::chrono::steady_clock::now().time_since_epoch().count()); }
} rnd;

/**
 * @brief 乱数 (数)
 * @note O(1)
 */
template <typename T>
T random_number(const T a, const T b) {
    assert(a < b);
    if (std::is_integral<T>::value) {
        std::uniform_int_distribution<T> dist(a, b - 1);
        return dist(rnd.mt);
    } else {
        std::uniform_real_distribution<> dist(a, b);
        return dist(rnd.mt);
    }
}

/**
 * @note O(1)
 */
template <typename T>
T random_number(const T b) {
    return random_number(T(0), b);
}
#line 6 "01_Math/01_NumberTheory/02.01.03_is-prime.miller-rabin.hpp"

/**
 * @brief 素数判定 (ミラー・ラビン)
 * @note O(k\log^3{n})
 */
bool is_prime(std::uint64_t n, std::uint32_t k = 20) {
    if (n == 2) return true;
    if (n < 2 || !(n & 1)) return false;
    std::uint64_t d = n - 1;
    while (!(d & 1)) d >>= 1;
    for (std::uint32_t i = 0; i < k; ++i) {
        std::uint64_t a = random_number((std::uint64_t)1, n), t = d, y = mod_pow(a, t, n);
        while (t != n - 1 && y != 1 && y != n - 1) {
            y = mod_pow(y, 2, n);
            t <<= 1;
        }
        if (y != n - 1 && !(t & 1)) return false;
    }
    return true;
}
#line 4 "test/01_Math/01_NumberTheory/02.01.03_yukicoder-3030.test.cpp"

signed main() {
    int n; cin >> n;
    for (int i = 0; i < n; ++i) {
        int x; cin >> x;
        cout << x << " " << is_prime(x) << endl;
    }
}
0