結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
AC2K
|
| 提出日時 | 2023-03-31 11:37:38 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,768 bytes |
| コンパイル時間 | 3,170 ms |
| コンパイル使用メモリ | 243,432 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-09-22 16:39:55 |
| 合計ジャッジ時間 | 3,520 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | WA * 10 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
template <class T, class U = T>
constexpr U mod_pow(T base, T exp, T mod) {
T ans = 1;
base %= mod;
while (exp > 0) {
if (exp & 1) {
ans *= base;
ans %= mod;
}
base *= base;
base %= mod;
exp >>= 1;
}
return ans;
}
namespace prime {
namespace miller {
using i128 = __int128_t;
using u128 = __uint128_t;
using u64 = __uint64_t;
constexpr bool miller_rabin_long(const u64& n) {
if (n <= 1) return false;
if (n == 2)return true;
if (n % 2 == 0) return false;
u64 d = n - 1;
while (~d & 1) d >>= 1;
constexpr u64 bases_long[] = { 2, 325, 9375, 28178, 450775, 9780504, 1795265022 };
for (const auto& a : bases_long) {
if (n == a) {
return true;
}
u64 t = d;
u64 pw = mod_pow<u128>(a, t, n);
while (t != n - 1 && pw != 1 && pw != n - 1) {
pw = pw * pw % n;
t <<= 1;
}
if (pw != n - 1 && ~t & 1 == 0) {
return false;
}
}
return true;
}
constexpr bool is_prime(u64 n) {
return miller_rabin_long(n);
}
};
};
///@brief fast prime check(MillerRabinの素数判定)
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
uint64_t xi;
scanf("%lld", &xi);
printf("%lld ", xi);
if (prime::miller::is_prime(xi)) {
puts("1");
}
else {
puts("0");
}
}
}
AC2K