#line 1 "main.cpp" #define PROBLEM "https://yukicoder.me/problems/no/3030" #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; const long double pi = acos(-1); constexpr uint64_t MOD = 1e9 + 7; constexpr uint64_t MOD2 = 998244353; constexpr int dx[] = { 1,0,-1,0 }; constexpr int dy[] = { 0,1,0,-1 }; templatestatic constexpr inline void chmax(T&x,T y){if(xstatic constexpr inline void chmin(T&x,T y){if(x>y)x=y;} #line 2 "math/barrett.hpp" namespace internal { ///@brief barrett reduction class barrett { using u32 = uint32_t; using u64 = uint64_t; u32 m; u64 im; public: explicit barrett() = default; explicit barrett(const u32& m_) :m(m_), im((u64)(-1) / m_ + 1) {} u32 get_mod() const { return m; } u32 mul(u32 a, u32 b) { if (a == 0 || b == 0) { return 0; } u64 z = a; z *= b; #ifdef _MSC_VER u64 x; _umul128(z, im, &x); #else u64 x = (u64)(((__uint128_t)(z)*im) >> 64); #endif u32 v = (u32)(z - x * m); if (v >= m)v += m; return v; } }; } #line 2 "math/montgomery.hpp" namespace internal { using u32 = uint32_t; using u64 = uint64_t; using i32 = int32_t; using i64 = int64_t; using u128 = __uint128_t; using i128 = __int128_t; /// @brief MontgomeryReduction template class MontgomeryReduction64 { static constexpr int lg = numeric_limits::digits; T mod, r, r2, minv; T calc_inv() { T t = 0, res = 0; for (int i = 0; i < lg; i++) { if (~t & 1) { t += mod; res += static_cast(1) << i; } t >>= 1; } return res; } public: MontgomeryReduction64() = default; constexpr T get_mod() { return mod; } constexpr int get_lg() { return lg; } void set_mod(const T& m) { assert(m > 0); assert(m & 1); mod = m; r = (-static_cast(mod)) % mod; r2 = (-static_cast(mod)) % mod; minv = calc_inv(); } T reduce(LargeT x) const { u64 res = (x + static_cast(static_cast(x) * minv) * mod) >> lg; if (res >= mod)res -= mod; return res; } T generate(LargeT x) { return reduce(x * r2); } T mult(T x, T y) { return reduce(static_cast(x) * y); } }; }; #line 4 "math/dynamic_modint.hpp" template class dynamic_modint32 { using u32 = uint32_t; using u64 = uint64_t; using i32 = int32_t; using i64 = int64_t; using br = internal::barrett; static br brt; static u32 mod; u32 v; //value public: static void set_mod(u32 mod_) { brt = br(mod_); mod = mod_; } private: constexpr u32 normalize(i64 x) const { i32 m = x % mod; if (m < 0) { m += mod; } return m; } public: explicit constexpr dynamic_modint32() :v(0) { assert(mod); } //modが決定済みである必要がある explicit constexpr dynamic_modint32(i64 v_) :v(normalize(v_)) { assert(mod); } u32 val() const { return v; } static u32 get_mod() { return mod; } using mint = dynamic_modint32; //operators constexpr mint& operator=(i64 r) { v = normalize(r); return (*this); } constexpr mint& operator+=(const mint& r) { v += r.v; if (v >= mod) { v -= mod; } return (*this); } constexpr mint& operator-=(const mint&r) { v += mod - r.v; if (v >= mod) { v -= mod; } return (*this); } constexpr mint& operator*=(const mint& r) { v = brt.mul(v, r.v); return (*this); } constexpr mint operator+(const mint& r) const { return mint(*this) += r; } constexpr mint operator-(const mint& r) const { return mint(*this) -= r; } constexpr mint operator*(const mint& r) const { return mint(*this) *= r; } constexpr mint& operator+= (i64 r) { return (*this) += mint(r); } constexpr mint& operator-= (i64 r) { return (*this) -= mint(r); } constexpr mint& operator*= (i64 r) { return (*this) *= mint(r); } friend mint operator+(i64 l, const mint& r) { return mint(l) += r; } friend mint operator+(const mint& l, i64 r) { return mint(l) += r; } friend mint operator-(i64 l, const mint& r) { return mint(l) -= r; } friend mint operator-(const mint& l, i64 r) { return mint(l) -= r; } friend mint operator*(i64 l, const mint& r) { return mint(l) *= r; } friend mint operator*(const mint& l, i64 r) { return mint(l) += r; } friend ostream& operator<<(ostream& os, const mint& mt) { os << mt.val(); return os; } friend istream& operator>>(istream& is, mint& mt) { i64 v_; is >> v_; mt = v_; return is; } constexpr mint pow(u64 e) const { mint res(1), base(*this); while (e) { if (e & 1) { res *= base; } e >>= 1; base *= base; } return res; } constexpr mint inv() const { return pow(mod - 2); } mint& operator/=(const mint& r) { return (*this) *= r.inv(); } mint operator/(const mint& r) const { return mint(*this) *= r.inv(); } mint& operator/=(const i64& r) { return (*this) /= mint(r); } friend mint operator/(const mint& l, const i64& r) { return mint(l) /= r; } friend mint operator/(const i64& l, const mint& r) { return mint(l) /= r; } }; templatetypename dynamic_modint32::u32 dynamic_modint32::mod; templatetypename dynamic_modint32::br dynamic_modint32::brt; /// @brief dynamic_modint(64bit) /// @tparam T 型(64bit,32bitなど) /// @tparam LargeT 積が収まってくれる型 /// @note T,LargeTのいずれもunsignedで渡す template class dynamic_modint { static T mod; static internal::MontgomeryReduction64 mr; public: static void set_mod(T mod_) { mr.set_mod(mod_); mod = mod_; } static T get_mod() { return mod; } private: T v; public: dynamic_modint(T v_ = 0) { assert(mod); v = mr.generate(v_); } T val() const { return mr.reduce(v); } using mint = dynamic_modint; mint& operator+=(const mint& r) { v += r.v; if (v >= mr.get_mod()) { v -= mr.get_mod(); } return (*this); } mint& operator-=(const mint& r) { v += mr.get_mod() - r.v; if (v >= mr.get_mod) { v -= mr.get_mod(); } return (*this); } mint& operator*=(const mint& r) { v = mr.mult(v, r.v); return (*this); } mint operator+(const mint& r) { return mint(*this) += r; } mint operator-(const mint& r) { return mint(*this) -= r; } mint operator*(const mint& r) { return mint(*this) *= r; } mint& operator=(const T& v_) { (*this) = mint(v_); return (*this); } friend ostream& operator<<(ostream& os, const mint& mt) { os << mt.val(); return os; } friend istream& operator>>(istream& is, mint& mt) { T v_; is >> v_; mt = v_; return is; } template mint pow(P e) const { assert(e >= 0); mint res(1), base(*this); while (e) { if (e & 1) { res *= base; } e >>= 1; base *= base; } return res; } mint inv() const { return pow(mod - 2); } mint& operator/=(const mint& r) { return (*this) *= r.inv(); } mint operator/(const mint& r) const { return mint(*this) *= r.inv(); } mint& operator/=(T r) { return (*this) /= mint(r); } friend mint operator/(const mint& l, T r) { return mint(l) /= r; } friend mint operator/(T l, const mint& r) { return mint(l) /= r; } }; templateT dynamic_modint::mod; templateinternal::MontgomeryReduction64 dynamic_modint::mr; ///@brief dynamic modint(動的modint) ///@docs docs/math/dynamic_modint.md #line 3 "math/miller.hpp" namespace prime { namespace miller { using i128 = __int128_t; using u128 = __uint128_t; using u64 = uint64_t; using u32 = uint32_t; template bool inline miller_rabin(u64 n, const u64 bases[], int length) { u64 d = n - 1; while (~d & 1) { d >>= 1; } u64 rev = n - 1; if (mint::get_mod() != n) { mint::set_mod(n); } for (int i = 0; i < length; i++) { u64 a = bases[i]; if (n <= a) { return true; } u64 t = d; mint y = mint(a).pow(t); while (t != n - 1 && y.val() != 1 && y.val() != rev) { y *= y; t <<= 1; } if (y.val() != rev && (~t & 1))return false; } return true; } constexpr u64 bases_int[3] = { 2, 7, 61 }; // intだと、2,7,61で十分 constexpr u64 bases_ll[7] = { 2, 325, 9375, 28178, 450775, 9780504, 1795265022 }; constexpr bool inline is_prime(u64 n) { if (n < 2) { return false; } else if (n == 2) { return true; } else if (~n & 1) { return false; } if (n < (1ul << 31)) { return miller_rabin>(n, bases_int, 3); } else { return miller_rabin>(n, bases_ll, 7); } } }; }; ///@brief MillerRabinの素数判定 #line 4 "main.cpp" int main(){ int n; scanf("%d", &n); for (int i = 0; i < n; i++){ uint64_t xi; scanf("%lld", &xi); printf("%lld ", xi); if (prime::miller::is_prime(xi)) { puts("1"); } else { puts("0"); } } }