#line 2 "template.hpp" #include using namespace std; #define rep(i, N) for(int i=0;i<(N);i++) #define all(x) (x).begin(),(x).end() #define popcount(x) __builtin_popcount(x) using i128=__int128_t; using ll = long long; using ld = long double; using graph = vector>; using P = pair; constexpr int inf = 1e9; constexpr ll infl = 1e18; constexpr ld eps = 1e-6; constexpr long double pi = acos(-1); constexpr ll MOD = 1e9 + 7; constexpr ll MOD2 = 998244353; constexpr int dx[] = { 1,0,-1,0 }; constexpr int dy[] = { 0,1,0,-1 }; templateinline void chmax(T&x,T y){if(xinline void chmin(T&x,T y){if(x>y)x=y;} #line 2 "math/montgomery.hpp" class montgomery64 { using mint = montgomery64; using i64 = int64_t; using u64 = uint64_t; using u128 = __uint128_t; static u64 mod; static u64 r; static u64 n2; static u64 get_r() { u64 ret = mod; for (i64 i = 0; i < 5; ++i) ret *= 2 - mod * ret; return ret; } public: static void set_mod(u128 m) { assert(m < (1ll << 63)); assert((m & 1) == 1); mod = m; n2 = -u128(m) % m; r = get_r(); assert(r * mod == 1); } protected: i128 a; public: montgomery64() : a(0) {} template montgomery64(const T& b) : a(reduce((u128(b) + mod)* n2)) {}; private: template static u64 reduce(const T& b) { return (b + u128(u64(b) * u64(-r)) * mod) >> 64; } public: template mint& operator=(const T&rhs){ return (*this) = mint(rhs); } mint& operator+=(const mint& b) { if (i64(a += b.a - 2 * mod) < 0) a += 2 * mod; return *this; } mint& operator-=(const mint& b) { if (i64(a -= b.a) < 0) a += 2 * mod; return *this; } mint& operator*=(const mint& b) { a = reduce(u128(a) * b.a); return *this; } mint& operator/=(const mint& b) { *this *= b.inverse(); return *this; } mint operator+(const mint& b) const { return mint(*this) += b; } mint operator-(const mint& b) const { return mint(*this) -= b; } mint operator*(const mint& b) const { return mint(*this) *= b; } mint operator/(const mint& b) const { return mint(*this) /= b; } bool operator==(const mint& b) const { return (a >= mod ? a - mod : a) == (b.a >= mod ? b.a - mod : b.a); } bool operator!=(const mint& b) const { return (a >= mod ? a - mod : a) != (b.a >= mod ? b.a - mod : b.a); } mint operator-() const { return mint() - mint(*this); } mint pow(u128 n) const { mint ret(1), mul(*this); while (n > 0) { if (n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream& operator<<(ostream& os, const mint& b) { return os << b.get(); } friend istream& operator>>(istream& is, mint& b) { int64_t t; is >> t; b = montgomery64(t); return (is); } mint inverse() const { return pow(mod - 2); } u64 get() const { u64 ret = reduce(a); return ret >= mod ? ret - mod : ret; } static u64 get_mod() { return mod; } }; typename montgomery64::u64 montgomery64::mod, montgomery64::r, montgomery64::n2; /// @brief Montgomery ///by https://nyaannyaan.github.io/library/modint/modint-montgomery64.hpp #line 3 "main.test.cpp" namespace fast_prime{ //fast_is_prime using u64 = uint64_t; using mint = montgomery64; namespace miller_rabin{ const vector bases_int = {2, 7, 61}; const vector bases_long = {2, 325, 9375, 28178, 450775, 9780504, 1795265022}; bool miller_rabin(u64 n, const vector &bases){ if (mint::get_mod() != n){ mint::set_mod(n); } u64 d = n ^ 1uL; uint q = __builtin_ctz(d); d >>= q; mint e = 1, r = n - 1; for (const auto &a : bases){ if (a == n){ return true; } else if (n % a == 0){ return false; } i128 pw = mint(a).pow(n).get(); if (pw == 1){ continue; } bool is_prime_flag = true; for (int r = 0; r < q; r++){ pw = mint(a).pow((i128)(1) * d * (1uL << r)).get(); if (pw == n - 1){ is_prime_flag = false; break; } } if (is_prime_flag){ return false; } } return true; } bool is_prime(u64 n){ if (n < 2){ return false; } else if (n == 2){ return true; } else if (~n & 1uL){ return false; } if (n < (1ll << 30)){ return miller_rabin(n, bases_int); } else{ return miller_rabin(n, bases_long); } } }; }; using fast_prime::miller_rabin::is_prime; int main(){ int n; scanf("%d", &n); while (n--){ long long x; scanf("%lld",&x); printf("%lld %d\n", x, is_prime(x)); } }