/** * */ // header {{{ #include using namespace std; #define CPP_STR(x) CPP_STR_I(x) #define CPP_CAT(x,y) CPP_CAT_I(x,y) #define CPP_STR_I(args...) #args #define CPP_CAT_I(x,y) x ## y using i8 = int8_t; using u8 = uint8_t; using i16 = int16_t; using u16 = uint16_t; using i32 = int32_t; using u32 = uint32_t; using i64 = int64_t; using u64 = uint64_t; #ifdef __SIZEOF_INT128__ using i128 = __int128; using u128 = unsigned __int128; #endif using f32 = float; using f64 = double; using f80 = __float80; using f128 = __float128; // }}} template constexpr T PROCON_INF(); template<> constexpr i64 PROCON_INF() { return 1'010'000'000'000'000'017LL; } template<> constexpr f64 PROCON_INF() { return 1e100; } constexpr i64 INF = PROCON_INF(); constexpr f64 FINF = PROCON_INF(); constexpr i64 MOD = 1'000'000'007LL; constexpr f64 EPS = 1e-12; constexpr f64 PI = 3.14159265358979323846; // util {{{ #define FOR(i, start, end) for(i64 i = (start), CPP_CAT(i,xxxx_end)=(end); i < CPP_CAT(i,xxxx_end); ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(f,c,...) (([&](decltype((c)) cccc) { return (f)(std::begin(cccc), std::end(cccc), ## __VA_ARGS__); })(c)) #define SLICE(f,c,l,r,...) (([&](decltype((c)) cccc, decltype((l)) llll, decltype((r)) rrrr) {\ auto iiii = llll <= rrrr ? std::begin(cccc)+llll : std::end(cccc);\ auto jjjj = llll <= rrrr ? std::begin(cccc)+rrrr : std::end(cccc);\ return (f)(iiii, jjjj, ## __VA_ARGS__);\ })(c,l,r)) #define GENERIC(f) ([](auto&&... args) -> decltype(auto) { return (f)(std::forward(args)...); }) // ビット演算 {{{ // 2の補数を仮定 // 引数は [-INF,INF] のみ想定 i64 BIT_I(i64 i) { return 1LL << i; } i64 BIT_I_1(i64 i) { return BIT_I(i) - 1; } i64 BIT_GET(i64 x, i64 i) { return x & BIT_I(i); } bool BIT_TEST(i64 x, i64 i) { return BIT_GET(x,i) != 0; } i64 BIT_SET(i64 x, i64 i) { return x | BIT_I(i); } i64 BIT_CLEAR(i64 x, i64 i) { return x & ~BIT_I(i); } i64 BIT_FLIP(i64 x, i64 i) { return x ^ BIT_I(i); } i64 BIT_ASSIGN(i64 x, i64 i, bool b) { return b ? BIT_SET(x,i) : BIT_CLEAR(x,i); } i64 BIT_COUNT_LEADING_ZEROS(i64 x) { if(x == 0) return 64; return __builtin_clzll(x); } i64 BIT_COUNT_LEADING_ONES(i64 x) { return BIT_COUNT_LEADING_ZEROS(~x); } i64 BIT_COUNT_TRAILING_ZEROS(i64 x) { if(x == 0) return 64; return __builtin_ctzll(x); } i64 BIT_COUNT_TRAILING_ONES(i64 x) { return BIT_COUNT_TRAILING_ZEROS(~x); } // 末尾へ続く0を識別するマスクを返す (ex. 0b10100 -> 0b00011) // x=0 なら -1 を返す i64 BIT_MASK_TRAILING_ZEROS(i64 x) { return ~x & (x-1); } // 末尾へ続く1を識別するマスクを返す (ex. 0b10011 -> 0b00011) // x=-1 なら -1 を返す i64 BIT_MASK_TRAILING_ONES(i64 x) { return x & ~(x+1); } i64 BIT_COUNT_ONES(i64 x) { return __builtin_popcountll(x); } i64 BIT_COUNT_ZEROS(i64 x) { return 64 - BIT_COUNT_ONES(x); } // 先頭から続く冗長な符号ビットを数える (ex. 1 -> 62, -1 -> 63) i64 BIT_COUNT_LEADING_REDUNDANT_SIGN_BITS(i64 x) { return __builtin_clrsbll(x); } // 1の個数が奇数なら1, 偶数なら0を返す i64 BIT_PARITY(i64 x) { return __builtin_parityll(x); } // 最右の0を分離する (ex. 0b11001 -> 0b00010) // x=-1 なら 0 を返す i64 BIT_EXTRACT_FIRST_ZERO(i64 x) { return ~x & (x+1); } // 最右の1を分離する (ex. 0b10110 -> 0b00010) // x=0 なら 0 を返す i64 BIT_EXTRACT_FIRST_ONE(i64 x) { return x & (-x); } // 最右の0を1にする (ex. 0b11001 -> 0b11011) i64 BIT_FLIP_FIRST_ZERO(i64 x) { return x | (x+1); } // 最右の1を0にする (ex. 0b10110 -> 0b10100) i64 BIT_FLIP_FIRST_ONE(i64 x) { return x & (x-1); } // 最右の1の位置(1-based)を得る // x=0 なら 0 を返す i64 BIT_FIND_FIRST_ONE(i64 x) { return __builtin_ffsll(x); } // 最右の0の位置(1-based)を得る // x=-1 なら 0 を返す i64 BIT_FIND_FIRST_ZERO(i64 x) { return BIT_FIND_FIRST_ONE(~x); } // 最右の0をそれより右に伝播する (ex. 0b11011 -> 0b11000) // x=-1 なら -1 を返す i64 BIT_PROPAGATE_FIRST_ZERO(i64 x) { if(x == -1) return -1; return x & (x+1); } // 最右の1をそれより右に伝播する (ex. 0b10100 -> 0b10111) // x=0 なら 0 を返す i64 BIT_PROPAGATE_FIRST_ONE(i64 x) { if(x == 0) return 0; return x | (x-1); } // 最右の0および末尾へ続く1を識別するマスクを返す (ex. 0b11011 -> 0b00111) // x=-1 なら 0 を返す i64 BIT_MASKTO_FIRST_ZERO(i64 x) { if(x == -1) return 0; return x ^ (x+1); } // 最右の1および末尾へ続く0を識別するマスクを返す (ex. 0b10100 -> 0b00111) // x=0 なら 0 を返す i64 BIT_MASKTO_FIRST_ONE(i64 x) { if(x == 0) return 0; return x ^ (x-1); } // 最右の連続した0を1にする (ex. 0b101001 -> 0b101111) // x=-1 なら -1 を返す i64 BIT_FLIP_FIRST_ZEROS(i64 x) { return ((x&(x+1))-1) | x; } // 最右の連続した1を0にする (ex. 0b10110 -> 0b10000) // x=0 なら 0 を返す i64 BIT_FLIP_FIRST_ONES(i64 x) { return ((x|(x-1))+1) & x; } // X ⊆ {0,1,...,n-1}, |X| = k なる部分集合 X を昇順に列挙する // comb(n,k) 個 // // ex. // ``` // i64 x = BIT_I_1(3); // do { // // ... // } while(BIT_NEXT_SET_SIZED(x, 10)); // ``` bool BIT_NEXT_SET_SIZED(i64& x, i64 n) { if(x == 0) return false; i64 t = BIT_PROPAGATE_FIRST_ONE(x) + 1; x = t | (BIT_MASK_TRAILING_ZEROS(t) >> (BIT_COUNT_TRAILING_ZEROS(x)+1)); return x < BIT_I(n); } // 集合 Y の部分集合 X を昇順に列挙する // 2^|Y| 個 // // ex. // ``` // i64 y = 0b10101; // i64 x = 0; // do { // // ... // } while(BIT_NEXT_SUBSET(x, y)); // ``` bool BIT_NEXT_SUBSET(i64& x, i64 y) { if(x == y) return false; x = (x-y) & y; return true; } // 集合 Y の部分集合 X を降順に列挙する // 2^|Y| 個 // // ex. // ``` // i64 y = 0b10101; // i64 x = y; // do { // // ... // } while(BIT_PREV_SUBSET(x, y)); // ``` bool BIT_PREV_SUBSET(i64& x, i64 y) { if(x == 0) return false; x = (x-1) & y; return true; } // 集合 Y を包含する集合 X ⊆ {0,1,...,n-1} を昇順に列挙する // 2^(n-|Y|) 個 // // ex. // ``` // i64 y = 0b00010101; // i64 x = y; // do { // // ... // } while(BIT_NEXT_SUPERSET(x, 8, y)); // ``` bool BIT_NEXT_SUPERSET(i64& x, i64 n, i64 y) { x = (x+1) | y; return x < BIT_I(n); } // }}} // BoolArray {{{ class BoolArray { public: using value_type = bool; using reference = value_type&; using const_reference = const value_type&; using iterator = value_type*; using const_iterator = const value_type*; using difference_type = ptrdiff_t; using size_type = size_t; using reverse_iterator = std::reverse_iterator; using const_reverse_iterator = std::reverse_iterator; BoolArray() : BoolArray(0) {} explicit BoolArray(size_t n) : BoolArray(n,false) {} BoolArray(size_t n, bool value) : size_(n), data_(new bool[n]) { ALL(fill, *this, value); } BoolArray(initializer_list init) : size_(init.size()), data_(new bool[size_]) { ALL(copy, init, begin()); } template BoolArray(InputIt first, InputIt last) { deque tmp(first, last); size_ = tmp.size(); data_ = new bool[size_]; ALL(copy, tmp, begin()); } BoolArray(const BoolArray& other) : size_(other.size_), data_(new bool[size_]) { ALL(copy, other, begin()); } BoolArray(BoolArray&& other) noexcept : size_(other.size_), data_(other.data_) { other.data_ = nullptr; } BoolArray& operator=(const BoolArray& other) { if(this == &other) return *this; if(!data_ || size_ < other.size_) { delete[] data_; data_ = new bool[other.size_]; } size_ = other.size_; ALL(copy, other, begin()); return *this; } BoolArray& operator=(BoolArray&& other) noexcept { if(this == &other) return *this; size_ = other.size_; data_ = other.data_; other.data_ = nullptr; return *this; } BoolArray& operator=(initializer_list init) { if(!data_ || size_ < init.size()) { delete[] data_; data_ = new bool[init.size()]; } size_ = init.size(); ALL(copy, init, begin()); return *this; } void swap(BoolArray& other) noexcept { std::swap(size_, other.size_); std::swap(data_, other.data_); } ~BoolArray() { delete[] data_; data_ = nullptr; } bool empty() const noexcept { return size_ == 0; } size_type size() const noexcept { return size_; } size_type max_size() const noexcept { return 1'010'000'000; } iterator begin() noexcept { return data_; } const_iterator begin() const noexcept { return data_; } const_iterator cbegin() const noexcept { return data_; } iterator end() noexcept { return data_+size_; } const_iterator end() const noexcept { return data_+size_; } const_iterator cend() const noexcept { return data_+size_; } reverse_iterator rbegin() noexcept { return reverse_iterator(end()); } const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); } const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(end()); } reverse_iterator rend() noexcept { return reverse_iterator(begin()); } const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); } const_reverse_iterator crend() const noexcept { return const_reverse_iterator(begin()); } reference operator[](size_type pos) { return data_[pos]; } const_reference operator[](size_type pos) const { return data_[pos]; } bool* data() noexcept { return data_; } const bool* data() const noexcept { return data_; } private: size_t size_; bool* data_; }; void swap(BoolArray& lhs, BoolArray& rhs) noexcept { lhs.swap(rhs); } bool operator==(const BoolArray& lhs, const BoolArray& rhs) { return equal(begin(lhs), end(lhs), begin(rhs), end(rhs)); } bool operator!=(const BoolArray& lhs, const BoolArray& rhs) { return !(lhs == rhs); } bool operator<(const BoolArray& lhs, const BoolArray& rhs) { return lexicographical_compare(begin(lhs), end(lhs), begin(rhs), end(rhs)); } bool operator> (const BoolArray& lhs, const BoolArray& rhs) { return rhs < lhs; } bool operator<=(const BoolArray& lhs, const BoolArray& rhs) { return !(rhs < lhs); } bool operator>=(const BoolArray& lhs, const BoolArray& rhs) { return !(lhs < rhs); } // }}} // 多次元 vector {{{ // 最内周が vector になるのを避けるための措置 template struct Array1Container { using type = vector; }; template<> struct Array1Container { using type = BoolArray; }; // イテレート用 template struct is_arrayn_container { static constexpr bool value = false; }; template struct is_arrayn_container> { static constexpr bool value = true; }; template<> struct is_arrayn_container { static constexpr bool value = true; }; template auto arrayn_make(i64 n, T x) { using Cont = typename Array1Container::type; return Cont(n, x); } template = nullptr> auto arrayn_make(i64 n, Args... args) { auto inner = arrayn_make(args...); return vector(n, inner); } template enable_if_t::value> arrayn_foreach(T& e, F f) { f(e); } template enable_if_t::value> arrayn_foreach(T& ary, F f) { for(auto& e : ary) arrayn_foreach(e, f); } template enable_if_t::value> arrayn_fill(T& ary, const U& x) { arrayn_foreach(ary, [&x](auto& e) { e = x; }); } // }}} // 多次元生配列 {{{ template enable_if_t::value==0> CARRAY_FOREACH(T& e, F f) { f(e); } template enable_if_t::value!=0> CARRAY_FOREACH(Array& ary, F f) { for(auto& e : ary) CARRAY_FOREACH(e, f); } template enable_if_t::value!=0> CARRAY_FILL(Array& ary, const U& v) { CARRAY_FOREACH(ary, [&v](auto& e) { e = v; }); } // }}} // lambda で再帰 {{{ template class FixPoint { public: explicit constexpr FixPoint(F&& f) : f_(forward(f)) {} template constexpr decltype(auto) operator()(Args&&... args) const { return f_(*this, forward(args)...); } private: const F f_; }; template decltype(auto) FIX(F&& f) { return FixPoint(forward(f)); } // }}} // tuple {{{ template = nullptr> constexpr auto tuple_head(const tuple& t) { return get<0>(t); } template constexpr auto tuple_tail_helper(const tuple& t, index_sequence) { return make_tuple(get(t)...); } template = nullptr> constexpr auto tuple_tail(const tuple&) { return make_tuple(); } template = nullptr> constexpr auto tuple_tail(const tuple& t) { return tuple_tail_helper(t, make_index_sequence()); } // }}} // FST/SND {{{ template T1& FST(pair& p) { return p.first; } template const T1& FST(const pair& p) { return p.first; } template T2& SND(pair& p) { return p.second; } template const T2& SND(const pair& p) { return p.second; } template = nullptr> auto& FST(tuple& t) { return get<0>(t); } template = nullptr> const auto& FST(const tuple& t) { return get<0>(t); } template = nullptr> auto& SND(tuple& t) { return get<1>(t); } template = nullptr> const auto& SND(const tuple& t) { return get<1>(t); } // }}} template, enable_if_t< is_integral::value && is_integral::value && is_signed::value != is_unsigned::value, nullptr_t > = nullptr> common_type_t MAX(T1 x, T2 y, Comp comp={}) { return max>(x, y, comp); } template, enable_if_t< is_floating_point::value && is_floating_point::value, nullptr_t > = nullptr> common_type_t MAX(T1 x, T2 y, Comp comp={}) { return max>(x, y, comp); } template> const T& MAX(const T& x, const T& y, Comp comp={}) { return max(x, y, comp); } template> T MAX(initializer_list ilist, Comp comp={}) { return max(ilist, comp); } template, enable_if_t< is_integral::value && is_integral::value && is_signed::value != is_unsigned::value, nullptr_t > = nullptr> common_type_t MIN(T1 x, T2 y, Comp comp={}) { return min>(x, y, comp); } template, enable_if_t< is_floating_point::value && is_floating_point::value, nullptr_t > = nullptr> common_type_t MIN(T1 x, T2 y, Comp comp={}) { return min>(x, y, comp); } template> const T& MIN(const T& x, const T& y, Comp comp={}) { return min(x, y, comp); } template> T MIN(initializer_list ilist, Comp comp={}) { return min(ilist, comp); } template T ABS(T x) { static_assert(is_signed::value, "ABS(): argument must be signed"); return x < 0 ? -x : x; } template i64 SIZE(const C& c) { return static_cast(c.size()); } template i64 SIZE(const T (&)[N]) { return static_cast(N); } bool is_odd (i64 x) { return x % 2 != 0; } bool is_even(i64 x) { return x % 2 == 0; } template i64 cmp(T x, T y) { return (y i64 sgn(T x) { return cmp(x, T(0)); } // 事前条件: a >= 0, b >= 0 i64 gcd_impl(i64 a, i64 b) { if(b == 0) return a; return gcd_impl(b, a%b); } // GCD(0,0) = 0 i64 GCD(i64 a, i64 b) { return gcd_impl(ABS(a), ABS(b)); } // LCM(0,x) は未定義 i64 LCM(i64 a, i64 b) { assert(a != 0 && b != 0); a = ABS(a); b = ABS(b); return a / gcd_impl(a,b) * b; } // lo:OK, hi:NG template i64 bisect_integer(i64 lo, i64 hi, Pred pred) { assert(lo < hi); while(lo+1 < hi) { i64 mid = (lo+hi) / 2; if(pred(mid)) lo = mid; else hi = mid; } return lo; } template f64 bisect_real(f64 lo, f64 hi, Pred pred, i64 iter=100) { assert(lo < hi); REP(_, iter) { f64 mid = (lo+hi) / 2; if(pred(mid)) lo = mid; else hi = mid; } return lo; } i64 ipow(i64 x, i64 e) { assert(e >= 0); i64 res = 1; REP(_, e) { res *= x; } return res; } i64 sqrt_floor(i64 x) { assert(x >= 0); i64 lo = 0; i64 hi = MIN(x/2+2, 3037000500LL); return bisect_integer(lo, hi, [x](i64 r) { return r*r <= x; }); } i64 sqrt_ceil(i64 x) { i64 r = sqrt_floor(x); return r*r == x ? r : r+1; } // 0 <= log2_ceil(x) <= 63 i64 log2_ceil(i64 x) { assert(x > 0); return 64 - BIT_COUNT_LEADING_ZEROS(x-1); } // 0 <= log2_floor(x) <= 62 i64 log2_floor(i64 x) { assert(x > 0); return 63 - BIT_COUNT_LEADING_ZEROS(x); } // 0 <= log10_ceil(x) <= 19 i64 log10_ceil(i64 x) { assert(x > 0); static constexpr i64 TABLE[19] { 1LL, 10LL, 100LL, 1000LL, 10000LL, 100000LL, 1000000LL, 10000000LL, 100000000LL, 1000000000LL, 10000000000LL, 100000000000LL, 1000000000000LL, 10000000000000LL, 100000000000000LL, 1000000000000000LL, 10000000000000000LL, 100000000000000000LL, 1000000000000000000LL, }; REP(i, SIZE(TABLE)) { if(x <= TABLE[i]) return i; } return SIZE(TABLE); } // 0 <= log10_floor(x) <= 18 i64 log10_floor(i64 x) { assert(x > 0); static constexpr i64 TABLE[18] { 9LL, 99LL, 999LL, 9999LL, 99999LL, 999999LL, 9999999LL, 99999999LL, 999999999LL, 9999999999LL, 99999999999LL, 999999999999LL, 9999999999999LL, 99999999999999LL, 999999999999999LL, 9999999999999999LL, 99999999999999999LL, 999999999999999999LL, }; REP(i, SIZE(TABLE)) { if(x <= TABLE[i]) return i; } return SIZE(TABLE); } // 2^n - 1 の形かどうか bool is_mersenne(i64 x) { assert(x >= 0); return (x&(x+1)) == 0; } bool is_pow2(i64 x) { assert(x > 0); return (x&(x-1)) == 0; } // x > 0 i64 pow2_ceil(i64 x) { return BIT_I(log2_ceil(x)); } // x > 0 i64 pow2_floor(i64 x) { return BIT_I(log2_floor(x)); } // Haskell の divMod と同じ pair divmod(i64 a, i64 b) { i64 q = a / b; i64 r = a % b; if((b>0 && r<0) || (b<0 && r>0)) { --q; r += b; } return {q,r}; } i64 div_ceil(i64 a, i64 b) { i64 q = a / b; i64 r = a % b; if((b>0 && r>0) || (b<0 && r<0)) ++q; return q; } i64 div_floor(i64 a, i64 b) { return divmod(a,b).first; } i64 modulo(i64 a, i64 b) { return divmod(a,b).second; } bool feq(f64 x, f64 y, f64 eps=EPS) { return fabs(x-y) < eps; } template> bool chmax(T& xmax, const U& x, Comp comp={}) { if(comp(xmax, x)) { xmax = x; return true; } return false; } template> bool chmin(T& xmin, const U& x, Comp comp={}) { if(comp(x, xmin)) { xmin = x; return true; } return false; } template auto FOLD(InputIt first, InputIt last, typename iterator_traits::value_type init, BinaryOp op) { for(; first != last; ++first) init = op(move(init), *first); return init; } template auto FOLD1(InputIt first, InputIt last, BinaryOp op) { auto init = *first++; return FOLD(first, last, init, op); } template auto SUM(InputIt first, InputIt last) { using T = typename iterator_traits::value_type; return accumulate(first, last, T()); } template ForwardIt transform_self(ForwardIt first, ForwardIt last, UnaryOperation op) { return transform(first, last, first, op); } template void UNIQ(C& c) { c.erase(ALL(unique,c), end(c)); } template auto FLIP(BinaryFunc f) { return [f](const auto& x, const auto& y) { return f(y,x); }; } template auto ON(BinaryFunc bf, UnaryFunc uf) { return [bf,uf](const auto& x, const auto& y) { return bf(uf(x), uf(y)); }; } template auto LT_ON(F f) { return ON(less<>(), f); } template auto GT_ON(F f) { return ON(greater<>(), f); } template auto EQ_ON(F f) { return ON(equal_to<>(), f); } template auto NE_ON(F f) { return ON(not_equal_to<>(), f); } template> auto EQUIV(Comp comp={}) { return [comp](const auto& lhs, const auto& rhs) { return !comp(lhs,rhs) && !comp(rhs,lhs); }; } struct IDENTITY { using is_transparent = void; template constexpr T&& operator()(T&& x) const noexcept { return forward(x); } }; template struct OpMax { using result_type = T; using first_argument_type = T; using second_argument_type = T; T operator()(const T& x, const T& y) const { return MAX(x, y); } }; template<> struct OpMax { using is_transparent = void; template auto operator()(T1&& x, T2&& y) const { return MAX(forward(x), forward(y)); } }; template struct OpMin { using result_type = T; using first_argument_type = T; using second_argument_type = T; T operator()(const T& x, const T& y) const { return MIN(x, y); } }; template<> struct OpMin { using is_transparent = void; template auto operator()(T1&& x, T2&& y) const { return MIN(forward(x), forward(y)); } }; template struct OpGcd { using result_type = T; using first_argument_type = T; using second_argument_type = T; T operator()(const T& x, const T& y) const { return GCD(x, y); } }; template<> struct OpGcd { using is_transparent = void; template auto operator()(T1&& x, T2&& y) const { return GCD(forward(x), forward(y)); } }; template struct OpLcm { using result_type = T; using first_argument_type = T; using second_argument_type = T; T operator()(const T& x, const T& y) const { return LCM(x, y); } }; template<> struct OpLcm { using is_transparent = void; template auto operator()(T1&& x, T2&& y) const { return LCM(forward(x), forward(y)); } }; template ForwardIt next_bounded(ForwardIt last, ForwardIt it, i64 n=1) { auto bound = distance(it, last); return next(it, MIN(n, bound)); } template ForwardIt prev_bounded(ForwardIt first, ForwardIt it, i64 n=1) { auto bound = distance(first, it); return prev(it, MIN(n, bound)); } template void advance_bounded(ForwardIt first, ForwardIt last, ForwardIt& it, i64 n) { if(n > 0) { auto bound = distance(it, last); advance(it, MIN(n, bound)); } else if(n < 0) { auto bound = distance(it, first); advance(it, MAX(n, bound)); } } char digit_chr(i64 n) { return static_cast('0' + n); } i64 digit_ord(char c) { return c - '0'; } char lower_chr(i64 n) { return static_cast('a' + n); } i64 lower_ord(char c) { return c - 'a'; } char upper_chr(i64 n) { return static_cast('A' + n); } i64 upper_ord(char c) { return c - 'A'; } // 出力は operator<< を直接使わず、このテンプレート経由で行う // 提出用出力とデバッグ用出力を分けるため template struct Formatter { static ostream& write_str(ostream& out, const T& x) { return out << x; } static ostream& write_repr(ostream& out, const T& x) { return out << x; } }; template ostream& WRITE_STR(ostream& out, const T& x) { return Formatter::write_str(out, x); } template ostream& WRITE_REPR(ostream& out, const T& x) { return Formatter::write_repr(out, x); } template ostream& WRITE_JOIN_STR(ostream& out, InputIt first, InputIt last, const string& sep) { while(first != last) { WRITE_STR(out, *first++); if(first != last) out << sep; } return out; } template ostream& WRITE_JOIN_REPR(ostream& out, InputIt first, InputIt last, const string& sep) { while(first != last) { WRITE_REPR(out, *first++); if(first != last) out << sep; } return out; } template ostream& WRITE_RANGE_STR(ostream& out, InputIt first, InputIt last) { return WRITE_JOIN_STR(out, first, last, " "); } template ostream& WRITE_RANGE_REPR(ostream& out, InputIt first, InputIt last) { out << "["; WRITE_JOIN_REPR(out, first, last, ", "); out << "]"; return out; } template void FROM_STR(const string& s, T& x) { istringstream in(s); in >> x; } template string TO_STR(const T& x) { ostringstream out; WRITE_STR(out, x); return out.str(); } template string TO_REPR(const T& x) { ostringstream out; WRITE_REPR(out, x); return out.str(); } template string RANGE_TO_STR(InputIt first, InputIt last) { ostringstream out; WRITE_RANGE_STR(out, first, last); return out.str(); } template string RANGE_TO_REPR(InputIt first, InputIt last) { ostringstream out; WRITE_RANGE_REPR(out, first, last); return out.str(); } template string JOIN(InputIt first, InputIt last, const string& sep) { ostringstream out; WRITE_JOIN_STR(out, first, last, sep); return out.str(); } template<> struct Formatter { static ostream& write_str(ostream& out, i64 x) { return out << x; } static ostream& write_repr(ostream& out, i64 x) { if(x == INF) return out << "INF"; if(x == -INF) return out << "-INF"; return out << x; } }; template<> struct Formatter { static ostream& write_str(ostream& out, f64 x) { return out << x; } static ostream& write_repr(ostream& out, f64 x) { #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wfloat-equal" if(x == FINF) return out << "FINF"; if(x == -FINF) return out << "-FINF"; #pragma GCC diagnostic pop return out << x; } }; template struct Formatter::value>> { static ostream& write_str(ostream& out, Enum x) { return WRITE_STR(out, static_cast>(x)); } static ostream& write_repr(ostream& out, Enum x) { return WRITE_REPR(out, static_cast>(x)); } }; template struct Formatter> { static ostream& write_str(ostream& out, const vector& v) { return WRITE_RANGE_STR(out, begin(v), end(v)); } static ostream& write_repr(ostream& out, const vector& v) { out << "vector"; return WRITE_RANGE_REPR(out, begin(v), end(v)); } }; template<> struct Formatter { static ostream& write_str(ostream& out, const BoolArray& a) { return WRITE_RANGE_STR(out, begin(a), end(a)); } static ostream& write_repr(ostream& out, const BoolArray& a) { out << "BoolArray"; return WRITE_RANGE_REPR(out, begin(a), end(a)); } }; template struct Formatter> { static ostream& write_str(ostream& out, const pair& p) { WRITE_STR(out, p.first); out << ' '; WRITE_STR(out, p.second); return out; } static ostream& write_repr(ostream& out, const pair& p) { out << "("; WRITE_REPR(out, p.first); out << ","; WRITE_REPR(out, p.second); out << ")"; return out; } }; template struct Formatter> { template = nullptr> static ostream& write_str_impl(ostream& out, const tuple&) { return out; } template = nullptr> static ostream& write_str_impl(ostream& out, const tuple& t) { if(I != 0) out << ' '; WRITE_STR(out, get(t)); return write_str_impl(out, t); } template = nullptr> static ostream& write_repr_impl(ostream& out, const tuple&) { if(sizeof...(TS) == 0) out << "("; return out << ")"; } template = nullptr> static ostream& write_repr_impl(ostream& out, const tuple& t) { if(I == 0) out << "("; else out << ","; WRITE_REPR(out, get(t)); return write_repr_impl(out, t); } static ostream& write_str(ostream& out, const tuple& t) { return write_str_impl(out, t); } static ostream& write_repr(ostream& out, const tuple& t) { return write_repr_impl(out, t); } }; template void RD(T& x) { cin >> x; #ifdef PROCON_LOCAL assert(cin); #endif } template void RD1(T& x) { RD(x); --x; } template auto RD_ARRAY(i64 n) { auto res = arrayn_make(n, T()); arrayn_foreach(res, [](T& e) { RD(e); }); return res; } template auto RD1_ARRAY(i64 n) { auto res = arrayn_make(n, T()); arrayn_foreach(res, [](T& e) { RD1(e); }); return res; } template auto RD_ARRAY2(i64 h, i64 w) { auto res = arrayn_make(h,w, T()); arrayn_foreach(res, [](T& e) { RD(e); }); return res; } template auto RD1_ARRAY2(i64 h, i64 w) { auto res = arrayn_make(h,w, T()); arrayn_foreach(res, [](T& e) { RD1(e); }); return res; } template pair RD_PAIR() { T1 x; RD(x); T2 y; RD(y); return { x, y }; } template pair RD1_PAIR() { T1 x; RD1(x); T2 y; RD1(y); return { x, y }; } template = nullptr> auto RD_TUPLE() { return make_tuple(); } template auto RD_TUPLE() { T x; RD(x); return tuple_cat(make_tuple(x), RD_TUPLE()); } template = nullptr> auto RD1_TUPLE() { return make_tuple(); } template auto RD1_TUPLE() { T x; RD1(x); return tuple_cat(make_tuple(x), RD_TUPLE()); } void PRINT() {} template void PRINT(const T& x, const TS& ...args) { WRITE_STR(cout, x); if(sizeof...(args)) { cout << ' '; PRINT(args...); } } template void PRINTLN(const TS& ...args) { PRINT(args...); cout << '\n'; } [[noreturn]] void EXIT() { cout.flush(); #ifdef PROCON_LOCAL cerr.flush(); exit(0); #else _Exit(0); #endif } template = nullptr> void DBG_IMPL(i64 line, const char* expr, const tuple& value) { #ifdef PROCON_LOCAL cerr << "[L " << line << "]: "; cerr << expr << " = "; WRITE_REPR(cerr, get<0>(value)); cerr << "\n"; #endif } template = nullptr> void DBG_IMPL(i64 line, const char* expr, const tuple& value) { #ifdef PROCON_LOCAL cerr << "[L " << line << "]: "; cerr << "(" << expr << ") = "; WRITE_REPR(cerr, value); cerr << "\n"; #endif } template void DBG_CARRAY_IMPL(i64 line, const char* expr, const T (&ary)[N]) { #ifdef PROCON_LOCAL cerr << "[L " << line << "]: "; cerr << expr << " = "; WRITE_RANGE_REPR(cerr, begin(ary), end(ary)); cerr << "\n"; #endif } template void DBG_RANGE_IMPL(i64 line, const char* expr1, const char* expr2, InputIt first, InputIt last) { #ifdef PROCON_LOCAL cerr << "[L " << line << "]: "; cerr << expr1 << "," << expr2 << " = "; WRITE_RANGE_REPR(cerr, first, last); cerr << "\n"; #endif } #define DBG(args...) DBG_IMPL(__LINE__, CPP_STR_I(args), make_tuple(args)) #define DBG_CARRAY(expr) DBG_CARRAY_IMPL(__LINE__, CPP_STR(expr), (expr)) #define DBG_RANGE(first,last) DBG_RANGE_IMPL(__LINE__, CPP_STR(first), CPP_STR(last), (first), (last)) #define PAIR make_pair #define TUPLE make_tuple // }}} // init {{{ struct ProconInit { static constexpr int IOS_PREC = 15; static constexpr bool AUTOFLUSH = false; ProconInit() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(IOS_PREC); #ifdef PROCON_LOCAL cerr << fixed << setprecision(IOS_PREC); #endif if(AUTOFLUSH) cout << unitbuf; } } PROCON_INIT; // }}} // container {{{ // hash {{{ template struct procon_hash { size_t operator()(const T& x) const { return hash()(x); } }; template size_t procon_hash_value(const T& x) { return procon_hash()(x); } template void procon_hash_combine(size_t& seed, const T& x) { seed ^= procon_hash_value(x) + 0x9e3779b9 + (seed<<6) + (seed>>2); } template void procon_hash_range(size_t& seed, InputIt first, InputIt last) { for(; first != last; ++first) procon_hash_combine(seed, *first); } template size_t procon_hash_range(InputIt first, InputIt last) { size_t seed = 0; procon_hash_range(seed, first, last); return seed; } template = nullptr> void procon_hash_tuple(size_t&, const tuple&) {} template = nullptr> void procon_hash_tuple(size_t& seed, const tuple& t) { procon_hash_combine(seed, tuple_head(t)); procon_hash_tuple(seed, tuple_tail(t)); } template struct procon_hash> { size_t operator()(const vector& v) const { return ALL(procon_hash_range, v); } }; template struct procon_hash> { size_t operator()(const pair& p) const { size_t seed = 0; procon_hash_combine(seed, p.first); procon_hash_combine(seed, p.second); return seed; } }; template struct procon_hash> { size_t operator()(const tuple& t) const { size_t seed = 0; procon_hash_tuple(seed, t); return seed; } }; template> using HashSet = unordered_set,Eq>; template> using HashMap = unordered_map,Eq>; template> using HashMultiset = unordered_multiset,Eq>; template> using HashMultimap = unordered_multimap,Eq>; // }}} template using MaxHeap = priority_queue, less>; template using MinHeap = priority_queue, greater>; template bool set_contains(const set& s, const typename set::key_type& x) { return s.find(x) != end(s); } template bool set_contains(const unordered_set& s, const typename unordered_set::key_type& x) { return s.find(x) != end(s); } template bool set_contains(const multiset& s, const typename multiset::key_type& x) { return s.find(x) != end(s); } template bool set_contains(const unordered_multiset& s, const typename unordered_multiset::key_type& x) { return s.find(x) != end(s); } template bool map_contains(const map& m, const typename map::key_type& k) { return m.find(k) != end(m); } template bool map_contains(const unordered_map& m, const typename unordered_map::key_type& k) { return m.find(k) != end(m); } template bool map_contains(const multimap& m, const typename map::key_type& k) { return m.find(k) != end(m); } template bool map_contains(const unordered_multimap& m, const typename unordered_map::key_type& k) { return m.find(k) != end(m); } template bool multiset_erase_one(multiset& m, const typename multiset::key_type& k) { auto it = m.find(k); if(it == end(m)) return false; m.erase(it); return true; } template bool multiset_erase_one(unordered_multiset& m, const typename unordered_multiset::key_type& k) { auto it = m.find(k); if(it == end(m)) return false; m.erase(it); return true; } // POP() 系 {{{ // 効率は悪い template T POP_FRONT(vector& v) { T x = v.front(); v.erase(begin(v)); return x; } template T POP_BACK(vector& v) { T x = v.back(); v.pop_back(); return x; } template T POP_FRONT(deque& v) { T x = v.front(); v.pop_front(); return x; } template T POP_BACK(deque& v) { T x = v.back(); v.pop_back(); return x; } template T POP_FRONT(forward_list& ls) { T x = ls.front(); ls.pop_front(); return x; } template T POP_FRONT(list& ls) { T x = ls.front(); ls.pop_front(); return x; } template T POP_BACK(list& ls) { T x = ls.back(); ls.pop_back(); return x; } template T POP(stack& stk) { T x = stk.top(); stk.pop(); return x; } template T POP(queue& que) { T x = que.front(); que.pop(); return x; } template T POP(priority_queue& que) { T x = que.top(); que.pop(); return x; } // }}} // bimap {{{ template struct BiHashMap { HashMap fwd_; HashMap rev_; void insert(const T1& x, const T2& y) { auto it_fwd = fwd_.find(x); if(it_fwd == end(fwd_)) { fwd_.insert(it_fwd, make_pair(x,y)); rev_.insert(end(rev_), make_pair(y,x)); } else { assert(y == it_fwd->second); } } bool contains_fwd(const T1& x) const { return map_contains(fwd_, x); } bool contains_rev(const T2& y) const { return map_contains(rev_, y); } const T2& at_fwd(const T1& x) const { auto it = fwd_.find(x); assert(it != end(fwd_)); return it->second; } const T1& at_rev(const T2& y) const { auto it = rev_.find(y); assert(it != end(rev_)); return it->second; } size_t size() const { return fwd_.size(); } }; // }}} // Formatter {{{ template struct Formatter> { static ostream& write_str(ostream& out, const array& a) { return WRITE_RANGE_STR(out, begin(a), end(a)); } static ostream& write_repr(ostream& out, const array& a) { out << "array"; return WRITE_RANGE_REPR(out, begin(a), end(a)); } }; template struct Formatter> { static ostream& write_str(ostream& out, const deque& deq) { return WRITE_RANGE_STR(out, begin(deq), end(deq)); } static ostream& write_repr(ostream& out, const deque& deq) { out << "deque"; return WRITE_RANGE_REPR(out, begin(deq), end(deq)); } }; template struct Formatter> { static ostream& write_str(ostream& out, const forward_list& ls) { return WRITE_RANGE_STR(out, begin(ls), end(ls)); } static ostream& write_repr(ostream& out, const forward_list& ls) { out << "forward_list"; return WRITE_RANGE_REPR(out, begin(ls), end(ls)); } }; template struct Formatter> { static ostream& write_str(ostream& out, const list& ls) { return WRITE_RANGE_STR(out, begin(ls), end(ls)); } static ostream& write_repr(ostream& out, const list& ls) { out << "list"; return WRITE_RANGE_REPR(out, begin(ls), end(ls)); } }; template struct Formatter> { static ostream& write_str(ostream& out, const set& s) { return WRITE_RANGE_STR(out, begin(s), end(s)); } static ostream& write_repr(ostream& out, const set& s) { out << "set"; return WRITE_RANGE_REPR(out, begin(s), end(s)); } }; template struct Formatter> { static ostream& write_str(ostream& out, const multiset& s) { return WRITE_RANGE_STR(out, begin(s), end(s)); } static ostream& write_repr(ostream& out, const multiset& s) { out << "multiset"; return WRITE_RANGE_REPR(out, begin(s), end(s)); } }; template struct Formatter> { static ostream& write_str(ostream& out, const unordered_set& s) { return WRITE_RANGE_STR(out, begin(s), end(s)); } static ostream& write_repr(ostream& out, const unordered_set& s) { out << "unordered_set"; return WRITE_RANGE_REPR(out, begin(s), end(s)); } }; template struct Formatter> { static ostream& write_str(ostream& out, const unordered_multiset& s) { return WRITE_RANGE_STR(out, begin(s), end(s)); } static ostream& write_repr(ostream& out, const unordered_multiset& s) { out << "unordered_multiset"; return WRITE_RANGE_REPR(out, begin(s), end(s)); } }; template struct Formatter> { static ostream& write_str(ostream& out, const map& m) { return WRITE_RANGE_STR(out, begin(m), end(m)); } static ostream& write_repr(ostream& out, const map& m) { out << "map"; return WRITE_RANGE_REPR(out, begin(m), end(m)); } }; template struct Formatter> { static ostream& write_str(ostream& out, const multimap& m) { return WRITE_RANGE_STR(out, begin(m), end(m)); } static ostream& write_repr(ostream& out, const multimap& m) { out << "multimap"; return WRITE_RANGE_REPR(out, begin(m), end(m)); } }; template struct Formatter> { static ostream& write_str(ostream& out, const unordered_map& m) { return WRITE_RANGE_STR(out, begin(m), end(m)); } static ostream& write_repr(ostream& out, const unordered_map& m) { out << "unordered_map"; return WRITE_RANGE_REPR(out, begin(m), end(m)); } }; template struct Formatter> { static ostream& write_str(ostream& out, const unordered_multimap& m) { return WRITE_RANGE_STR(out, begin(m), end(m)); } static ostream& write_repr(ostream& out, const unordered_multimap& m) { out << "unordered_multimap"; return WRITE_RANGE_REPR(out, begin(m), end(m)); } }; template struct Formatter> { static ostream& write_str(ostream& out, const stack& orig) { stack stk(orig); while(!stk.empty()) { WRITE_STR(out, stk.top()); stk.pop(); if(!stk.empty()) out << ' '; } return out; } static ostream& write_repr(ostream& out, const stack& orig) { stack stk(orig); out << "stack["; while(!stk.empty()) { WRITE_REPR(out, stk.top()); stk.pop(); if(!stk.empty()) out << ", "; } out << "]"; return out; } }; template struct Formatter> { static ostream& write_str(ostream& out, const queue& orig) { queue que(orig); while(!que.empty()) { WRITE_STR(out, que.front()); que.pop(); if(!que.empty()) out << ' '; } return out; } static ostream& write_repr(ostream& out, const queue& orig) { queue que(orig); out << "queue["; while(!que.empty()) { WRITE_REPR(out, que.front()); que.pop(); if(!que.empty()) out << ", "; } out << "]"; return out; } }; template struct Formatter> { static ostream& write_str(ostream& out, const priority_queue& orig) { priority_queue que(orig); while(!que.empty()) { WRITE_STR(out, que.top()); que.pop(); if(!que.empty()) out << ' '; } return out; } static ostream& write_repr(ostream& out, const priority_queue& orig) { priority_queue que(orig); out << "priority_queue["; while(!que.empty()) { WRITE_REPR(out, que.top()); que.pop(); if(!que.empty()) out << ", "; } out << "]"; return out; } }; // }}} // }}} // num {{{ // 事前条件: a >= 0, b >= 0 i64 extgcd_impl(i64 a, i64 b, i64& x, i64& y) { if(b == 0) { x = 1; y = 0; return a; } i64 g = extgcd_impl(b, a%b, y, x); y -= a/b * x; return g; } // g=gcd(a,b), および ax+by=g の整数解 (x0,y0) を求める // (g,x0,y0) を返す // g!=0 のとき、一般解は (x,y) = (x0+m*b/g, y0-m*a/g) で与えられる(mは整数) tuple extgcd(i64 a, i64 b) { i64 x, y; i64 g = extgcd_impl(ABS(a), ABS(b), x, y); x *= sgn(a); y *= sgn(b); return make_tuple(g, x, y); } vector divisors_proper(i64 n) { if(n == 1) return {}; vector res(1, 1); i64 d = 2; for(; d*d < n; ++d) { if(n % d == 0) { res.emplace_back(d); res.emplace_back(n/d); } } if(d*d == n) res.emplace_back(d); return res; } vector divisors(i64 n) { vector res = divisors_proper(n); res.emplace_back(n); return res; } // 素因数分解 // (素因数,指数) のリストを返す // n >= 1 でなければならない // n == 1 の場合、空リストを返す vector> factorize(i64 n) { assert(n >= 1); vector> res; i64 m = n; for(i64 i = 2; i*i <= n; ++i) { if(m == 1) break; i64 e = 0; while(m % i == 0) { ++e; m /= i; } if(e) res.emplace_back(i, e); } if(m > 1) res.emplace_back(m, 1); return res; } bool is_square(i64 x) { i64 r = sqrt_floor(x); return r*r == x; } // Miller-Rabin 法 // // 参考: http://miller-rabin.appspot.com/ bool is_prime_u32(u32 n) { static constexpr u32 AS[] { 2, 7, 61, }; static const auto mulmod32 = [](u32 a, u32 b, u32 m) -> u32 { u64 res = a; res *= b; res %= m; return static_cast(res); }; static const auto powmod32 = [](u32 a, u32 b, u32 m) -> u32 { u32 res = 1; while(b > 0) { if(b & 1) res = mulmod32(res, a, m); a = mulmod32(a, a, m); b >>= 1; } return res; }; if(n <= 1) return false; if(n == 2) return true; if(n % 2 == 0) return false; u32 d = n-1; u32 s = __builtin_ctz(d); d >>= s; for(u32 a : AS) { if(a >= n) a %= n; if(a == 0) continue; u32 x = powmod32(a, d, n); if(x == 1 || x == n-1) continue; u32 r; for(r = 1; r < s; ++r) { x = mulmod32(x, x, n); if(x == 1) return false; if(x == n-1) break; } if(r == s) return false; } return true; } bool is_prime_u64(u64 n) { static constexpr u64 AS[] { 2, 325, 9375, 28178, 450775, 9780504, 1795265022, }; static const auto mulmod64 = [](u64 a, u64 b, u64 m) -> u64 { u128 res = a; res *= b; res %= m; return static_cast(res); }; static const auto powmod64 = [](u64 a, u64 b, u64 m) -> u64 { u64 res = 1; while(b > 0) { if(b & 1) res = mulmod64(res, a, m); a = mulmod64(a, a, m); b >>= 1; } return res; }; if(n <= numeric_limits::max()) return is_prime_u32(static_cast(n)); if(n % 2 == 0) return false; u64 d = n-1; u64 s = __builtin_ctzll(d); d >>= s; for(u64 a : AS) { if(a >= n) a %= n; if(a == 0) continue; u64 x = powmod64(a, d, n); if(x == 1 || x == n-1) continue; u64 r; for(r = 1; r < s; ++r) { x = mulmod64(x, x, n); if(x == 1) return false; if(x == n-1) break; } if(r == s) return false; } return true; } bool is_prime(i64 n) { assert(n >= 0); return is_prime_u64(static_cast(n)); } // 二分累乗 template Monoid pow_binary(Monoid x, i64 e) { assert(e >= 0); Monoid res(1); // 行列などの場合はここを適当に変える Monoid cur = x; while(e > 0) { if(e & 1) res *= cur; cur *= cur; e >>= 1; } return res; } // mod m での a の逆元 // a ⊥ m でなければならない i64 inv_mod(i64 a, i64 m) { i64 g,x0; tie(g,x0,ignore) = extgcd(a, m); assert(g == 1); return modulo(x0, m); } template struct ModPT { static_assert(P >= 2, "P must be a prime"); i64 v_; // [0,P) ModPT() : v_(0) {} ModPT(i64 v) : v_(modulo(v,P)) {} ModPT operator-() const { return ModPT(-v_); } ModPT& operator+=(ModPT rhs) { v_ += rhs.v_; v_ %= P; return *this; } ModPT& operator-=(ModPT rhs) { v_ += P; v_ -= rhs.v_; v_ %= P; return *this; } ModPT& operator*=(ModPT rhs) { v_ *= rhs.v_; v_ %= P; return *this; } ModPT& operator++() { return *this += 1; } ModPT& operator--() { return *this -= 1; } ModPT operator++(int) { ModPT res(*this); ++*this; return res; } ModPT operator--(int) { ModPT res(*this); --*this; return res; } explicit operator i64() const { return v_; } ModPT inv() const { return ModPT(inv_mod(v_,P)); } }; template ModPT

