#define LOCAL #ifndef LOCAL #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("fast-math") #endif #include #include #include #include #include #include #include #include #include /* 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; /* io */ static inline 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; } static inline u32 in_u32(void) { // 0 ~ 4294967295 (> 10 ^ 9) 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 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; } static inline 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(int x) { if (x >= 10) _write_int(x / 10); putchar_unlocked(x - x / 10 * 10 + 48); } static inline void write_int(int x) { if (x < 0) { putchar_unlocked('-'); x = -x; } _write_int(x); } static inline void out_u32(u32 x) { if (x >= 10) out_u32(x / 10); putchar_unlocked(x - x / 10 * 10 + 48); } static inline void _out_i64(i64 x) { if (x >= 10) _out_i64(x / 10); putchar_unlocked(x - x / 10 * 10 + 48); } static inline void out_i64(i64 x) { if (x < 0) { putchar_unlocked('-'); x = -x; } _out_i64(x); } static inline void out_u64(u64 x) { if (x >= 10) out_u64(x / 10); putchar_unlocked(x - x / 10 * 10 + 48); } static inline void NL(void) { putchar_unlocked('\n'); } static inline void SP(void) { putchar_unlocked(' '); } /* macro */ #define SWAP(a, b) (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b))) #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b)) /* bit macro */ #define POPCNT(a) __builtin_popcountll((a)) #define CTZ(a) __builtin_ctzll((a)) #define CLZ(a) __builtin_clzll((a)) #define LSBit(a) ((a) & (-(a))) #define CLSBit(a) ((a) & ((a) - (1))) #define HAS_SINGLE_BIT(a) (POPCNT((a)) == 1) #define MSB32(a) ((31) - __builtin_clz((a))) #define MSB64(a) ((63) - __builtin_clzll((a))) #define BIT_CEIL(a) ((!(a)) ? (1) : ((POPCNT(a)) == (1) ? ((1ull) << ((63) - CLZ((a)))) : ((1ull) << ((64) - CLZ(a))))) #define BIT_FLOOR(a) ((!(a)) ? (0) : ((1ull) << ((63) - CLZ((a))))) #define BIT_WIDTH(a) ((a) ? ((64) - CLZ((a))) : (0)) #define _ROTL(x, s) (((x) << ((s) % (64))) | (((x) >> ((64) - ((s) % (64)))))) #define _ROTR(x, s) (((x) >> ((s) % (64))) | (((x) << ((64) - ((s) % (64)))))) #define ROTL(x, s) (((s) == (0)) ? (0) : ((((i128)(s)) < (0)) ? (_ROTR((x), -(s))) : (_ROTL((x), (s))))) #define ROTR(x, s) (((s) == (0)) ? (0) : ((((i128)(s)) < (0)) ? (_ROTL((x), -(s))) : (_ROTR((x), (s))))) /* debug */ #ifdef LOCAL void dump_int(int x) { fprintf(stderr, "\033[1;36m%d\033[0m\n", x); } void dump_i64(i64 x) { fprintf(stderr, "\033[1;36m%ld\033[0m\n", x); } void dump_u32(u32 x) { fprintf(stderr, "\033[1;36m%u\033[0m\n", x); } void dump_u64(u64 x) { fprintf(stderr, "\033[1;36m%lu\033[0m\n", x); } void dump_int_array(int *a, int a_len) { for (int i = 0; i < a_len; i++) { if (i == a_len - 1) fprintf(stderr, "\033[1;36m%d\033[0m\n", a[i]); else fprintf(stderr, "\033[1;36m%d\033[0m ", a[i]); } } void dump_i64_array(i64 *a, int a_len) { for (int i = 0; i < a_len; i++) { if (i == a_len - 1) fprintf(stderr, "\033[1;36m%ld\033[0m\n", a[i]); else fprintf(stderr, "\033[1;36m%ld\033[0m ", a[i]); } } void dump_u32_array(u32 *a, int a_len) { for (int i = 0; i < a_len; i++) { if (i == a_len - 1) fprintf(stderr, "\033[1;36m%u\033[0m\n", a[i]); else fprintf(stderr, "\033[1;36m%u\033[0m ", a[i]); } } void dump_u64_array(u64 *a, int a_len) { for (int i = 0; i < a_len; i++) { if (i == a_len - 1) fprintf(stderr, "\033[1;36m%lu\033[0m\n", a[i]); else fprintf(stderr, "\033[1;36m%lu\033[0m ", a[i]); } } void printb(u32 v) { u32 mask = (int)1 << (sizeof(v) * CHAR_BIT - 1); do putchar(mask & v ? '1' : '0'); while (mask >>= 1); } void putb(u32 v) { putchar('0'), putchar('b'), printb(v), putchar('\n'); } #endif /* montgomery modular multiplication 32bit */ typedef uint32_t m32; m32 _one_m32(u32 mod) { return -1u % mod + 1; } m32 _r2_m32(u32 mod) { return (u64)(i64)-1 % mod + 1; } m32 _inv_m32(u32 mod) { u32 inv = mod; for (int ixjw82jwm = 0; ixjw82jwm < __builtin_ctz(sizeof(u32) * 8) - 1; ++ixjw82jwm) inv *= 2 - mod * inv; return inv; } m32 _reduce_m32(u64 a, m32 inv, u32 mod) { i64 z = (a >> 32) - ((((u32)a * inv) * (u64)mod) >> 32); return z < 0 ? z + mod : (u32)z; } m32 to_m32(u32 a, m32 r2, m32 inv, u32 mod) { return _reduce_m32((u64)a * r2, inv, mod); } m32 from_m32(m32 A, m32 inv, u32 mod) { m32 t = _reduce_m32((u64)A, inv, mod) - mod; return t + (mod & -(t >> 31u)); } m32 add_m32(m32 A, m32 B, u32 mod) { A += B - (mod << 1u); A += (mod << 1u) & -(A >> 31u); return A; } m32 sub_m32(m32 A, m32 B, u32 mod) { A -= B; A += (mod << 1u) & -(A >> 31u); return A; } m32 min_m32(m32 A, u32 mod) { return sub_m32(0, A, mod); } m32 mul_m32(m32 A, m32 B, m32 inv, u32 mod) { return _reduce_m32((u64)A * B, inv, mod); } m32 pow_m32(m32 A, i64 n, m32 inv, u32 mod) { m32 ret = _one_m32(mod); while (n > 0) { if (n & 1) ret = mul_m32(ret, A, inv, mod); A = mul_m32(A, A, inv, mod); n >>= 1; } return ret; } m32 inv_m32(m32 A, m32 inv, u32 mod) { return pow_m32(A, (i64)mod - 2, inv, mod); } m32 div_m32(m32 A, m32 B, m32 inv, u32 mod) { /* assert(is_prime(mod)); */ return mul_m32(A, inv_m32(B, inv, mod), inv, mod); } m32 in_m32(m32 r2, m32 inv, u32 mod) { 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, r2, inv, mod); } void out_m32(m32 A, m32 inv, u32 mod) { u32 a = from_m32(A, inv, mod); out_u32(a); } #ifdef LOCAL void dump_m32(m32 x, m32 inv, u32 mod) { fprintf(stderr, "\033[1;36m(m32 = %u, u32 = %u)\033[0m\n", x, from_m32(x, inv, mod)); } void dump_m32_array(m32 *x, int x_len, m32 inv, u32 mod) { fprintf(stderr, "m32 * => "); for (int i = 0; i < x_len; i++) { if (i == x_len - 1) fprintf(stderr, "%u\n", x[i]); else fprintf(stderr, "%u ", x[i]); } fprintf(stderr, "u32 * => "); for (int i = 0; i < x_len; i++) { if (i == x_len - 1) fprintf(stderr, "%u\n", from_m32(x[i], inv, mod)); else fprintf(stderr, "%u ", from_m32(x[i], inv, mod)); } } #endif /* montgomery modular multiplication 64bit */ typedef uint64_t m64; m64 _one_m64(u64 mod) { return (u64)-1ull % mod + 1; } m64 _r2_m64(u64 mod) { return (u128)(i128)-1 % mod + 1; } m64 _inv_m64(u64 mod) { u64 inv = mod; for (int h2zq6gm5d = 0; h2zq6gm5d < __builtin_ctz(sizeof(u64) * 8) - 1; ++h2zq6gm5d) inv *= 2 - mod * inv; return inv; } m64 _reduce_m64(u128 a, m64 inv, u64 mod) { i128 A = (a >> 64) - ((((u64)a * inv) * (u128)mod) >> 64); return A < 0 ? (u64)(A + mod) : (u64)A; } m64 to_m64(u64 a, m64 r2, m64 inv, u64 mod) { return _reduce_m64((u128)a * r2, inv, mod); } u64 from_m64(m64 A, m64 inv, u64 mod) { m64 t = _reduce_m64((u128)A, inv, mod) - mod; return t + (mod & -(t >> 63u)); } m64 add_m64(m64 A, m64 B, u64 mod) { A += B - (mod << 1u); A += (mod << 1u) & -(A >> 63u); return A; } m64 sub_m64(m64 A, m64 B, u64 mod) { A -= B; A += (mod << 1u) & -(A >> 63u); return A; } m64 min_m64(m64 A, u64 mod) { return sub_m64(0, A, mod); } m64 mul_m64(m64 A, m64 B, m64 inv, u64 mod) { return _reduce_m64((u128)A * B, inv, mod); } m64 pow_m64(m64 A, i64 n, m64 inv, u64 mod) { m64 ret = _one_m64(mod); while (n > 0) { if (n & 1) ret = mul_m64(ret, A, inv, mod); A = mul_m64(A, A, inv, mod); n >>= 1; } return ret; } m64 recip_m64(m64 A, m64 inv, u64 mod) { return pow_m64(A, (i64)mod - 2, inv, mod); } m64 div_m64(m64 A, m64 B, m64 inv, u64 mod) { return mul_m64(A, recip_m64(B, inv, mod), inv, mod); } m64 in_m64(m64 r2, m64 inv, u64 mod) { 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, r2, inv, mod); } void out_m64(m64 A, m64 inv, u64 mod) { u64 a = from_m64(A, inv, mod); out_u64(a); } #ifdef LOCAL void dump_m64(m64 x, m64 inv, u64 mod) { fprintf(stderr, "\033[1;36m(m64 = %lu, u64 = %lu)\033[0m\n", x, from_m64(x, inv, mod)); } void dump_m64_array(m64 *x, int x_len, m64 inv, u64 mod) { fprintf(stderr, "m64 * => "); for (int i = 0; i < x_len; i++) { if (i == x_len - 1) fprintf(stderr, "%lu\n", x[i]); else fprintf(stderr, "%lu ", x[i]); } fprintf(stderr, "u64 * => "); for (int i = 0; i < x_len; i++) { if (i == x_len - 1) fprintf(stderr, "%lu\n", from_m64(x[i], inv, mod)); else fprintf(stderr, "%lu ", from_m64(x[i], inv, mod)); } } #endif /* miller-rabin primary test */ bool miller_rabin32(u32 n, u32 d, const u32 *bases, int bases_len, m32 r2, m32 inv, m32 one, m32 rev) { for (int i = 0; i < bases_len; i++) { if (n <= bases[i]) break; m32 a = to_m32(bases[i], r2, inv, n); u32 t = d; m32 y = pow_m32(a, t, inv, n); while (t != n - 1 && y != one && y != rev) { y = mul_m32(y, y, inv, n); t <<= 1; } if (y != rev && (!(t & 1))) return false; } return true; } bool is_prime32(u32 n) { u32 m = n - 1; m32 r2 = _r2_m32(n); m32 inv = _inv_m32(n); m32 one = _one_m32(n); m32 rev = to_m32(m, r2, inv, n); u32 d = m >> CTZ(m); const u32 bases[] = { 2u, 7u, 61u }; return miller_rabin32(n, d, bases, 3, r2, inv, one, rev); } bool miller_rabin64(u64 n, u64 d, const u64 *bases, int bases_len, m64 r2, m64 inv, m64 one, m64 rev) { for (int i = 0; i < bases_len; i++) { if (n <= bases[i]) break; m64 a = to_m64(bases[i], r2, inv, n); u64 t = d; m64 y = pow_m64(a, t, inv, n); while (t != n - 1 && y != one && y != rev) { y = mul_m64(y, y, inv, n); t <<= 1; } if (y != rev && (!(t & 1))) return false; } return true; } bool is_prime64(u64 n) { u64 m = n - 1; m64 r2 = _r2_m64(n); m64 inv = _inv_m64(n); m64 one = _one_m64(n); m64 rev = to_m64(m, r2, inv, n); u64 d = m >> CTZ(m); const u64 bases[] = { 2ull, 325ull, 9375ull, 28178ull, 450775ull, 9780504ull, 1795265022ull }; return miller_rabin64(n, d, bases, 7, r2, inv, one, rev); } bool is_prime(u64 n) { if (n <= 3u) return n == 2u || n == 3u; if (!(n & 1)) return false; if (n < ((u32)1u << 31)) return is_prime32((u32)n); return is_prime64(n); } void Main(void) { int Q = read_int(); while (Q--) { u64 x = in_u64(); out_u64(x); SP(); write_int(is_prime(x)); NL(); } } int main(void) { Main(); return 0; }