/* * @uni_kakurenbo * https://github.com/uni-kakurenbo/competitive-programming-workspace * * CC0 1.0 http://creativecommons.org/publicdomain/zero/1.0/deed.ja */ /* #language C++ 20 GCC */ /* [begin]: template/debug.hpp */ /* [begin]: debugger/debug.hpp */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* [begin]: numeric/int128.hpp */ #include #include #include /* [begin]: snippet/internal/types.hpp */ #include namespace uni { using i16 = std::int16_t; using u16 = std::uint16_t; using i32 = std::int32_t; using u32 = std::uint32_t; using i64 = std::int64_t; using u64 = std::uint64_t; #ifdef __GNUC__ using i128 = __int128_t; using u128 = __uint128_t; #endif using uint = unsigned; using ll = long long; using ull = unsigned long long; using ld = long double; } /* [end]: snippet/internal/types.hpp*/ /* [begin]: snippet/iterations.hpp */ /* [begin]: macro/overload.hpp */ #define $OVERLOAD2(arg0, arg1, cmd, ...) cmd #define $OVERLOAD3(arg0, arg1, arg2, cmd, ...) cmd #define $OVERLOAD4(arg0, arg1, arg2, arg3, cmd, ...) cmd #define $OVERLOAD5(arg0, arg1, arg2, arg3, arg4, cmd, ...) cmd #define $OVERLOAD6(arg0, arg1, arg2, arg3, arg4, arg5, cmd, ...) cmd /* [end]: macro/overload.hpp*/ #define LOOP(n) REPI(_$, (n)) #define REPI(i,n) for(std::decay_t i=0, i##_length=(n); i,std::decay_t> i=(l), i##_last=(r); i,std::decay_t,std::decay_t> i=(l), i##_last=(r); i=0;) #define REPB(i,l,r) for(std::common_type_t,std::decay_t> i=(r), i##_last=(l); --i>=i##_last;) #define REPRS(i,l,r,s) for(std::common_type_t,std::decay_t,std::decay_t> i=(l)+((r)-(l)-1)/(s)*(s), i##_last=(l); i>=i##_last; (i-=(s))) #define REP(...) $OVERLOAD4(__VA_ARGS__, REPIS, REPF, REPI, LOOP)(__VA_ARGS__) #define REPD(...) $OVERLOAD4(__VA_ARGS__, REPRS, REPB, REPR)(__VA_ARGS__) #define FORO(i,n) for(int i=0, i##_last=static_cast(n); i<=i##_last; ++i) #define FORI(i,l,r) for(std::common_type_t,std::decay_t> i=(l), i##_last=(r); i<=i##_last; ++i) #define FORIS(i,l,r,s) for(std::common_type_t,std::decay_t,std::decay_t> i=(l), i##_last=(r); i<=i##_last; i+=(s)) #define FORRO(i,n) for(auto i=(n); i>=0; --i) #define FORR(i,l,r) for(std::common_type_t,std::decay_t> i=(r), i##_last=(l); i>=i##_last; --i) #define FORRS(i,l,r,s) for(std::common_type_t,std::decay_t,std::decay_t> i=(l)+((r)-(l))/(s)*(s), i##_last=(l); i>=i##_last; i-=(s)) #define FOR(...) $OVERLOAD4(__VA_ARGS__, FORIS, FORI, FORO)(__VA_ARGS__) #define FORD(...) $OVERLOAD4(__VA_ARGS__, FORRS, FORR, FORRO)(__VA_ARGS__) #define ITR1(e0,v) for(const auto &e0 : (v)) #define ITRP1(e0,v) for(auto e0 : (v)) #define ITRR1(e0,v) for(auto &e0 : (v)) #define ITR2(e0,e1,v) for(const auto [e0, e1] : (v)) #define ITRP2(e0,e1,v) for(auto [e0, e1] : (v)) #define ITRR2(e0,e1,v) for(auto &[e0, e1] : (v)) #define ITR3(e0,e1,e2,v) for(const auto [e0, e1, e2] : (v)) #define ITRP3(e0,e1,e2,v) for(auto [e0, e1, e2] : (v)) #define ITRR3(e0,e1,e2,v) for(auto &[e0, e1, e2] : (v)) #define ITR4(e0,e1,e2,e3,v) for(const auto [e0, e1, e2, e3] : (v)) #define ITRP4(e0,e1,e2,e3,v) for(auto [e0, e1, e2, e3] : (v)) #define ITRR4(e0,e1,e2,e3,v) for(auto &[e0, e1, e2, e3] : (v)) #define ITR5(e0,e1,e2,e3,e4,v) for(const auto [e0, e1, e2, e3, e4] : (v)) #define ITRP5(e0,e1,e2,e3,e4,v) for(auto [e0, e1, e2, e3, e4] : (v)) #define ITRR5(e0,e1,e2,e3,e4,v) for(auto &[e0, e1, e2, e3, e4] : (v)) #define ITR(...) $OVERLOAD6(__VA_ARGS__, ITR5, ITR4, ITR3, ITR2, ITR1)(__VA_ARGS__) #define ITRP(...) $OVERLOAD6(__VA_ARGS__, ITRP5, ITRP4, ITRP3, ITRP2, ITRP1)(__VA_ARGS__) #define ITRR(...) $OVERLOAD6(__VA_ARGS__, ITRR5, ITRR4, ITRR3, ITRR2, ITRR1)(__VA_ARGS__) /* [end]: snippet/iterations.hpp*/ /* [begin]: internal/dev_env.hpp */ #ifdef LOCAL_JUDGE inline constexpr bool DEV_ENV = true; inline constexpr bool NO_EXCEPT = false; #else inline constexpr bool DEV_ENV = false; inline constexpr bool NO_EXCEPT = true; #endif #if __cplusplus >= 202100L #define CPP20 true #define CPP23 true #elif __cplusplus >= 202002L #define CPP20 true #define CPP23 false #else #define CPP20 false #define CPP23 false #endif /* [end]: internal/dev_env.hpp*/ namespace std { template basic_istream& operator>>(std::basic_istream& in, uni::i128& v) noexcept(NO_EXCEPT) { std::string str; in >> str; v = 0; bool negative = (str[0] == '-'); REP(d, std::next(str.begin(), negative), str.end()) { assert(std::isdigit(*d)); v = v * 10 + *d - '0'; } if(negative) v *= -1; return in; } template basic_istream& operator>>(std::basic_istream& in, uni::u128& v) noexcept(NO_EXCEPT) { std::string str; in >> str; v = 0U; assert(str[0] != '-'); REP(d, str.begin(), str.end()) { assert(std::isdigit(*d)); v = v * 10U + *d - '0'; } return in; } template basic_ostream& operator<<(std::basic_ostream& out, uni::i128 v) noexcept(NO_EXCEPT) { if(v == 0) return out << 0; if(v < 0) out << '-', v *= -1; std::string str; while(v > 0) str += static_cast(v%10) + '0', v /= 10; std::reverse(str.begin(), str.end()); return out << str; } template basic_ostream& operator<<(std::basic_ostream& out, uni::u128 v) noexcept(NO_EXCEPT) { if(v == 0) return out << 0U; std::string str; while(v > 0) str += static_cast(v%10U) + '0', v /= 10U; std::reverse(str.begin(), str.end()); return out << str; } } /* [end]: numeric/int128.hpp*/ /* [begin]: internal/type_traits.hpp */ namespace uni { namespace internal { template struct tuple_or_pair { using type = std::tuple; }; template struct tuple_or_pair { using type = std::pair; }; template using tuple_or_pair_t = typename tuple_or_pair::type; template constexpr std::underlying_type_t to_underlying(const T& v) noexcept(NO_EXCEPT) { return static_cast>(v); } template using are_same = std::conjunction...>; template inline constexpr bool are_same_v = are_same::value; template using is_same_as_any_of = std::disjunction...>; template inline constexpr bool is_same_as_any_of_v = is_same_as_any_of::value; template concept same_as_any_of = is_same_as_any_of_v; template using is_base_of_all = std::conjunction...>; template inline constexpr bool is_base_of_all_v = is_base_of_all::value; template using is_base_of_any = std::disjunction...>; template inline constexpr bool is_base_of_any_v = is_base_of_any::value; template struct remove_cvref { using type = typename std::remove_cv_t>; }; template using remove_cvref_t = typename remove_cvref::type; template struct literal_operator { static constexpr const char* value = ""; }; template<> struct literal_operator { static constexpr const char* value = "U"; }; template<> struct literal_operator { static constexpr const char* value = "L"; }; template<> struct literal_operator { static constexpr const char* value = "UL"; }; template<> struct literal_operator { static constexpr const char* value = "LL"; }; template<> struct literal_operator { static constexpr const char* value = "ULL"; }; template<> struct literal_operator { static constexpr const char* value = "F"; }; template<> struct literal_operator { static constexpr const char* value = "D"; }; template<> struct literal_operator { static constexpr const char* value = "LD"; }; #ifdef __SIZEOF_INT128__ template<> struct literal_operator<__int128_t> { static constexpr const char* value = "LLL"; }; template<> struct literal_operator<__uint128_t> { static constexpr const char* value = "ULLL"; }; #endif template inline constexpr auto literal_operator_v = literal_operator::value; template struct nth_type {}; template struct nth_type<0, Head, Tail...> { using type = Head; }; template struct nth_type : public nth_type {}; template using nth_type_t = typename nth_type::type; template