結果
問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
ユーザー |
|
提出日時 | 2020-07-29 10:26:07 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 745 ms / 9,973 ms |
コード長 | 1,521 bytes |
コンパイル時間 | 2,253 ms |
コンパイル使用メモリ | 196,892 KB |
最終ジャッジ日時 | 2025-01-12 07:29:49 |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 10 |
ソースコード
#include <bits/stdc++.h> using namespace std; class Random { using T = uint64_t; mt19937_64 mt; random_device rd; public: Random() { seed(); } void seed() { mt.seed(rd()); } void seed(T s) { mt.seed(s); } T operator()() { return mt(); } T operator()(T r) { // [0, r) uniform_int_distribution<T> u(0, 0 < r ? r - 1 : 0); return u(mt); } T operator()(T l, T r) { // [l, r) uniform_int_distribution<T> u(l, max(l, r) - 1); return u(mt); } T dice() { return operator()(1, 7); } bool rand_bool() { return operator()(2); } bool rand_bool(double p) { bernoulli_distribution u(p); return u(mt); } template <class T> void shuffle(T& v) { std::shuffle(v.begin(), v.end(), mt); } } rnd; __uint128_t Powmod(__uint128_t a, __int128_t n, __int128_t m) { __uint128_t res = 1; while (n > 0) { if (n & 1) res = res * a % m, n--; else a = a * a % m, n >>= 1; } return res; } bool MillerRabin(const uint64_t n) { if (n == 2) { return true; } else if (n <= 1 || !(n & 1)) { return false; } uint64_t d = n - 1; while (!(d & 1)) { d >>= 1; } for (int _ = 20; _--;) { auto a = rnd(1, n); __int128_t t = d, y = Powmod(a, t, n); while (t != n - 1 && y != 1 && y != n - 1) { y = y * y % n; t <<= 1; } if (y != n - 1 && !(t & 1)) { return false; } } return true; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int32_t n; cin >> n; while (n--) { uint64_t x; cin >> x; cout << x << ' ' << MillerRabin(x) << '\n'; } }