#include #include const uint64_t bases[] = {2,325,9375,28178,450775,9780504,1795265022}; uint64_t modpow(uint64_t b, uint64_t p, uint64_t n) { if (p == 2) { return (uint64_t)(((__uint128_t)b) * ((__uint128_t)b) % ((__uint128_t)n)); } uint64_t r = ((p & 1) == 0) ? 1 : b; for (p >>= 1; p != 0; p >>= 1) { b = (uint64_t)(((__uint128_t)b) * ((__uint128_t)b) % ((__uint128_t)n)); if ((p & 1) == 1) { r = (uint64_t)(((__uint128_t)r) * ((__uint128_t)b) % ((__uint128_t)n)); } } return r; } int miller_rabin(uint64_t n) { if (n == 2) { return 1; } if (n < 2 || (n & 1) == 0) { return 0; } uint64_t n1 = n - 1, d = n - 1; int s = 0; while ((d & 1) == 0) { d >>= 1; s += 1; } for (int i = 0; i < 7; ++i) { uint64_t a = bases[i] % n; if (a == 0) { continue; } uint64_t t = modpow(a, d, n); if (t == 1 || t == n1) { continue; } for (int j = 1; j < s; ++j) { t = modpow(t, 2, n); if (t == n1) { goto cont; } } return 0; cont: continue; } return 1; } int main(int argc, char *argv[]) { int n; scanf("%d", &n); for(int i = 0; i < n; ++i) { int64_t x; scanf("%lld", &x); printf("%lld %d\n", x, miller_rabin(x)); } }