#line 1 "test/01_Math/01_NumberTheory/02.01.03_yukicoder-3030.test.cpp" #define PROBLEM "https://yukicoder.me/problems/no/3030" #line 1 "template/template.hpp" #include #define int int64_t using namespace std; #line 3 "01_Math/02_Combinatorics/01.01.01_big-mod.hpp" /** * @brief 大きな mod 上の計算 * @note O(1) */ inline std::int64_t mod(std::int64_t a, std::int64_t m) { return (a % m + m) % m; } /** * @note O(\log{m}) */ inline std::int64_t mul(std::int64_t a, std::int64_t b, std::int64_t m) { a = mod(a, m), b = mod(b, m); std::int64_t res = 0; while (b) { if (b & 1) res = mod(res + a, m); a = mod(a + a, m); b >>= 1; } return res; } #line 3 "01_Math/02_Combinatorics/01.03.02_mod-pow.big-mod.hpp" /** * @brief 累乗 : $a^n\bmod{m}$ ($m$ が大きい場合) * @note O(\log{n}\log{m}) */ std::int64_t mod_pow(std::int64_t a, std::int64_t n, std::int64_t m) { a = (a % m + m) % m; std::int64_t res = 1; while (n) { if (n & 1) res = mul(res, a, m); a = mul(a, a, m); n >>= 1; } return res; } #line 5 "06_Others/04_Random/01_random-number.hpp" #include struct Random { std::mt19937_64 mt; Random() { mt.seed(std::chrono::steady_clock::now().time_since_epoch().count()); } } rnd; /** * @brief 乱数 (数) * @note O(1) */ template T random_number(const T a, const T b) { assert(a < b); if (std::is_integral::value) { std::uniform_int_distribution dist(a, b - 1); return dist(rnd.mt); } else { std::uniform_real_distribution<> dist(a, b); return dist(rnd.mt); } } /** * @note O(1) */ template T random_number(const T b) { return random_number(T(0), b); } #line 6 "01_Math/01_NumberTheory/02.01.03_is-prime.miller-rabin.hpp" /** * @brief 素数判定 (ミラー・ラビン) * @note O(k\log^3{n}) */ bool is_prime(std::uint64_t n, std::uint32_t k = 20) { if (n == 2) return true; if (n < 2 || !(n & 1)) return false; std::uint64_t d = n - 1; while (!(d & 1)) d >>= 1; for (std::uint32_t i = 0; i < k; ++i) { std::uint64_t a = random_number((std::uint64_t)1, n), t = d, y = mod_pow(a, t, n); while (t != n - 1 && y != 1 && y != n - 1) { y = mod_pow(y, 2, n); t <<= 1; } if (y != n - 1 && !(t & 1)) return false; } return true; } #line 4 "test/01_Math/01_NumberTheory/02.01.03_yukicoder-3030.test.cpp" signed main() { int n; cin >> n; for (int i = 0; i < n; ++i) { int x; cin >> x; cout << x << " " << is_prime(x) << endl; } }