結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
nonamae
|
| 提出日時 | 2022-07-19 18:26:07 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 30 ms / 9,973 ms |
| コード長 | 8,438 bytes |
| コンパイル時間 | 3,477 ms |
| コンパイル使用メモリ | 219,912 KB |
| 最終ジャッジ日時 | 2025-01-30 11:09:59 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 10 |
ソースコード
#pragma region opt
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma endregion opt
#include <bits/stdc++.h>
#pragma region type
using i8 = std::int8_t; using i16 = std::int16_t; using i32 = std::int32_t; using i64 = std::int64_t; using u8 = std::uint8_t; using u16 = std::uint16_t; using u32 = std::uint32_t; using u64 = std::uint64_t;
using i128 = __int128_t; using u128 = __uint128_t;
using f32 = float; using f64 = double; using f80 = long double;
template<typename T> using vec = std::vector<T>;
template<typename T> using vvec = std::vector<std::vector<T>>;
template<typename T> using vvvec = std::vector<std::vector<std::vector<T>>>;
template<typename T> using pvec = std::pair<std::vector<T>, std::vector<T>>;
#pragma endregion type
#pragma region MACRO for
#define FOR(i,a,b) for(int i=(a), i##_len=(b); i<i##_len; ++i)
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define LOOP(n) for(int _=0; _<(n); ++_)
#pragma endregion MACRO for
#pragma region MACRO container
#define ALL(obj) (obj).begin(),(obj).end()
#define SZ(obj) (static_cast<int>((obj).size()))
#pragma endregion MACRO container
#pragma region MACRO bits
#define POPCNT32(a) __builtin_popcount((a))
#define POPCNT64(a) __builtin_popcountll((a))
#define CTZ32(a) __builtin_ctz((a))
#define CLZ32(a) __builtin_clz((a))
#define CTZ64(a) __builtin_ctzll((a))
#define CLZ64(a) __builtin_clzll((a))
#define HAS_SINGLE_BIT32(a) (__builtin_popcount((a)) == (1))
#define HAS_SINGLE_BIT64(a) (__builtin_popcountll((a)) == (1))
#define MSB32(a) ((31) - __builtin_clz((a)))
#define MSB64(a) ((63) - __builtin_clzll((a)))
#define BIT_WIDTH32(a) ((a) ? ((32) - __builtin_clz((a))) : (0))
#define BIT_WIDTH64(a) ((a) ? ((64) - __builtin_clzll((a))) : (0))
#define LSBit(a) ((a) & (-(a)))
#define CLSBit(a) ((a) & ((a) - (1)))
#define BIT_CEIL32(a) ((!(a)) ? (1) : ((POPCNT32(a)) == (1) ? ((1u) << ((31) - CLZ32((a)))) : ((1u) << ((32) - CLZ32(a)))))
#define BIT_CEIL64(a) ((!(a)) ? (1) : ((POPCNT64(a)) == (1) ? ((1ull) << ((63) - CLZ64((a)))) : ((1ull) << ((64) - CLZ64(a)))))
#define BIT_FLOOR32(a) ((!(a)) ? (0) : ((1u) << ((31) - CLZ32((a)))))
#define BIT_FLOOR64(a) ((!(a)) ? (0) : ((1ull) << ((63) - CLZ64((a)))))
#define _ROTL32(x, s) (((x) << ((s) % (32))) | (((x) >> ((32) - ((s) % (32))))))
#define _ROTR32(x, s) (((x) >> ((s) % (32))) | (((x) << ((32) - ((s) % (32))))))
#define ROTL32(x, s) (((s) == (0)) ? (x) : ((((i64)(s)) < (0)) ? (_ROTR32((x), -(s))) : (_ROTL32((x), (s)))))
#define ROTR32(x, s) (((s) == (0)) ? (x) : ((((i64)(s)) < (0)) ? (_ROTL32((x), -(s))) : (_ROTR32((x), (s)))))
#define _ROTL64(x, s) (((x) << ((s) % (64))) | (((x) >> ((64) - ((s) % (64))))))
#define _ROTR64(x, s) (((x) >> ((s) % (64))) | (((x) << ((64) - ((s) % (64))))))
#define ROTL64(x, s) (((s) == (0)) ? (x) : ((((i128)(s)) < (0)) ? (_ROTR64((x), -(s))) : (_ROTL64((x), (s)))))
#define ROTR64(x, s) (((s) == (0)) ? (x) : ((((i128)(s)) < (0)) ? (_ROTL64((x), -(s))) : (_ROTR64((x), (s)))))
#pragma endregion MACRO bits
#pragma region util
template<class T> inline bool chmax(T& a,T b){ if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a,T b){ if (a > b) { a = b; return 1; } return 0; }
#pragma endregion util
#pragma region IO
// -2147483648 ~ 2147483647 (> 10 ^ 9)
i32 in_i32(void) {
i32 c, x = 0, f = 1;
while (c = getchar_unlocked(), c < 48 || c > 57) if (c == 45) f = -f;
while (47 < c && c < 58) {
x = x * 10 + c - 48;
c = getchar_unlocked();
}
return f * x;
}
static inline void out_i32_inner(i32 x) {
if (x >= 10) out_i32_inner(x / 10);
putchar_unlocked(x - x / 10 * 10 + 48);
}
void out_i32(i32 x) {
if (x < 0) {
putchar_unlocked('-');
x = -x;
}
out_i32_inner(x);
}
// -9223372036854775808 ~ 9223372036854775807 (> 10 ^ 18)
i64 in_i64(void) {
i64 c, x = 0, f = 1;
while (c = getchar_unlocked(), c < 48 || c > 57) if (c == 45) f = -f;
while (47 < c && c < 58) {
x = x * 10 + c - 48;
c = getchar_unlocked();
}
return f * x;
}
static inline void out_i64_inner(i64 x) {
if (x >= 10) out_i64_inner(x / 10);
putchar_unlocked(x - x / 10 * 10 + 48);
}
void out_i64(i64 x) {
if (x < 0) {
putchar_unlocked('-');
x = -x;
}
out_i64_inner(x);
}
// 0 ~ 4294967295 (> 10 ^ 9)
u32 in_u32(void) {
u32 c, x = 0;
while (c = getchar_unlocked(), c < 48 || c > 57);
while (47 < c && c < 58) {
x = x * 10 + c - 48;
c = getchar_unlocked();
}
return x;
}
void out_u32(u32 x) {
if (x >= 10) out_u32(x / 10);
putchar_unlocked(x - x / 10 * 10 + 48);
}
// 0 ~ 18446744073709551615 (> 10 ^ 19)
u64 in_u64(void) {
u64 c, x = 0;
while (c = getchar_unlocked(), c < 48 || c > 57);
while (47 < c && c < 58) {
x = x * 10 + c - 48;
c = getchar_unlocked();
}
return x;
}
void out_u64(u64 x) {
if (x >= 10) out_u64(x / 10);
putchar_unlocked(x - x / 10 * 10 + 48);
}
void NL(void) { putchar_unlocked('\n'); }
void SP(void) { putchar_unlocked(' '); }
#pragma endregion IO
u32 is_prime32(u32 n) {
using m32 = u32;
const m32 one = (u32)-1u % n + 1;
const m32 r2 = (u64)(i64)-1 % n + 1;
m32 N_ = n;
for (int _ = 0; _ < 4; ++_) N_ *= 2 - N_ * n;
const m32 N = N_;
auto reduce = [](u64 a, m32 x, u32 mod) {
u32 y = (u32)(a >> 32) - (u32)(((u64)((u32)a * x) * mod) >> 32);
return (i32)y < 0 ? y + mod : y;
};
auto to = [&reduce](u32 a, m32 x, m32 y, u32 mod) -> m32 {
return reduce((u64)a * x, y, mod);
};
auto mul = [&reduce](m32 x, m32 y, m32 z, u32 mod) -> m32 {
return reduce(u64(x) * y, z, mod);
};
auto pow = [&mul](m32 a, u32 k, m32 x, m32 z, u32 mod) -> m32 {
m32 ret = x;
m32 A = a;
while (k > 0) {
if (k & 1) ret = mul(ret, A, z, mod);
A = mul(A, A, z, mod);
k >>= 1;
}
return ret;
};
u32 m = n - 1;
m32 rev = to(m, r2, N, n);
u32 d = m >> CTZ32(m);
u32 base[] = { 2u, 7u, 61u };
for (int i = 0; i < 3; i++) {
if (n <= base[i]) break;
u32 t = d;
m32 y = pow(to(base[i], r2, N, n), t, one, N, n);
while (t != m && y != one && y != rev) {
y = mul(y, y, N, n);
t <<= 1;
}
if (y != rev && (!(t & 1))) return false;
}
return true;
}
u64 is_prime64(u64 n) {
using m64 = u64;
const m64 one = (u64)-1ull % n + 1;
const m64 r2 = (u128)(i128)-1 % n + 1;
m64 N_ = n;
for (int _ = 0; _ < 5; _++) N_ *= 2 - N_ * n;
const m64 N = N_;
auto reduce = [](u128 a, m64 x, u64 mod) {
u64 y = (u64)(a >> 64) - (u64)(((u128)((u64)a * x) * mod) >> 64);
return (i64)y < 0 ? y + mod : y;
};
auto to = [&reduce](u64 a, m64 x, m64 y, u64 mod) -> m64 {
return reduce((u128)a * x, y, mod);
};
auto mul = [&reduce](m64 x, m64 y, m64 z, u64 mod) -> m64 {
return reduce(u128(x) * y, z, mod);
};
auto pow = [&mul](m64 a, u64 k, m64 x, m64 z, u64 mod) -> m64 {
m64 ret = x;
m64 A = a;
while (k > 0) {
if (k & 1) ret = mul(ret, A, z, mod);
A = mul(A, A, z, mod);
k >>= 1;
}
return ret;
};
u64 m = n - 1;
m64 rev = to(m, r2, N, n);
u64 d = m >> CTZ64(m);
u64 base[] = { 2ul, 325ul, 9375ul, 28178ul, 450775ul, 9780504ul, 1795265022ul };
for (int i = 0; i < 7; i++) {
if (n <= base[i]) break;
u64 t = d;
m64 y = pow(to(base[i], r2, N, n), t, one, N, n);
while (t != m && y != one && y != rev) {
y = mul(y, y, N, n);
t <<= 1;
}
if (y != rev && (!(t & 1))) return false;
}
return true;
}
u32 is_prime(u64 n) {
if (n <= 1) return 0;
if (n <= 3) return 1;
if (!(n & 1)) return 0;
if (n < (1ull << 30)) return is_prime32(u32(n));
return is_prime64(n);
}
void Main() {
// your source here
i32 Q = in_i32();
while (Q--) {
u64 x = in_u64();
out_u64(x); SP(); out_i32(is_prime(x));
NL();
}
return;
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
std::cout << std::fixed << std::setprecision(13);
std::cerr << std::fixed << std::setprecision(3);
Main();
}
nonamae