結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
minami
|
| 提出日時 | 2019-04-04 10:29:36 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,956 bytes |
| コンパイル時間 | 1,841 ms |
| コンパイル使用メモリ | 168,736 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-11-18 16:37:18 |
| 合計ジャッジ時間 | 20,480 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 8 WA * 2 |
ソースコード
#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif
//#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = 1'000'000'007;
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }
// (a*b) % mod
template<typename T>
T modmul(T a, T b, T mod) {
dump(a, b, mod);
T x = 0, y = a % mod;
while (b > 0) {
if (b & 1)x = x + y % mod;
y = y * 2 % mod;
b >>= 1;
}
dump(x % mod);
return x % mod;
}
// 累乗
// O(log e)
// mod^2 が T の最大値より大きければオーバーフローするので掛け算に modmul を使う
template<typename T>
T modpow(T a, T e, T mod) {
dump(a, e, mod);
T res = 1;
while (e > 0) {
if (e & 1)res = modmul(res, a, mod);
a = modmul(a, a, mod);
e >>= 1;
}
dump(res);
return res;
}
// 素数判定(Miller-Rabin primality test)
// 2^24程度から
// millerRabinPrimalityTest(n, 5)
template<typename T>
bool millerRabinPrimalityTest(T x, int iteration) {
if (x < 2)return false;
if (x != 2 && x % 2 == 0)return false;
T s = x - 1;
while (s % 2 == 0)s /= 2;
for (int i = 0; i < iteration; i++) {
T a = rand() % (x - 1) + 1, tmp = s;
T mod = modpow(a, tmp, x);
while (tmp != x - 1 && mod != 1 && mod != x - 1) {
mod = modmul(mod, mod, x); // mod * mod % x;
tmp *= 2;
dump(tmp, mod);
}
if (mod != x - 1 && tmp % 2 == 0)return false;
}
return true;
}
using u128 = __uint128_t;
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n; cin >> n;
rep(_, 0, n) {
long long x; cin >> x;
cout << x << " " << millerRabinPrimalityTest(u128(x), 5) << endl;
}
return 0;
}
minami