#include #include uint64_t oneM(uint64_t m) { return (uint64_t)-1ull % m + 1; } uint64_t r2M(uint64_t m) { return (__uint128_t)(__int128_t)-1 % m + 1; } uint64_t nM(uint64_t m) { uint64_t N = m; for (int _ = 0; _ < 5; ++_) N *= 2 - N * m; return N; } uint64_t reduceM(__uint128_t a, uint64_t N, uint64_t m) { uint64_t y = (uint64_t)(a >> 64) - (uint64_t)(((__uint128_t)((uint64_t)a * N) * m) >> 64); return (int64_t)y < 0 ? y + m : y; } uint64_t powM(uint64_t a, int64_t n, uint64_t one, uint64_t N, uint64_t m) { uint64_t ret = one; while (n > 0) { if (n & 1) ret = reduceM((__uint128_t)ret * a, N, m); a = reduceM((__uint128_t)a * a, N, m); n >>= 1; } return ret; } static uint64_t x = 0; static uint64_t w = 0; static uint64_t s = 0xb5ad4eceda1ce2a9; inline static uint32_t next_msws(void) { x *= x; x += (w += s); return x = (x >> 32) | (x << 32); } uint32_t range_msws(uint32_t l, uint32_t r) { return next_msws() % (r - l + 1) + l; } uint64_t bin_gcd(uint64_t a, uint64_t b) { if (!a || !b) return a | b; uint64_t s = __builtin_ctzll(a | b); uint64_t t; a >>= __builtin_ctzll(a); do { b >>= __builtin_ctzll(b); if (a > b) t = a, a = b, b = a; b -= a; } while (b); return a << s; } int jacobi(int64_t a, uint64_t n) { uint64_t t; int j = 1; while (a) { if (a < 0) { a = -a; if ((n & 3) == 3) j = -j; } int s = __builtin_ctzll(a); a >>= s; if (((n & 7) == 3 || (n & 7) == 5) && (s & 1)) j = -j; if ((a & n & 3) == 3) j = -j; t = a, a = n, n = t; a %= n; if (a > n / 2) a -= n; } return n == 1 ? j : 0; } int is_prime(uint64_t n) { if (n <= 1) return 0; if (n <= 3) return 1; if (!(n & 1)) return 0; const uint64_t mod = n; const uint64_t r2 = r2M(n); const uint64_t N = nM(n); const uint64_t one = oneM(n); const uint64_t rev = reduceM((__uint128_t)(n - 1) * r2, N, n); for (int _ = 0; _ < 7; ++_) { uint32_t a = range_msws(2u, ((n - 1) > ((1ull << 32) - 1)) ? 1u << 31 : n - 1); int x = jacobi(a, n); uint64_t y = (x == -1) ? rev : ((x == 0) ? 0 : one); if (y == 0 || y != powM(reduceM((__uint128_t)a * r2, N, mod), (mod - 1) / 2, one, N, mod)) return 0; } return 1; } uint64_t in(void) { uint64_t 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(uint64_t x) { if (x >= 10) out(x / 10); putchar_unlocked(x - x / 10 * 10 + 48); } int main(void) { uint64_t T = in(); while (T--) { uint64_t x = in(); out(x); putchar_unlocked(' '); out(is_prime(x)); putchar_unlocked('\n'); } return 0; }