結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
|
| 提出日時 | 2020-12-21 08:56:56 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,682 bytes |
| コンパイル時間 | 2,186 ms |
| コンパイル使用メモリ | 196,044 KB |
| 最終ジャッジ日時 | 2025-01-17 05:25:53 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 4 TLE * 6 |
ソースコード
#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"
/**
* @brief 大きな mod 上の計算
* @note O(1)
*/
inline std::int64_t mod(std::int64_t a, std::int64_t m) {
return (a % m + m) % m;
}
/**
* @note O(\log{m})
*/
inline std::int64_t mul(std::int64_t a, std::int64_t b, std::int64_t m) {
a = mod(a, m), b = mod(b, m);
std::int64_t res = 0;
while (b) {
if (b & 1) res = mod(res + a, m);
a = mod(a + a, m);
b >>= 1;
}
return res;
}
#line 3 "01_Math/02_Combinatorics/01.03.02_mod-pow.big-mod.hpp"
/**
* @brief 累乗 : $a^n\bmod{m}$ ($m$ が大きい場合)
* @note O(\log{n}\log{m})
*/
std::int64_t mod_pow(std::int64_t a, std::int64_t n, std::int64_t m) {
a = (a % m + m) % m;
std::int64_t res = 1;
while (n) {
if (n & 1) res = mul(res, a, m);
a = mul(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;
}
}