結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
nonamae
|
| 提出日時 | 2021-11-05 12:42:06 |
| 言語 | C (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 10,441 bytes |
| コンパイル時間 | 1,012 ms |
| コンパイル使用メモリ | 47,360 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-11-06 02:18:15 |
| 合計ジャッジ時間 | 1,633 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 4 WA * 6 |
ソースコード
#pragma region opt
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma endregion opt
#pragma region header
#define _GNU_SOURCE
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <string.h>
#include <time.h>
#pragma endregion header
#pragma region type
/* signed integer */
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
typedef __int128_t i128;
/* unsigned integer */
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef __uint128_t u128;
/* floating point number */
typedef float f32;
typedef double f64;
typedef long double f80;
#pragma endregion type
#pragma region macro
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define SWAP(a, b) (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b)))
#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
#pragma region io
int read_int(void) {
// -2147483648 ~ 2147483647 (> 10 ^ 9)
int 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;
}
i32 in_i32(void) {
// -2147483648 ~ 2147483647 (> 10 ^ 9)
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;
}
u32 in_u32(void) {
// 0 ~ 4294967295 (> 10 ^ 9)
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;
}
i64 in_i64(void) {
// -9223372036854775808 ~ 9223372036854775807 (> 10 ^ 18)
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;
}
u64 in_u64(void) {
// 0 ~ 18446744073709551615 (> 10 ^ 19)
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;
}
static inline void write_int_inner(int x) {
if (x >= 10) write_int_inner(x / 10);
putchar_unlocked(x - x / 10 * 10 + 48);
}
void write_int(int x) {
if (x < 0) {
putchar_unlocked('-');
x = -x;
}
write_int_inner(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);
}
void out_u32(u32 x) {
if (x >= 10) out_u32(x / 10);
putchar_unlocked(x - x / 10 * 10 + 48);
}
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);
}
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(' '); }
void write_int_array(int *a, int a_len) {
for (int i = 0; i < a_len; i++) {
if (i) SP();
write_int(a[i]);
}
NL();
}
void out_i32_array(i32 *a, int a_len) {
for (int i = 0; i < a_len; i++) {
if (i) SP();
out_i32(a[i]);
}
NL();
}
void out_u32_array(u32 *a, int a_len) {
for (int i = 0; i < a_len; i++) {
if (i) SP();
out_u32(a[i]);
}
NL();
}
void out_i64_array(i64 *a, int a_len) {
for (int i = 0; i < a_len; i++) {
if (i) SP();
out_i64(a[i]);
}
NL();
}
void out_u64_array(u64 *a, int a_len) {
for (int i = 0; i < a_len; i++) {
if (i) SP();
out_u64(a[i]);
}
NL();
}
#pragma endregion io
#pragma region montgomery32
typedef uint32_t m32;
u32 mod, mod2;
m32 one, two, rev, r2, inv;
static inline void set_m32(u32 m) {
if (mod == m) return;
mod = m;
mod2 = m << 1;
one = (u32)-1u % m + 1;
r2 = (u64)(i64)-1 % mod + 1;
u32 work = m;
for (int i = 0; i < 4; ++i) work *= 2 - work * m;
inv = work;
work = (u32)((2ull * r2) >> 32) - (u32)(((u64)((u32)(2ull * r2) * inv) * m) >> 32);
two = ((i32)work < 0) ? work + m : work;
work = (u32)(((u64)(m - 1) * r2) >> 32) - (u32)(((u64)((u32)((u64)(m - 1) * r2) * inv) * m) >> 32);
rev = ((i32)work < 0) ? work + m : work;
}
m32 _reduce_m32(u64 a) {
u32 work = (u32)(a >> 32) - (u32)(((u64)((u32)a * inv) * mod) >> 32);
return (i32)work < 0 ? work + mod : work;
}
m32 to_m32(u32 a) { return _reduce_m32((u64)a * r2); }
u32 from_m32(m32 A) { return _reduce_m32((u64)A); }
m32 add_m32(m32 A, m32 B) {
A += B - mod;
if ((i32)A < 0) A += mod;
return A;
}
m32 sub_m32(m32 A, m32 B) {
if ((i32)(A -= B) < 0) A += mod2;
return A;
}
m32 min_m32(m32 A) { return sub_m32(0u, A); }
m32 mul_m32(m32 A, m32 B) { return _reduce_m32((u64)A * B); }
m32 pow_m32(m32 A, i64 n) {
m32 ret = one;
while (n > 0) {
if (n & 1) ret = mul_m32(ret, A);
A = mul_m32(A, A);
n >>= 1;
}
return ret;
}
m32 inv_m32(m32 A) { return pow_m32(A, (i64)mod - 2); }
m32 div_m32(m32 A, m32 B) { return mul_m32(A, inv_m32(B)); }
m32 in_m32() {
u32 c, a = 0;
while (c = getchar_unlocked(), c < 48 || c > 57);
while (47 < c && c < 58) {
a = a * 10 + c - 48;
c = getchar_unlocked();
}
return to_m32(a);
}
void out_m32(m32 A) {
u32 a = from_m32(A);
out_u32(a);
}
#pragma endregion montgomery32
#pragma region montgomery64
typedef uint64_t m64;
u64 mod_64, mod2_64;
m64 one_64, two_64, rev_64, r2_64, inv_64;
static inline void set_m64(u64 m) {
if (mod_64 == m) return;
mod_64 = m;
mod2_64 = m << 1;
one_64 = (u64)-1ul % m + 1;
r2_64 = (u64)(i64)-1 % m + 1;
u64 work = m;
for (int i = 0; i < 5; ++i) work *= 2 - work * m;
inv_64 = work;
work = (u64)(((u128)2ull * r2_64) >> 64) - (u64)(((u128)((u64)(2ul * r2_64) * inv_64) * m) >> 64);
two_64 = ((i64)work < 0) ? work + m : work;
work = (u64)((u128)((m - 1) * r2_64) >> 64) - (u64)(((u128)((u64)((m - 1) * r2_64) * inv_64) * m) >> 64);
rev_64 = ((i64)work < 0) ? work + m : work;
}
m64 _reduce_m64(u128 a) {
u64 work = (u64)(a >> 64) - (u64)(((u128)((u64)a * inv_64) * mod_64) >> 64);
return (i64)work < 0 ? work + mod_64 : work;
}
m64 to_m64(u64 a) { return _reduce_m64((u128)a * r2_64); }
u64 from_m64(m64 A) { return _reduce_m64((u128)A); }
m64 add_m64(m64 A, m64 B) {
A += B - mod_64;
if ((i64)A < 0) A += mod_64;
return A;
}
m64 sub_m64(m64 A, m64 B) {
if ((i64)(A -= B) < 0) A += mod2_64;
return A;
}
m64 min_m64(m64 A) { return sub_m64(0u, A); }
m64 mul_m64(m64 A, m64 B) { return _reduce_m64((u64)A * B); }
m64 pow_m64(m64 A, i64 n) {
m64 ret = one_64;
while (n > 0) {
if (n & 1) ret = mul_m64(ret, A);
A = mul_m64(A, A);
n >>= 1;
}
return ret;
}
m64 inv_m64(m64 A) { return pow_m64(A, (i64)mod_64 - 2); }
m64 div_m64(m64 A, m64 B) { return mul_m64(A, inv_m64(B)); }
m64 in_m64() {
u64 c, a = 0;
while (c = getchar_unlocked(), c < 48 || c > 57);
while (47 < c && c < 58) {
a = a * 10 + c - 48;
c = getchar_unlocked();
}
return to_m64(a);
}
void out_m64(m64 A) {
u64 a = from_m64(A);
out_u64(a);
}
#pragma endregion montgomery64
#pragma region Miller_Rabin primary test
bool is_prime32(u32 n) {
set_m32(n);
u32 m = n - 1;
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_m32(to_m32(base[i]), t);
while (t != m && y != one && y != rev) {
y = mul_m32(y, y);
t <<= 1;
}
if (y != rev && (!(t & 1))) return false;
}
return true;
}
bool is_prime64(u64 n) {
u64 m = n - 1;
set_m64(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_m64(to_m64(base[i]), t);
while (t != m && y != one && y != rev) {
y = mul_m64(y, y);
t <<= 1;
}
if (y != rev && (!(t & 1))) return false;
}
return true;
}
bool is_prime(u64 n) {
if (n <= 3ul) return n == 2ul || n == 3ul;
if (!(n & 1)) return false;
if (n < ((u32)1u << 31)) return is_prime32((u32)n);
return is_prime64(n);
}
#pragma endregion Miller_Rabin primary test
void Main(void) {
int n = read_int();
while (n--) {
u64 x = in_u64();
out_u64(x);
SP();
write_int(is_prime(x));
NL();
}
}
int main(void) {
Main();
return 0;
}
nonamae