#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" __int128_t mod_pow(__int128_t a, long long n, long long m) { a %= m; __int128_t res = 1; while (n) { if (n & 1) (res *= a) %= m; (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; } }