operator+(ModPT

lhs, ModPT

rhs) { return ModPT

(lhs) += rhs; } template ModPT

operator+(ModPT

lhs, i64 rhs) { return ModPT

(lhs) += rhs; } template ModPT

operator+(i64 lhs, ModPT

rhs) { return ModPT

(rhs) += lhs; } template ModPT

operator-(ModPT

lhs, ModPT

rhs) { return ModPT

(lhs) -= rhs; } template ModPT

operator-(ModPT

lhs, i64 rhs) { return ModPT

(lhs) -= rhs; } template ModPT

operator-(i64 lhs, ModPT

rhs) { return ModPT

(rhs) -= lhs; } template ModPT

operator*(ModPT

lhs, ModPT

rhs) { return ModPT

(lhs) *= rhs; } template ModPT

operator*(ModPT

lhs, i64 rhs) { return ModPT

(lhs) *= rhs; } template ModPT

operator*(i64 lhs, ModPT

rhs) { return ModPT

(rhs) *= lhs; } template bool operator==(ModPT

lhs, ModPT

rhs) { return lhs.v_ == rhs.v_; } template bool operator==(ModPT

lhs, i64 rhs) { return lhs == ModPT

(rhs); } template bool operator==(i64 lhs, ModPT

rhs) { return ModPT

(lhs) == rhs; } template bool operator!=(ModPT

lhs, ModPT

rhs) { return !(lhs == rhs); } template bool operator!=(ModPT

lhs, i64 rhs) { return !(lhs == rhs); } template bool operator!=(i64 lhs, ModPT

rhs) { return !(lhs == rhs); } template istream& operator>>(istream& in, ModPT

& x) { i64 t; in >> t; x = t; return in; } template struct Formatter> { static ostream& write_str(ostream& out, ModPT

x) { return WRITE_STR(out, x.v_); } static ostream& write_repr(ostream& out, ModPT

x) { return WRITE_REPR(out, x.v_); } }; using ModP = ModPT; // }}} //-------------------------------------------------------------------- void solve() { i64 N; RD(N); auto A = RD_ARRAY(N); auto grundy = FIX([](auto&& self, vector& es) -> i64 { HashMap,i64> memo; auto it = memo.find(es); if(it == end(memo)) { HashSet gs; REP(i, SIZE(es)) { if(es[i] >= 1) { --es[i]; gs.emplace(self(es)); ++es[i]; } if(es[i] >= 2) { es[i] -= 2; gs.emplace(self(es)); es[i] += 2; } } i64 res = 0; while(set_contains(gs, res)) ++res; it = memo.emplace_hint(it, es, res); } return SND(*it); }); vector gs(N); REP(i, N) { auto fs = factorize(A[i]); vector es(SIZE(fs)); ALL(transform, fs, begin(es), GENERIC(SND)); gs[i] = grundy(es); } DBG(gs); bool ans = ALL(FOLD1, gs, bit_xor<>()) != 0; PRINTLN(ans ? "Alice" : "Bob"); // * 小さいケースで試した? // * 不可能なケースはチェックした? // * MOD はとった? // * メモ化忘れてない? // * 入出力の 0-based/1-based 確認した? // * 時間/メモリ制限は確認した? // * 違うやつ提出してない? // * 違うやつテストしてない? } signed main() { solve(); EXIT(); }