結果
問題 | No.3030 ミラー・ラビン素数判定法のテスト |
ユーザー | AC2K |
提出日時 | 2023-07-25 10:17:48 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 36 ms / 9,973 ms |
コード長 | 15,007 bytes |
コンパイル時間 | 955 ms |
コンパイル使用メモリ | 84,828 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-10-02 04:43:50 |
合計ジャッジ時間 | 1,759 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 2 ms
5,248 KB |
testcase_04 | AC | 26 ms
5,248 KB |
testcase_05 | AC | 23 ms
5,248 KB |
testcase_06 | AC | 12 ms
5,248 KB |
testcase_07 | AC | 12 ms
5,248 KB |
testcase_08 | AC | 12 ms
5,248 KB |
testcase_09 | AC | 36 ms
5,248 KB |
ソースコード
#line 1 "test/yuki/No3030.test.cpp" #define PROBLEM "https://yukicoder.me/problems/no/3030" #include <iostream> #line 2 "src/math/dynamic_modint.hpp" #include <cassert> #line 2 "src/internal/barrett.hpp" #include <cstdint> namespace kyopro { namespace internal { /** * @brief Barrett Reduction */ class barrett { using u32 = uint32_t; using u64 = uint64_t; using u128 = __uint128_t; u32 m; u64 im; public: constexpr explicit barrett() : m(0), im(0) {} constexpr explicit barrett(u32 m) : m(m), im(static_cast<u64>(-1) / m + 1) {} constexpr u32 get_mod() const { return m; } constexpr u32 reduce(u32 a) const { return mul(1, a); } constexpr u32 mul(u32 a, u32 b) const { u64 z = (u64)a * b; u64 x = (u64)(((u128)(z)*im) >> 64); u64 y = x * m; return (u32)(z - y + (z < y ? m : 0)); } }; }; // namespace internal }; // namespace kyopro /** * @ref * https://github.com/atcoder/ac-library/blob/master/atcoder/internal_math.hpp */ #line 3 "src/internal/montgomery.hpp" #include <limits> #include <numeric> #line 5 "src/internal/type_traits.hpp" #include <typeinfo> namespace kyopro { namespace internal { /* * @ref https://qiita.com/kazatsuyu/items/f8c3b304e7f8b35263d8 */ template <typename... Args> struct first_enabled {}; template <typename T, typename... Args> struct first_enabled<std::enable_if<true, T>, Args...> { using type = T; }; template <typename T, typename... Args> struct first_enabled<std::enable_if<false, T>, Args...> : first_enabled<Args...> {}; template <typename T, typename... Args> struct first_enabled<T, Args...> { using type = T; }; template <typename... Args> using first_enabled_t = typename first_enabled<Args...>::type; template <int dgt> struct int_least { static_assert(dgt <= 128); using type = first_enabled_t<std::enable_if<dgt <= 8, __int8_t>, std::enable_if<dgt <= 16, __int16_t>, std::enable_if<dgt <= 32, __int32_t>, std::enable_if<dgt <= 64, __int64_t>, std::enable_if<dgt <= 128, __int128_t> >; }; template <int dgt> struct uint_least { static_assert(dgt <= 128); using type = first_enabled_t<std::enable_if<dgt <= 8, __uint8_t>, std::enable_if<dgt <= 16, __uint16_t>, std::enable_if<dgt <= 32, __uint32_t>, std::enable_if<dgt <= 64, __uint64_t>, std::enable_if<dgt <= 128, __uint128_t> >; }; template <int dgt> using int_least_t = typename int_least<dgt>::type; template <int dgt> using uint_least_t = typename uint_least<dgt>::type; template <typename T> using double_size_uint_t = uint_least_t<2 * std::numeric_limits<T>::digits>; template <typename T> using double_size_int_t = int_least_t<2 * std::numeric_limits<T>::digits>; }; // namespace internal }; // namespace kyopro #line 6 "src/internal/montgomery.hpp" namespace kyopro { 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 Montgomery Reduction */ template <typename T> class Montgomery { static constexpr int lg = std::numeric_limits<T>::digits; using LargeT = internal::double_size_uint_t<T>; T mod, r, r2, minv; T inv() { T t = 0, res = 0; for (int i = 0; i < lg; ++i) { if (~t & 1) { t += mod; res += static_cast<T>(1) << i; } t >>= 1; } return res; } public: Montgomery() = default; constexpr T get_mod() { return mod; } void set_mod(T m) { assert(m); assert(m & 1); mod = m; r = (-static_cast<T>(mod)) % mod; r2 = (-static_cast<LargeT>(mod)) % mod; minv = inv(); } T reduce(LargeT x) const { u64 res = (x + static_cast<LargeT>(static_cast<T>(x) * minv) * mod) >> lg; if (res >= mod) res -= mod; return res; } T generate(LargeT x) { return reduce(x * r2); } T mul(T x, T y) { return reduce((LargeT)x * y); } }; }; // namespace internal }; // namespace kyopro #line 6 "src/math/dynamic_modint.hpp" namespace kyopro { template <int id = -1> class barrett_modint { using mint = barrett_modint<id>; using u32 = uint32_t; using u64 = uint64_t; using i32 = int32_t; using i64 = int64_t; using br = internal::barrett; static br brt; u32 v; public: static void set_mod(u32 mod_) { brt = br(mod_); } public: explicit constexpr barrett_modint() : v(0) { assert(mod()); } explicit constexpr barrett_modint(i64 v_) : v() { assert(mod()); if (v_ < 0) v_ = (i64)mod() - v_; v = brt.reduce(v_); } u32 val() const { return v; } static u32 mod() { return brt.get_mod(); } static mint raw(u32 v) { mint x; x.v = v; return x; } constexpr mint& operator++() { ++v; if (v == mod()) v = 0; return (*this); } constexpr mint& operator--() { if (v == 0) v = mod(); --v; return (*this); } constexpr mint operator++(int) { mint res(*this); ++(*this); return res; } constexpr mint operator--(int) { mint res(*this); --(*this); return res; } 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) { return (*this) *= r.inv(); } friend mint operator+(const mint& lhs, const mint& rhs) { return mint(lhs) += rhs; } friend mint operator-(const mint& lhs, const mint& rhs) { return mint(lhs) -= rhs; } friend mint operator*(const mint& lhs, const mint& rhs) { return mint(lhs) *= rhs; } friend mint operator/(const mint& lhs, const mint& rhs) { return mint(lhs) /= rhs; } friend bool operator==(const mint& lhs, const mint& rhs) { return lhs._v == rhs._v; } friend bool operator!=(const mint& lhs, const mint& rhs) { return lhs._v != rhs._v; } 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; } constexpr mint operator+() const { return *this; } constexpr mint operator-() const { return mint() - *this; } friend std::ostream& operator<<(std::ostream& os, const mint& mt) { os << mt.val(); return os; } friend std::istream& operator>>(std::istream& is, mint& mt) { i64 v_; is >> v_; mt = mint(v_); return is; } template <typename T> mint pow(T 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); } }; }; // namespace kyopro template <int id> typename kyopro::barrett_modint<id>::br kyopro::barrett_modint<id>::brt; namespace kyopro { template <typename T, int id = -1> class dynamic_modint { using LargeT = internal::double_size_uint_t<T>; static T _mod; static internal::Montgomery<T> mr; public: static void set_mod(T mod_) { mr.set_mod(mod_); _mod = mod_; } static T 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<T, id>; 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.mul(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 std::ostream& operator<<(std::ostream& os, const mint& mt) { os << mt.val(); return os; } friend std::istream& operator>>(std::istream& is, mint& mt) { T v_; is >> v_; mt = v_; return is; } template <typename P> 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; } }; }; // namespace kyopro template <typename T, int id> T kyopro::dynamic_modint<T, id>::_mod; template <typename T, int id> kyopro::internal::Montgomery<T> kyopro::dynamic_modint<T, id>::mr; /** * @brief 動的modint * @docs docs/math/dynamic_modint.md */ #line 3 "src/math/miller.hpp" namespace kyopro { /** * @brief MillerRabin素数判定法 */ class miller { using i128 = __int128_t; using u128 = __uint128_t; using u64 = uint64_t; using u32 = uint32_t; template <typename T, typename mint, const int bases[], int length> static constexpr bool miller_rabin(T n) { T d = n - 1; while (~d & 1) { d >>= 1; } const T rev = n - 1; if (mint::mod() != n) { mint::set_mod(n); } for (int i = 0; i < length; ++i) { if (n <= bases[i]) { return true; } T t = d; mint y = mint(bases[i]).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; } // 底 static constexpr int bases_int[3] = {2, 7, 61}; static constexpr int bases_ll[7] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022}; public: template <typename T> static constexpr bool is_prime(T n) { if (n < 2) { return false; } else if (n == 2) { return true; } else if (~n & 1) { return false; }; if (std::numeric_limits<T>::digits < 32 || n <= 1 << 30) { return miller_rabin<T, dynamic_modint<std::make_unsigned_t<T>>, bases_int, 3>(n); } else { return miller_rabin<T, dynamic_modint<std::make_unsigned_t<T>>, bases_ll, 7>(n); } return false; } }; }; // namespace kyopro /** * @docs docs/math/miller.md */ #line 2 "src/stream.hpp" #include <ctype.h> #include <stdio.h> #include <string> namespace kyopro { /** * 文字を1個読み込む */ inline char readchar() { char c = getchar_unlocked(); while (isspace(c)) c = getchar_unlocked(); return c; } /** * 整数の入出力 */ template <typename T> constexpr inline void readint(T& a) { a = 0; bool is_negative = false; char c = getchar_unlocked(); while (isspace(c)) { c = getchar_unlocked(); } if (c == '-') is_negative = true, c = getchar_unlocked(); while (isdigit(c)) { a = 10 * a + (c - '0'); c = getchar_unlocked(); } if (is_negative) a *= -1; } template <typename Head, typename... Tail> constexpr inline void readint(Head& head, Tail&... tail) { readint(head); readint(tail...); } template <typename T> void write_int(T a) { if (!a) { putchar_unlocked('0'); putchar_unlocked('\n'); return; } if (a < 0) putchar_unlocked('-'), a *= -1; char s[37]; int now = 37; while (a) { s[--now] = (char)'0' + a % 10; a /= 10; } while (now < 37) putchar_unlocked(s[now++]); } template <typename T> constexpr inline void putint(T a) { if (!a) { putchar_unlocked('0'); putchar_unlocked('\n'); return; } if (a < 0) putchar_unlocked('-'), a *= -1; char s[37]; int now = 37; while (a) { s[--now] = (char)'0' + a % 10; a /= 10; } while (now < 37) putchar_unlocked(s[now++]); putchar_unlocked('\n'); } template <typename Head, typename... Tail> constexpr inline void putint(Head head, Tail... tail) { putint(head); putint(tail...); } /** * 文字列の入出力 */ inline void readstr(std::string& str) { char c = getchar_unlocked(); while (isspace(c)) c = getchar_unlocked(); while (!isspace(c)) { str += c; c = getchar_unlocked(); } } inline void readstr(std::string& str,std::string& tail...) { readstr(str); readstr(tail); } inline void putstr(const std::string& str) { for (auto c : str) { putchar_unlocked(c); } putchar_unlocked('\n'); } inline void putstr(const std::string& str, const std::string& tail...) { putstr(str); putstr(tail); } }; // namespace kyopro /** * @brief fastIO */ #line 5 "test/yuki/No3030.test.cpp" int main() { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) { long long x; kyopro::readint(x); kyopro::write_int(x); putchar_unlocked(' '); putchar_unlocked(kyopro::miller::is_prime(x)?'1':'0'); putchar_unlocked('\n'); } }