#line 2 "library/other/template.hpp" #include #line 2 "library/template/macros.hpp" #line 4 "library/template/macros.hpp" #ifndef __COUNTER__ #define __COUNTER__ __LINE__ #endif #define OVERLOAD5(a, b, c, d, e, ...) e #define REP1_0(b, c) REP1_1(b, c) #define REP1_1(b, c) \ for (ll REP_COUNTER_##c = 0; REP_COUNTER_##c < (ll)(b); ++REP_COUNTER_##c) #define REP1(b) REP1_0(b, __COUNTER__) #define REP2(i, b) for (ll i = 0; i < (ll)(b); ++i) #define REP3(i, a, b) for (ll i = (ll)(a); i < (ll)(b); ++i) #define REP4(i, a, b, c) for (ll i = (ll)(a); i < (ll)(b); i += (ll)(c)) #define rep(...) OVERLOAD5(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__) #define RREP2(i, a) for (ll i = (ll)(a)-1; i >= 0; --i) #define RREP3(i, a, b) for (ll i = (ll)(a)-1; i >= (ll)(b); --i) #define RREP4(i, a, b, c) for (ll i = (ll)(a)-1; i >= (ll)(b); i -= (ll)(c)) #define rrep(...) OVERLOAD5(__VA_ARGS__, RREP4, RREP3, RREP2)(__VA_ARGS__) #define REPS2(i, b) for (ll i = 1; i <= (ll)(b); ++i) #define REPS3(i, a, b) for (ll i = (ll)(a) + 1; i <= (ll)(b); ++i) #define REPS4(i, a, b, c) for (ll i = (ll)(a) + 1; i <= (ll)(b); i += (ll)(c)) #define reps(...) OVERLOAD5(__VA_ARGS__, REPS4, REPS3, REPS2)(__VA_ARGS__) #define RREPS2(i, a) for (ll i = (ll)(a); i > 0; --i) #define RREPS3(i, a, b) for (ll i = (ll)(a); i > (ll)(b); --i) #define RREPS4(i, a, b, c) for (ll i = (ll)(a); i > (ll)(b); i -= (ll)(c)) #define rreps(...) OVERLOAD5(__VA_ARGS__, RREPS4, RREPS3, RREPS2)(__VA_ARGS__) #define each_for(...) for (auto&& __VA_ARGS__) #define each_const(...) for (const auto& __VA_ARGS__) #define all(v) std::begin(v), std::end(v) #define rall(v) std::rbegin(v), std::rend(v) #if __cpp_if_constexpr >= 201606L #define IF_CONSTEXPR constexpr #else #define IF_CONSTEXPR #endif #define IO_BUFFER_SIZE (1 << 17) #line 2 "library/template/alias.hpp" #line 4 "library/template/alias.hpp" using ll = long long; using uint = unsigned int; using ull = unsigned long long; using i128 = __int128_t; using u128 = __uint128_t; using ld = long double; using PLL = std::pair; template using prique = std::priority_queue, std::greater>; template struct infinity { static constexpr T value = std::numeric_limits::max() / 2; static constexpr T mvalue = std::numeric_limits::lowest() / 2; static constexpr T max = std::numeric_limits::max(); static constexpr T min = std::numeric_limits::lowest(); }; #if __cplusplus <= 201402L template constexpr T infinity::value; template constexpr T infinity::mvalue; template constexpr T infinity::max; template constexpr T infinity::min; #endif #if __cpp_variable_templates >= 201304L template constexpr T INF = infinity::value; #endif constexpr ll inf = infinity::value; constexpr ld EPS = 1e-8; constexpr ld PI = 3.1415926535897932384626; #line 2 "library/template/type_traits.hpp" #line 5 "library/template/type_traits.hpp" template struct function_traits_impl { using result_type = T; template using argument_type = typename std::tuple_element>::type; using argument_tuple = std::tuple; static constexpr std::size_t arg_size() { return sizeof...(Args); } }; template struct function_traits_helper; template struct function_traits_helper { using type = function_traits_impl; }; template struct function_traits_helper { using type = function_traits_impl; }; template struct function_traits_helper { using type = function_traits_impl; }; template struct function_traits_helper { using type = function_traits_impl; }; #if __cpp_noexcept_function_type >= 201510L template struct function_traits_helper { using type = function_traits_impl; }; template struct function_traits_helper { using type = function_traits_impl; }; template struct function_traits_helper { using type = function_traits_impl; }; template struct function_traits_helper { using type = function_traits_impl; }; #endif template using function_traits = typename function_traits_helper< decltype(&std::remove_reference::type::operator())>::type; template using function_result_type = typename function_traits::result_type; template using function_argument_type = typename function_traits::template argument_type; template using function_argument_tuple = typename function_traits::argument_tuple; template using is_signed_int = std::integral_constant::value && std::is_signed::value) || std::is_same::value>; template using is_unsigned_int = std::integral_constant::value && std::is_unsigned::value) || std::is_same::value>; template using is_int = std::integral_constant::value || is_unsigned_int::value>; template using make_signed_int = typename std::conditional< std::is_same::value || std::is_same::value, std::common_type, std::make_signed>::type; template using make_unsigned_int = typename std::conditional< std::is_same::value || std::is_same::value, std::common_type, std::make_unsigned>::type; template struct is_range : std::false_type {}; template struct is_range< T, decltype(all(std::declval::type>()), (void)0)> : std::true_type {}; template::value> struct range_rank : std::integral_constant {}; template struct range_rank : std::integral_constant::value + 1> {}; template struct int_least { static_assert(size <= 128, "size must be less than or equal to 128"); using type = typename std::conditional< size <= 8, std::int_least8_t, typename std::conditional< size <= 16, std::int_least16_t, typename std::conditional< size <= 32, std::int_least32_t, typename std::conditional::type>::type>::type>::type; }; template using int_least_t = typename int_least::type; template struct uint_least { static_assert(size <= 128, "size must be less than or equal to 128"); using type = typename std::conditional< size <= 8, std::uint_least8_t, typename std::conditional< size <= 16, std::uint_least16_t, typename std::conditional< size <= 32, std::uint_least32_t, typename std::conditional::type>::type>::type>::type; }; template using uint_least_t = typename uint_least::type; template using double_size_int = int_least::digits * 2 + 1>; template using double_size_int_t = typename double_size_int::type; template using double_size_uint = uint_least::digits * 2>; template using double_size_uint_t = typename double_size_uint::type; template using double_size = typename std::conditional::value, double_size_int, double_size_uint>::type; template using double_size_t = typename double_size::type; #line 2 "library/template/in.hpp" #line 4 "library/template/in.hpp" #include #line 8 "library/template/in.hpp" template class Scanner { private: template struct has_scan : std::false_type {}; template struct has_scan< T, decltype(std::declval().scan(std::declval()), (void)0)> : std::true_type {}; int fd; int idx, sz; bool state; std::array buffer; inline char cur() { if (idx == sz) load(); if (idx == sz) { state = false; return '\0'; } return buffer[idx]; } inline void next() { if (idx == sz) load(); if (idx == sz) return; ++idx; } public: inline void load() { int len = sz - idx; if (idx < len) return; std::memcpy(buffer.begin(), buffer.begin() + idx, len); sz = len + read(fd, buffer.data() + len, buf_size - len); buffer[sz] = 0; idx = 0; } Scanner(int fd) : fd(fd), idx(0), sz(0), state(true) {} Scanner(FILE* fp) : fd(fileno(fp)), idx(0), sz(0), state(true) {} inline char scan_char() { if (idx == sz) load(); return idx == sz ? '\0' : buffer[idx++]; } Scanner ignore(int n = 1) { if (idx + n > sz) load(); idx += n; return *this; } inline void discard_space() { if (idx == sz) load(); while (('\t' <= buffer[idx] && buffer[idx] <= '\r') || buffer[idx] == ' ') { if (++idx == sz) load(); } } void scan(char& a) { discard_space(); a = scan_char(); } void scan(bool& a) { discard_space(); a = scan_char() != '0'; } void scan(std::string& a) { discard_space(); a.clear(); while (cur() != '\0' && (buffer[idx] < '\t' || '\r' < buffer[idx]) && buffer[idx] != ' ') { a += scan_char(); } } template void scan(std::bitset& a) { discard_space(); if (idx + len > sz) load(); rrep (i, len) a[i] = buffer[idx++] != '0'; } template::value && !has_scan::value>::type* = nullptr> void scan(T& a) { discard_space(); if (buffer[idx] == '-') { ++idx; if (idx + 40 > sz && (idx == sz || ('0' <= buffer[sz - 1] && buffer[sz - 1] <= '9'))) load(); a = 0; while ('0' <= buffer[idx] && buffer[idx] <= '9') { a = a * 10 - (buffer[idx++] - '0'); } } else { if (idx + 40 > sz && '0' <= buffer[sz - 1] && buffer[sz - 1] <= '9') load(); a = 0; while ('0' <= buffer[idx] && buffer[idx] <= '9') { a = a * 10 + (buffer[idx++] - '0'); } } } template::value && !has_scan::value>::type* = nullptr> void scan(T& a) { discard_space(); if (idx + 40 > sz && '0' <= buffer[sz - 1] && buffer[sz - 1] <= '9') load(); a = 0; while ('0' <= buffer[idx] && buffer[idx] <= '9') { a = a * 10 + (buffer[idx++] - '0'); } } template::value && !has_scan::value>::type* = nullptr> void scan(T& a) { discard_space(); bool sgn = false; if (cur() == '-') { sgn = true; next(); } a = 0; while ('0' <= cur() && cur() <= '9') { a = a * 10 + cur() - '0'; next(); } if (cur() == '.') { next(); T n = 0, d = 1; for (int i = 0; '0' <= cur() && cur() <= '9' && i < (int)decimal_precision; ++i) { n = n * 10 + cur() - '0'; d *= 10; next(); } while ('0' <= cur() && cur() <= '9') next(); a += n / d; } if (sgn) a = -a; } private: template void scan(std::tuple& a) { if IF_CONSTEXPR (i < sizeof...(Args)) { scan(std::get(a)); scan(a); } } public: template void scan(std::tuple& a) { scan<0, Args...>(a); } template void scan(std::pair& a) { scan(a.first); scan(a.second); } template::value && !has_scan::value>::type* = nullptr> void scan(T& a) { for (auto&& i : a) scan(i); } template::value>::type* = nullptr> void scan(T& a) { a.scan(*this); } void operator()() {} template void operator()(Head& head, Args&... args) { scan(head); operator()(args...); } template Scanner& operator>>(T& a) { scan(a); return *this; } explicit operator bool() const { return state; } friend Scanner& getline(Scanner& scan, std::string& a) { a.erase(); char c; if ((c = scan.scan_char()) == '\n' || c == '\0') return scan; a += c; while ((c = scan.scan_char()) != '\n' && c != '\0') a += c; scan.state = true; return scan; } }; Scanner<> scan(0); #line 2 "library/template/out.hpp" #line 8 "library/template/out.hpp" struct NumberToString { char buf[10000][4]; constexpr NumberToString() : buf{} { rep (i, 10000) { int n = i; rrep (j, 4) { buf[i][j] = (char)('0' + n % 10); n /= 10; } } } } constexpr precalc_number_to_string; template class Printer { private: template struct has_print : std::false_type {}; template struct has_print().print(std::declval()), (void)0)> : std::true_type {}; template struct has_print().debug(std::declval()), (void)0)> : std::true_type {}; int fd; std::size_t idx; std::array buffer; std::size_t decimal_precision; public: inline void print_char(char c) { #if SHIO_LOCAL buffer[idx++] = c; if (idx == buf_size) flush(); #else if IF_CONSTEXPR (!debug) { buffer[idx++] = c; if (idx == buf_size) flush(); } #endif } inline void flush() { idx = write(fd, buffer.begin(), idx); idx = 0; } Printer(int fd) : fd(fd), idx(0), decimal_precision(16) {} Printer(FILE* fp) : fd(fileno(fp)), idx(0), decimal_precision(16) {} ~Printer() { flush(); } void set_decimal_precision(std::size_t decimal_precision) { this->decimal_precision = decimal_precision; } void print(char c) { if IF_CONSTEXPR (debug) print_char('\''); print_char(c); if IF_CONSTEXPR (debug) print_char('\''); } void print(bool b) { print_char((char)(b + '0')); } void print(const char* a) { if IF_CONSTEXPR (debug) print_char('"'); for (; *a != '\0'; ++a) print_char(*a); if IF_CONSTEXPR (debug) print_char('"'); } template void print(const char (&a)[len]) { if IF_CONSTEXPR (debug) print_char('"'); for (auto i : a) print_char(i); if IF_CONSTEXPR (debug) print_char('"'); } void print(const std::string& a) { if IF_CONSTEXPR (debug) print_char('"'); for (auto i : a) print_char(i); if IF_CONSTEXPR (debug) print_char('"'); } template void print(const std::bitset& a) { rrep (i, len) print_char((char)(a[i] + '0')); } template::value && !has_print::value>::type* = nullptr> void print(T a) { if (!a) { print_char('0'); return; } if IF_CONSTEXPR (is_signed_int::value) { if (a < 0) { print_char('-'); using U = typename make_unsigned_int::type; print(static_cast(-static_cast(a))); return; } } if (idx + 40 >= buf_size) flush(); static char s[40]; int t = 40; while (a >= 10000) { int i = a % 10000; a /= 10000; t -= 4; std::memcpy(s + t, precalc_number_to_string.buf[i], 4); } if (a >= 1000) { std::memcpy(buffer.begin() + idx, precalc_number_to_string.buf[a], 4); idx += 4; } else if (a >= 100) { std::memcpy(buffer.begin() + idx, precalc_number_to_string.buf[a] + 1, 3); idx += 3; } else if (a >= 10) { std::memcpy(buffer.begin() + idx, precalc_number_to_string.buf[a] + 2, 2); idx += 2; } else { buffer[idx++] = '0' | a; } std::memcpy(buffer.begin() + idx, s + t, 40 - t); idx += 40 - t; } template::value && !has_print::value>::type* = nullptr> void print(T a) { if (a == std::numeric_limits::infinity()) { print("inf"); return; } if (a == -std::numeric_limits::infinity()) { print("-inf"); return; } if (std::isnan(a)) { print("nan"); return; } if (a < 0) { print_char('-'); a = -a; } T b = a; if (b < 1) { print_char('0'); } else { std::string s; while (b >= 1) { s += (char)('0' + (int)std::fmod(b, 10.0)); b /= 10; } for (auto i = s.rbegin(); i != s.rend(); ++i) print_char(*i); } print_char('.'); rep (decimal_precision) { a *= 10; print_char((char)('0' + (int)std::fmod(a, 10.0))); } } private: template void print(const std::tuple& a) { if IF_CONSTEXPR (i < sizeof...(Args)) { if IF_CONSTEXPR (debug) print_char(','); print_char(' '); print(std::get(a)); print(a); } } public: template void print(const std::tuple& a) { if IF_CONSTEXPR (debug) print_char('('); if IF_CONSTEXPR (sizeof...(Args) != 0) print(std::get<0>(a)); print<1, Args...>(a); if IF_CONSTEXPR (debug) print_char(')'); } template void print(const std::pair& a) { if IF_CONSTEXPR (debug) print_char('('); print(a.first); if IF_CONSTEXPR (debug) print_char(','); print_char(' '); print(a.second); if IF_CONSTEXPR (debug) print_char(')'); } template::value && !has_print::value>::type* = nullptr> void print(const T& a) { if IF_CONSTEXPR (debug) print_char('{'); for (auto i = std::begin(a); i != std::end(a); ++i) { if (i != std::begin(a)) { if IF_CONSTEXPR (debug) print_char(','); print_char(' '); } print(*i); } if IF_CONSTEXPR (debug) print_char('}'); } template::value && !debug>::type* = nullptr> void print(const T& a) { a.print(*this); } template::value && debug>::type* = nullptr> void print(const T& a) { a.debug(*this); } void operator()() {} template void operator()(const Head& head, const Args&... args) { print(head); operator()(args...); } template Printer& operator<<(const T& a) { print(a); return *this; } Printer& operator<<(Printer& (*pf)(Printer&)) { return pf(*this); } }; template Printer& endl(Printer& pr) { pr.print_char('\n'); pr.flush(); return pr; } template Printer& flush(Printer& pr) { pr.flush(); return pr; } struct SetPrec { int n; template void print(Pr& pr) const { pr.set_decimal_precision(n); } template void debug(Pr& pr) const { pr.set_decimal_precision(n); } }; SetPrec setprec(int n) { return SetPrec{n}; }; Printer<> print(1), eprint(2); void prints() { print.print_char('\n'); } template auto prints(const T& v) -> decltype(print << v, (void)0) { print << v; print.print_char('\n'); } template auto prints(const Head& head, const Tail&... tail) -> decltype(print << head, (void)0) { print << head; print.print_char(' '); prints(tail...); } Printer debug(1), edebug(2); void debugs() { debug.print_char('\n'); } template auto debugs(const T& v) -> decltype(debug << v, (void)0) { debug << v; debug.print_char('\n'); } template auto debugs(const Head& head, const Tail&... tail) -> decltype(debug << head, (void)0) { debug << head; debug.print_char(' '); debugs(tail...); } #line 2 "library/template/bitop.hpp" #line 6 "library/template/bitop.hpp" namespace bitop { #define KTH_BIT(b, k) (((b) >> (k)) & 1) #define POW2(k) (1ull << (k)) inline ull next_combination(int n, ull x) { if (n == 0) return 1; ull a = x & -x; ull b = x + a; return (x & ~b) / a >> 1 | b; } #define rep_comb(i, n, k) \ for (ull i = (1ull << (k)) - 1; i < (1ull << (n)); \ i = bitop::next_combination((n), i)) inline constexpr int msb(ull x) { int res = x ? 0 : -1; if (x & 0xFFFFFFFF00000000) x &= 0xFFFFFFFF00000000, res += 32; if (x & 0xFFFF0000FFFF0000) x &= 0xFFFF0000FFFF0000, res += 16; if (x & 0xFF00FF00FF00FF00) x &= 0xFF00FF00FF00FF00, res += 8; if (x & 0xF0F0F0F0F0F0F0F0) x &= 0xF0F0F0F0F0F0F0F0, res += 4; if (x & 0xCCCCCCCCCCCCCCCC) x &= 0xCCCCCCCCCCCCCCCC, res += 2; return res + ((x & 0xAAAAAAAAAAAAAAAA) ? 1 : 0); } inline constexpr int ceil_log2(ull x) { return x ? msb(x - 1) + 1 : 0; } inline constexpr ull reverse(ull x) { x = ((x & 0xAAAAAAAAAAAAAAAA) >> 1) | ((x & 0x5555555555555555) << 1); x = ((x & 0xCCCCCCCCCCCCCCCC) >> 2) | ((x & 0x3333333333333333) << 2); x = ((x & 0xF0F0F0F0F0F0F0F0) >> 4) | ((x & 0x0F0F0F0F0F0F0F0F) << 4); x = ((x & 0xFF00FF00FF00FF00) >> 8) | ((x & 0x00FF00FF00FF00FF) << 8); x = ((x & 0xFFFF0000FFFF0000) >> 16) | ((x & 0x0000FFFF0000FFFF) << 16); return (x >> 32) | (x << 32); } inline constexpr ull reverse(ull x, int n) { return reverse(x) >> (64 - n); } } // namespace bitop inline constexpr int popcnt(ull x) noexcept { #if __cplusplus >= 202002L return std::popcount(x); #endif x = (x & 0x5555555555555555) + ((x >> 1) & 0x5555555555555555); x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333); x = (x & 0x0f0f0f0f0f0f0f0f) + ((x >> 4) & 0x0f0f0f0f0f0f0f0f); x = (x & 0x00ff00ff00ff00ff) + ((x >> 8) & 0x00ff00ff00ff00ff); x = (x & 0x0000ffff0000ffff) + ((x >> 16) & 0x0000ffff0000ffff); return (x & 0x00000000ffffffff) + ((x >> 32) & 0x00000000ffffffff); } #line 2 "library/template/func.hpp" #line 6 "library/template/func.hpp" template> inline constexpr bool chmin(T& a, const U& b, Comp cmp = Comp()) noexcept(noexcept(cmp(b, a))) { return cmp(b, a) ? a = b, true : false; } template> inline constexpr bool chmax(T& a, const U& b, Comp cmp = Comp()) noexcept(noexcept(cmp(a, b))) { return cmp(a, b) ? a = b, true : false; } inline constexpr ll gcd(ll a, ll b) { if (a < 0) a = -a; if (b < 0) b = -b; while (b) { const ll c = a; a = b; b = c % b; } return a; } inline constexpr ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } inline constexpr bool is_prime(ll N) { if (N <= 1) return false; for (ll i = 2; i * i <= N; ++i) { if (N % i == 0) return false; } return true; } inline std::vector prime_factor(ll N) { std::vector res; for (ll i = 2; i * i <= N; ++i) { while (N % i == 0) { res.push_back(i); N /= i; } } if (N != 1) res.push_back(N); return res; } inline constexpr ll my_pow(ll a, ll b) { ll res = 1; while (b) { if (b & 1) res *= a; b >>= 1; a *= a; } return res; } inline constexpr ll mod_pow(ll a, ll b, ll mod) { assert(mod > 0); if (mod == 1) return 0; a %= mod; ll res = 1; while (b) { if (b & 1) (res *= a) %= mod; b >>= 1; (a *= a) %= mod; } return res; } inline PLL extGCD(ll a, ll b) { const ll n = a, m = b; ll x = 1, y = 0, u = 0, v = 1; ll t; while (b) { t = a / b; std::swap(a -= t * b, b); std::swap(x -= t * u, u); std::swap(y -= t * v, v); } if (x < 0) { x += m; y -= n; } return {x, y}; } inline ll mod_inv(ll a, ll mod) { ll b = mod; ll x = 1, u = 0; ll t; while (b) { t = a / b; std::swap(a -= t * b, b); std::swap(x -= t * u, u); } if (x < 0) x += mod; assert(a == 1); return x; } #line 2 "library/template/util.hpp" #line 6 "library/template/util.hpp" template class RecLambda { private: F f; public: explicit constexpr RecLambda(F&& f_) : f(std::forward(f_)) {} template constexpr auto operator()(Args&&... args) -> decltype(f(*this, std::forward(args)...)) { return f(*this, std::forward(args)...); } }; template inline constexpr RecLambda rec_lambda(F&& f) { return RecLambda(std::forward(f)); } template struct multi_dim_vector { using type = std::vector::type>; }; template struct multi_dim_vector { using type = T; }; template constexpr std::vector make_vec(int n, Arg&& arg) { return std::vector(n, std::forward(arg)); } template constexpr typename multi_dim_vector::type make_vec(int n, Args&&... args) { return typename multi_dim_vector::type( n, make_vec(std::forward(args)...)); } template> class compressor { private: std::vector dat; Comp cmp; bool sorted = false; public: compressor() : compressor(Comp()) {} compressor(const Comp& cmp) : cmp(cmp) {} compressor(const std::vector& vec, bool f = false, const Comp& cmp = Comp()) : dat(vec), cmp(cmp) { if (f) build(); } compressor(std::vector&& vec, bool f = false, const Comp& cmp = Comp()) : dat(std::move(vec)), cmp(cmp) { if (f) build(); } compressor(std::initializer_list il, bool f = false, const Comp& cmp = Comp()) : dat(all(il)), cmp(cmp) { if (f) build(); } void reserve(int n) { assert(!sorted); dat.reserve(n); } void push_back(const T& v) { assert(!sorted); dat.push_back(v); } void push_back(T&& v) { assert(!sorted); dat.push_back(std::move(v)); } template void emplace_back(Args&&... args) { assert(!sorted); dat.emplace_back(std::forward(args)...); } void push(const std::vector& vec) { assert(!sorted); const int n = dat.size(); dat.resize(n + vec.size()); rep (i, vec.size()) dat[n + i] = vec[i]; } int build() { assert(!sorted); sorted = true; std::sort(all(dat), cmp); dat.erase(std::unique(all(dat), [&](const T& a, const T& b) -> bool { return !cmp(a, b) && !cmp(b, a); }), dat.end()); return dat.size(); } const T& operator[](int k) const& { assert(sorted); assert(0 <= k && k < (int)dat.size()); return dat[k]; } int get(const T& val) const { assert(sorted); auto itr = std::lower_bound(all(dat), val, cmp); assert(itr != dat.end() && !cmp(val, *itr)); return itr - dat.begin(); } int lower_bound(const T& val) const { assert(sorted); auto itr = std::lower_bound(all(dat), val, cmp); return itr - dat.begin(); } int upper_bound(const T& val) const { assert(sorted); auto itr = std::upper_bound(all(dat), val, cmp); return itr - dat.begin(); } bool contains(const T& val) const { assert(sorted); return std::binary_search(all(dat), val, cmp); } std::vector pressed(const std::vector& vec) const { assert(sorted); std::vector res(vec.size()); rep (i, vec.size()) res[i] = get(vec[i]); return res; } void press(std::vector& vec) const { assert(sorted); for (auto&& i : vec) i = get(i); } int size() const { assert(sorted); return dat.size(); } }; #line 2 "main.cpp" using namespace std; int main() { int A, B; scan >> A >> B; int N = A * A + B * B; set st1, st2; rep (i, N) rep (j, A + B) st1.emplace((i * A - B * j % N + N) % N, (i * B + j * A) % N); rep (i, N) rep (j, A + B) st2.emplace(((i + 1) * A - B * (j + 1) % N + N) % N, (i * B + (j + 1) * A) % N); for (auto [a, b] : st1) prints(a, b); for (auto [a, b] : st2) prints(a, b); }