// https://github.com/miscalculation53/library #define SINGLE_TESTCASE // #define MULTI_TESTCASE // #define AOJ_TESTCASE #ifndef LOCAL #define FAST_IO // #define FAST_CIO // #define INTERACTIVE #endif #define INF 4'000'000'000'000'000'037LL #define EPS 1e-11 #include using namespace std; #if __cplusplus > 202002L && defined(__STDCPP_FLOAT128_T__) #include #elif defined(__SIZEOF_FLOAT128__) #else #error "f128 requires a compiler with IEEE binary128 support, such as GCC on x86_64." #endif #define LIBRARY_HAS_FLOAT128 1 #include using namespace std; using ll = long long; using uint = unsigned int; using ull = unsigned long long; using pll = pair; #define vc vector template using vvc = vc>; using vpll = vc; using vstr = vc; using i128 = __int128_t; using u128 = __uint128_t; #define cauto const auto //! https://gcc.gnu.org/onlinedocs/libquadmath/Math-Library-Routines.html i128 stoi128(const string &s) { const bool neg = s.front() == '-'; u128 res = 0; for (size_t i = size_t(neg); i < s.size(); i++) res = 10 * res + unsigned(s[i] - '0'); if (neg) return -i128(res - 1) - 1; return i128(res); } inline string u128tos(u128 x) { string res; do { res += char('0' + x % 10); x /= 10; } while (x > 0); reverse(res.begin(), res.end()); return res; } string i128tos(i128 x) { return x < 0 ? "-" + u128tos(u128(-(x + 1)) + 1) : u128tos(u128(x)); } istream &operator>>(istream &is, i128 &a) { string s; if (!(is >> s)) return is; a = stoi128(s); return is; } ostream &operator<<(ostream &os, const i128 &a) { os << i128tos(a); return os; } #define overload4(_1,_2,_3,_4,name,...) name #define LIBRARY_REPEAT_LOOP(T,i,l,r,d) for (auto [i, library_repeat_end, library_repeat_step] = array{T(l), T(r), T(d)}; library_repeat_step > 0 ? i < library_repeat_end : i > library_repeat_end; i += library_repeat_step) #define rep1(i,n) LIBRARY_REPEAT_LOOP(ll, i, 0, n, 1) #define rep2(i,l,r) LIBRARY_REPEAT_LOOP(ll, i, l, r, 1) #define rep3(i,l,r,d) LIBRARY_REPEAT_LOOP(ll, i, l, r, d) #define rep(...) overload4(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__) #define repi1(i,n) LIBRARY_REPEAT_LOOP(int, i, 0, n, 1) #define repi2(i,l,r) LIBRARY_REPEAT_LOOP(int, i, l, r, 1) #define repi3(i,l,r,d) LIBRARY_REPEAT_LOOP(int, i, l, r, d) #define repi(...) overload4(__VA_ARGS__, repi3, repi2, repi1)(__VA_ARGS__) #define fe(...) for (auto __VA_ARGS__) #define fec(...) for (cauto &__VA_ARGS__) #define fem(...) for (auto &__VA_ARGS__) template constexpr pair &operator+=(pair &a, const P &b) { a.first += b.first; a.second += b.second; return a; } template constexpr pair operator+(pair a, const P &b) { return a += b; } template constexpr pair &operator-=(pair &a, const P &b) ; template constexpr pair operator-(pair a, const P &b) { return a -= b; } template constexpr pair operator-(pair a) { a.first = -a.first; a.second = -a.second; return a; } template constexpr array &operator+=(array &a, const A &b) { for (size_t i = 0; i < n; i++) a[i] += b[i]; return a; } template constexpr array operator+(array a, const A &b) { return a += b; } template constexpr array &operator-=(array &a, const A &b) ; template constexpr array operator-(array a, const A &b) { return a -= b; } template constexpr array operator-(array a) { for (auto &ai : a) ai = -ai; return a; } namespace internal { template constexpr auto &tuple_add_impl(A &a, const B &b, const index_sequence) ; template constexpr auto &tuple_neg_impl(A &a, const index_sequence) ; }; template constexpr tuple &operator+=(tuple &a, const Tp &b) { return internal::tuple_add_impl(a, b, make_index_sequence>>{}); } template constexpr tuple operator+(tuple a, const Tp &b) { return a += b; } template constexpr tuple &operator-=(tuple &a, const Tp &b) ; template constexpr tuple operator-(tuple a, const Tp &b) { return a -= b; } template constexpr tuple operator-(tuple a) { internal::tuple_neg_impl(a, make_index_sequence{}); return a; } template constexpr bool is_integral_ext = is_integral_v || is_same_v || is_same_v; template constexpr bool is_signed_ext = is_signed_v || is_same_v; template constexpr bool is_unsigned_ext = is_unsigned_v || is_same_v; namespace default_infty_detail { } namespace template_math_detail { } //! https://misawa.github.io/others/avoid_errors/techniques_to_avoid_errors.html #define ALL(a) (a).begin(), (a).end() template inline T SZ(const V &x) { return x.size(); } #define eb emplace_back #define LMD(x,fx) ([&](const auto &x) { return fx; }) template auto gen_vec(int n, const F &f) ; #define GEN_VEC(n,i,fi) (gen_vec(n, LMD(i, fi))) namespace template_vector_detail { } template void fill(V &v, const T &value) { if constexpr (is_assignable_v) std::fill(std::begin(v), std::end(v), value); else for (auto &row : v) fill(row, value); } namespace internal { } namespace internal_monoid_power { } template vvc top(const vvc &a) { if (a.empty()) return {}; const int n = a.size(), m = a[0].size(); vvc b(m, vc(n)); repi(i, n) { assert(SZ(a[i]) == m); repi(j, m) b[j][i] = a[i][j]; } return b; } vstr top(const vstr &a) { vvc a_(a.size()); repi(i, SZ(a)) a_[i] = {ALL(a[i])}; vvc b_ = top(a_); vstr b(b_.size()); repi(i, SZ(b)) b[i] = {ALL(b_[i])}; return b; } constexpr array DRULgrid = {{{1, 0}, {0, 1}, {-1, 0}, {0, -1}}}; constexpr array DRULplane = {{{0, -1}, {1, 0}, {0, 1}, {-1, 0}}}; template struct is_random_access_iterator { static constexpr bool value = is_same_v< typename iterator_traits::iterator_category, random_access_iterator_tag >; }; template constexpr bool is_random_access_iterator_v = is_random_access_iterator::value; #define DEFAULT_COMP ranges::less namespace internal { }; inline constexpr ll countr_zero(ll x) { assert(x != 0); return std::countr_zero((ull)x); } inline constexpr ull lsb_pos(ull x) { assert(x != 0); return countr_zero(x); } inline constexpr bool btest(ull x, uint k) { return (x >> k) & 1; } #define CPP_DUMP_DEFINE_DATA(...) #define dump(...) ((void)0) #define local(...) #define oj(...) __VA_ARGS__ #define local_oj(a,b) (b) template vc content(queue que) { vc res; while (!que.empty()) { res.eb(que.front()); que.pop(); } return res; } template vc content(priority_queue pque) { vc res; while (!pque.empty()) { res.eb(pque.top()); pque.pop(); } return res; } template auto content(const T &obj) { return obj.content(); } namespace fastio { constexpr uint32_t SIZ = 1 << 17; inline char ibuf[SIZ + 1], obuf[SIZ]; inline uint32_t pil = 0, pir = 0, por = 0; __attribute__((noinline)) inline void load() { const uint32_t remaining = pir - pil; if (remaining && pil) memmove(ibuf, ibuf + pil, remaining); pil = 0; pir = remaining + uint32_t(fread(ibuf + remaining, 1, SIZ - remaining, stdin)); ibuf[pir] = '\x7f'; } inline bool ascii_space(unsigned char c) { return c == ' ' || unsigned(c - '\t') < 5; } inline void find_token() { for (;;) { const char *p = ibuf + pil; const char *end = ibuf + pir; while (ascii_space(*p)) ++p; pil = uint32_t(p - ibuf); if (p != end) return; load(); if (!pir) abort(); } } __attribute__((noinline)) inline void flush() { size_t written = 0; while (written < por) { const size_t n = fwrite(obuf + written, 1, por - written, stdout); if (!n) abort(); written += n; } por = 0; } inline void append_bytes(const char *data, size_t size) { while (size) { const size_t n = min(size, size_t(SIZ - por)); memcpy(obuf + por, data, n); por += uint32_t(n); data += n; size -= n; if (por == SIZ) flush(); } } inline void append_char(char c) { if (por == SIZ) flush(); obuf[por++] = c; } template using integer_magnitude = conditional_t<(sizeof(T) > sizeof(ull)), u128, conditional_t<(sizeof(T) > sizeof(uint)), ull, uint>>; inline constexpr uint powers_of_ten[8] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000}; template __attribute__((always_inline)) inline void read_wide_integer_tail( U &value, const char *&p, uint64_t columns, uint64_t stop) ; template __attribute__((always_inline)) inline void rd1_integer(T &x) { using U = integer_magnitude; const char *p = ibuf + pil; for (;;) { while (ascii_space(*p)) ++p; if (p != ibuf + pir) break; pil = pir; load(); if (!pir) abort(); p = ibuf; } bool negative = false; if (*p == '+') ++p; else if constexpr (numeric_limits::is_signed) { if (*p == '-') negative = true, ++p; } U value = 0; for (;;) { while (size_t(ibuf + pir - p) >= 8) { uint64_t columns; memcpy(&columns, p, 8); const uint64_t stop = ((columns + 0x4646464646464646ULL) | (columns - 0x3030303030303030ULL)) & 0x8080808080808080ULL; if (stop) { if constexpr (sizeof(U) > sizeof(ull)) { read_wide_integer_tail(value, p, columns, stop); goto integer_complete; } else { if (stop & 0x80808080ULL) { if (!(stop & 0x808080ULL)) { value = value * 1000 + uint(columns & 15) * 100 + uint(columns >> 8 & 15) * 10 + uint(columns >> 16 & 15); p += 3; } else if (!(stop & 0x8080ULL)) { value = value * 100 + uint(columns & 15) * 10 + uint(columns >> 8 & 15); p += 2; } else if (!(stop & 0x80ULL)) { value = value * 10 + uint(columns & 15); ++p; } goto integer_complete; } break; } } columns &= 0x0f0f0f0f0f0f0f0fULL; const uint64_t pairs = (columns & 0x00ff00ff00ff00ffULL) * 10 + ((columns >> 8) & 0x00ff00ff00ff00ffULL); const uint64_t quads = (pairs & 0x0000ffff0000ffffULL) * 100 + ((pairs >> 16) & 0x0000ffff0000ffffULL); value = value * 100000000 + uint(quads) * 10000 + uint(quads >> 32); p += 8; } while (unsigned(*p - '0') < 10) { value = value * 10 + unsigned(*p - '0'); ++p; } pil = uint32_t(p - ibuf); if (pil != pir) break; load(); p = ibuf; if (!pir) break; } integer_complete: pil = uint32_t(p - ibuf); if constexpr (numeric_limits::is_signed) { if (negative && value) x = T(-T(value - 1) - 1); else x = T(value); } else x = T(value); } template ::is_integer, int> = 0> __attribute__((always_inline)) inline void rd1(T &x) { rd1_integer(x); } inline void rd1(char &x) ; inline void rd1(string &x) { find_token(); x.clear(); for (;;) { const char *p = ibuf + pil; const char *end = ibuf + pir; const char *first = p; while (p != end && !ascii_space(*p)) ++p; x.append(first, size_t(p - first)); pil = uint32_t(p - ibuf); if (p != end) return; load(); if (!pir) return; } } inline void rd1(float &x) ; inline void rd1(double &x) ; inline void rd1(long double &x) ; template void rd1(pair &x); template void rd1(tuple &x); template void rd1(array &x); template void rd1(vc &x); template void rd1(pair &x) ; template void rd1(tuple &x) ; template void rd1(array &x) ; template void rd1(vc &x) { if constexpr (is_same_v) for (size_t i = 0; i < x.size(); ++i) { bool value; rd1(value); x[i] = value; } else for (auto &element : x) rd1(element); } inline constexpr auto decimal_triplets = [] { array, 1000> result{}; for (unsigned i = 0; i < 1000; ++i) { result[i][0] = char('0' + i / 100); result[i][1] = char('0' + i / 10 % 10); result[i][2] = char('0' + i % 10); } return result; }(); inline void store_nine(char *out, uint x) { const uint q = x / 1000, r = q / 1000; memcpy(out, decimal_triplets[r].data(), 3); memcpy(out + 3, decimal_triplets[q - r * 1000].data(), 3); memcpy(out + 6, decimal_triplets[x - q * 1000].data(), 3); } inline char *store_leading_triplet(char *out, uint x) { if (x >= 100) { memcpy(out, decimal_triplets[x].data(), 3); return out + 3; } if (x >= 10) { memcpy(out, decimal_triplets[x].data() + 1, 2); return out + 2; } *out = char('0' + x); return out + 1; } inline char *store_short(char *out, uint x) { if (x >= 1000000) { const uint q = x / 1000000; const uint r = x - q * 1000000; out = store_leading_triplet(out, q); memcpy(out, decimal_triplets[r / 1000].data(), 3); memcpy(out + 3, decimal_triplets[r % 1000].data(), 3); return out + 6; } if (x >= 1000) { const uint q = x / 1000; out = store_leading_triplet(out, q); memcpy(out, decimal_triplets[x - q * 1000].data(), 3); return out + 3; } return store_leading_triplet(out, x); } template inline char *store_ordinary(char *out, U x) { if constexpr (sizeof(U) > sizeof(uint)) { if (x >= 1000000000000000000ULL) { const uint hi = uint(x / 1000000000000000000ULL); const ull tail = x - ull(hi) * 1000000000000000000ULL; const uint middle = uint(tail / 1000000000ULL); out = store_leading_triplet(out, hi); store_nine(out, middle); store_nine(out + 9, uint(tail - ull(middle) * 1000000000ULL)); return out + 18; } } if (x >= 1000000000) { const uint hi = uint(x / 1000000000); out = store_short(out, hi); store_nine(out, uint(x - U(hi) * 1000000000)); return out + 9; } return store_short(out, uint(x)); } template __attribute__((always_inline)) inline void write_wide_integer(U magnitude, bool negative) ; template __attribute__((always_inline)) inline void wt1_integer(T x) { using U = integer_magnitude; U magnitude = U(x); bool negative = false; if constexpr (numeric_limits::is_signed) { negative = x < 0; const U sign_mask = U(0) - U(negative); magnitude = (magnitude ^ sign_mask) + U(negative); } if constexpr (sizeof(U) <= sizeof(ull)) { if (por > SIZ - 40) flush(); char *out = obuf + por; *out = '-'; out += negative; out = store_ordinary(out, magnitude); if constexpr (newline) *out++ = '\n'; por = uint32_t(out - obuf); return; } else write_wide_integer(magnitude, negative); } template ::is_integer, int> = 0> __attribute__((always_inline)) inline void wt1(T x) { wt1_integer(x); } inline void wt1(char x) ; inline void wt1(const char *x) ; inline void wt1(string_view x) ; inline void wt1(const string &x) { append_bytes(x.data(), x.size()); } inline void wt1(float x) ; inline void wt1(double x) ; inline void wt1(long double x) ; template void wt1(const pair &x); template void wt1(const tuple &x); template void wt1(const array &x); template void wt1(const vc &x); template void wt1(const pair &x) { wt1(x.first), append_char(' '), wt1(x.second); } template void wt1(const tuple &x) ; template void wt1(const array &x) ; template void wt1(const vc &x) ; template void read(T &...x) { (rd1(x), ...); } template void write(T &&...x) { (wt1(forward(x)), ...); } template void print(T &&...x) { if constexpr (sizeof...(T) == 1 && ((is_integral_v> && !is_same_v, char>) && ...)) (wt1_integer, true>(forward(x)), ...); else { size_t index = 0; ((index++ ? void(append_char(' ')) : void(), wt1(forward(x))), ...); append_char('\n'); } } } struct Dummy { Dummy() { atexit(fastio::flush); } } dummy; //! https://trap.jp/post/1224/ namespace internal { template void READnodump(Ts &...a) { fastio::read(a...); } template void READVECnodump(int n, vc &...v) ; template void READVEC2nodump(int n, int m, vvc &...v) { (v.assign(size_t(n), vc(size_t(m))), ...); READnodump(v...); } template void READJAGnodump(int n, vvc &...vs) ; }; #define READ(...) internal::READnodump(__VA_ARGS__); dump(__VA_ARGS__) #define LIBRARY_INOUT_TYPE(...) LIBRARY_INOUT_TYPE_IMPL(__VA_ARGS__)(__VA_ARGS__) #define LIBRARY_INOUT_TYPE_IMPL(T,...) LIBRARY_INOUT_TYPE_SELECT(LIBRARY_INOUT_TYPE_PROBE T, LIBRARY_INOUT_TYPE_KEEP) #define LIBRARY_INOUT_TYPE_PROBE(...) _, LIBRARY_INOUT_TYPE_REMOVE #define LIBRARY_INOUT_TYPE_SELECT(...) LIBRARY_INOUT_TYPE_SECOND(__VA_ARGS__) #define LIBRARY_INOUT_TYPE_SECOND(_0,_1,...) _1 #define LIBRARY_INOUT_TYPE_KEEP(...) __VA_ARGS__ #define LIBRARY_INOUT_TYPE_REMOVE(T) LIBRARY_INOUT_TYPE_KEEP T #define IN(T,...) LIBRARY_INOUT_TYPE(T) __VA_ARGS__; READ(__VA_ARGS__) #define CHAR(...) IN(char, __VA_ARGS__) #define INT(...) IN(int, __VA_ARGS__) #define LL(...) IN(ll, __VA_ARGS__) #define STR(...) IN(string, __VA_ARGS__) #define ARR(T,n,...) array __VA_ARGS__; READ(__VA_ARGS__) #define READVEC(...) internal::READVECnodump(__VA_ARGS__); dump(__VA_ARGS__) #define READVEC2(...) internal::READVEC2nodump(__VA_ARGS__); dump(__VA_ARGS__) #define VEC(T,n,...) vc __VA_ARGS__; READVEC(n, __VA_ARGS__) #define VEC2(T,n,m,...) vvc __VA_ARGS__; READVEC2(n, m, __VA_ARGS__) #define READJAG(...) internal::READJAGnodump(__VA_ARGS__); dump(__VA_ARGS__) #define JAG(T,n,...) vvc __VA_ARGS__; READJAG(n, __VA_ARGS__) #define ENDL '\n' #define WRITE fastio::write #define PRINT fastio::print #define PRINTEXIT(...) do { PRINT(__VA_ARGS__); exit(0); } while (false) #define PRINTRETURN(...) do { PRINT(__VA_ARGS__); return; } while (false) template void PRINTV(const vc &v) { for (auto &vi : v) PRINT(vi); } #define PRINTVEXIT(...) do { PRINTV(__VA_ARGS__); exit(0); } while (false) #define PRINTVRETURN(...) do { PRINTV(__VA_ARGS__); return; } while (false) template void offset(vc &v, const Add &add) { for (auto &vi : v) vi += add; } template void offset(vvc &v, const Add &add) ; template array, m> unzip(const vc> &vt) ; template pair, vc> unzip(const vc> &vt) ; namespace internal { }; template auto unzip(const vc> &vt) ; namespace internal { template auto zip_vectors(const vc &...v) ; }; #define UNZIP(vt,...) auto [__VA_ARGS__] = unzip(vt) #define ZIP(vt,...) auto vt = internal::zip_vectors(__VA_ARGS__) mt19937_64 mt; namespace internal { }; /*! * Portions corresponding to AtCoder Library (CC0-1.0): modular arithmetic operators. * Upstream: atcoder/modint.hpp * https://github.com/atcoder/ac-library/tree/864245a00b00dd008d1abfdc239618fdb7d139da * License: https://github.com/atcoder/ac-library/blob/864245a00b00dd008d1abfdc239618fdb7d139da/LICENSE */ /*! * Portions corresponding to AtCoder Library (CC0-1.0): primality testing. * Upstream: atcoder/internal_math.hpp * https://github.com/atcoder/ac-library/tree/864245a00b00dd008d1abfdc239618fdb7d139da * License: https://github.com/atcoder/ac-library/blob/864245a00b00dd008d1abfdc239618fdb7d139da/LICENSE */ /*! * Portions corresponding to AtCoder Library (CC0-1.0): Barrett reduction. * Upstream: atcoder/internal_math.hpp * https://github.com/atcoder/ac-library/tree/864245a00b00dd008d1abfdc239618fdb7d139da * License: https://github.com/atcoder/ac-library/blob/864245a00b00dd008d1abfdc239618fdb7d139da/LICENSE */ //! https://www.mathenachia.blog/even-mod-montgomery-impl/ // using mint = modint1000000007; // using mint = static_modint<1000000000>; // using mint = modint; void init() { oj(mt.seed(random_device()())); } struct DynamicBitset { using Word = ull; static constexpr int word_bits = numeric_limits::digits; private: int n; vc dat; static int word_size(int n) { return (n + word_bits - 1) / word_bits; } static Word low_mask(int k) { return k == word_bits ? ~Word(0) : (Word(1) << k) - 1; } Word last_mask() const { return n % word_bits == 0 ? ~Word(0) : low_mask(n % word_bits); } void trim() { if (!dat.empty()) dat.back() &= last_mask(); } Word get_word(int i) const { int w = i / word_bits, b = i % word_bits; Word x = dat[w] >> b; if (b && w + 1 < SZ(dat)) x |= dat[w + 1] << (word_bits - b); return x; } template DynamicBitset &apply_slice(int l, int r, const DynamicBitset &b, int bl, const F &f) { assert(0 <= l && l <= r && r <= n); assert(0 <= bl && bl <= b.n); r = l + min(r - l, b.n - bl); if (l == r) return *this; if constexpr (op == 2) { if (this == &b && bl == 0 && l > 0) { int dw = l / word_bits, db = l % word_bits; int lw = l / word_bits, rw = (r - 1) / word_bits; repi(w, rw, lw - 1, -1) { Word x = dat[w - dw] << db; if (db && w > dw) x |= dat[w - dw - 1] >> (word_bits - db); int lo = max(l, w * word_bits), hi = min(r, (w + 1) * word_bits); Word mask = low_mask(hi - lo) << (lo % word_bits); Word added = x & ~dat[w] & mask; dat[w] |= x & mask; if constexpr (!is_same_v) { while (added) { f(w * word_bits + __builtin_ctzll(added)); added &= added - 1; } } } return *this; } } auto apply_word = [&](int w) { int lo = max(l, w * word_bits), hi = min(r, (w + 1) * word_bits); int len = hi - lo, shift = lo % word_bits; Word mask = low_mask(len) << shift; Word x = (b.get_word(bl + lo - l) & low_mask(len)) << shift; Word old = dat[w], y; if constexpr (op == 0) y = x; else if constexpr (op == 1) y = old & x; else if constexpr (op == 2) y = old | x; else y = old ^ x; dat[w] = (old & ~mask) | (y & mask); if constexpr (!is_same_v) { Word added = dat[w] & ~old & mask; while (added) { f(w * word_bits + __builtin_ctzll(added)); added &= added - 1; } } }; int lw = l / word_bits, rw = (r - 1) / word_bits; if constexpr (op == 2 && is_same_v) { if (this != &b && lw < rw) { apply_word(lw); apply_word(rw); int si = bl + (lw + 1) * word_bits - l; int sw = si / word_bits, sb = si % word_bits; if (sb == 0) repi(w, lw + 1, rw) dat[w] |= b.dat[sw++]; else repi(w, lw + 1, rw) { dat[w] |= (b.dat[sw] >> sb) | (b.dat[sw + 1] << (word_bits - sb)); sw++; } return *this; } } if (this == &b && bl < l && l < bl + r - l) repi(w, rw, lw - 1, -1) apply_word(w); else repi(w, lw, rw + 1) apply_word(w); return *this; } public: template class OnesRange; class OnesIterator { template friend class OnesRange; const vc *words = nullptr; int index = 0; Word pending = 0; void skip_empty() { while (index < SZ(*words)) { pending = (*words)[index]; if (pending) return; ++index; } } OnesIterator(const vc *words, int index) : words(words), index(index) { skip_empty(); } public: using iterator_category = input_iterator_tag; using value_type = int; using difference_type = ptrdiff_t; using reference = int; using pointer = void; OnesIterator() = default; int operator*() const { assert(pending != 0); return index * word_bits + __builtin_ctzll(pending); } OnesIterator &operator++() ; OnesIterator operator++(int) ; bool operator==(const OnesIterator &other) const { return words == other.words && index == other.index && pending == other.pending; } bool operator!=(const OnesIterator &other) const { return !(*this == other); } }; template class OnesRange { friend struct DynamicBitset; Storage storage; explicit OnesRange(Storage storage) : storage(std::move(storage)) {} const vc &words() const ; public: using iterator = OnesIterator; using const_iterator = OnesIterator; iterator begin() const { return iterator(&words(), 0); } iterator end() const { return iterator(&words(), SZ(words())); } const_iterator cbegin() const { return begin(); } const_iterator cend() const { return end(); } }; struct Reference { DynamicBitset *bs; int pos; operator bool() const { return bs->test(pos); } Reference &operator=(bool value) { bs->set(pos, value); return *this; } Reference &operator=(const Reference &ref) { return *this = bool(ref); } Reference &flip() ; }; DynamicBitset() : n(0) {} DynamicBitset(int n, bool value = false) : n(n), dat(n < 0 ? 0 : word_size(n), value ? ~Word(0) : 0) { assert(n >= 0); trim(); } explicit DynamicBitset(const string &s, char zero = '0', char one = '1') : n(0) { assign(s, zero, one); } int size() const { return n; } int word_count() const { return dat.size(); } Word word(int i) const { assert(0 <= i && i < word_count()); return dat[i]; } DynamicBitset &set_word(int i, Word value) { assert(0 <= i && i < word_count()); dat[i] = value; if (i + 1 == word_count()) trim(); return *this; } bool empty() const ; int capacity() const { return int(dat.capacity()) * word_bits; } void reserve(int n) ; void resize(int n, bool value = false) { assert(n >= 0); int old = this->n; dat.resize(word_size(n)); this->n = n; if (value && old < n) set_range(old, n); trim(); } DynamicBitset &assign(const string &s, char zero = '0', char one = '1') { assert(zero != one); n = int(s.size()); dat.assign(word_size(n), 0); repi(i, n) { char c = s[n - 1 - i]; assert(c == zero || c == one); if (c == one) dat[i / word_bits] |= Word(1) << (i % word_bits); } return *this; } bool test(int i) const { assert(0 <= i && i < n); return dat[i / word_bits] >> (i % word_bits) & 1; } bool operator[](int i) const ; Reference operator[](int i) ; DynamicBitset &set(int i, bool value = true) ; DynamicBitset &reset(int i) ; DynamicBitset &flip(int i) ; DynamicBitset &set() ; DynamicBitset &reset() ; DynamicBitset &flip() ; DynamicBitset &set_range(int l, int r, bool value = true) { assert(0 <= l && l <= r && r <= n); if (!value) return reset_range(l, r); if (l == r) return *this; int lw = l / word_bits, rw = (r - 1) / word_bits; if (lw == rw) dat[lw] |= low_mask((r - 1) % word_bits + 1) & ~low_mask(l % word_bits); else { dat[lw] |= ~low_mask(l % word_bits); fill(dat.begin() + lw + 1, dat.begin() + rw, ~Word(0)); dat[rw] |= low_mask((r - 1) % word_bits + 1); } trim(); return *this; } DynamicBitset &reset_range(int l, int r) { assert(0 <= l && l <= r && r <= n); if (l == r) return *this; int lw = l / word_bits, rw = (r - 1) / word_bits; if (lw == rw) dat[lw] &= ~(low_mask((r - 1) % word_bits + 1) & ~low_mask(l % word_bits)); else { dat[lw] &= low_mask(l % word_bits); fill(dat.begin() + lw + 1, dat.begin() + rw, 0); dat[rw] &= ~low_mask((r - 1) % word_bits + 1); } return *this; } DynamicBitset &flip_range(int l, int r) { assert(0 <= l && l <= r && r <= n); if (l == r) return *this; int lw = l / word_bits, rw = (r - 1) / word_bits; if (lw == rw) dat[lw] ^= low_mask((r - 1) % word_bits + 1) & ~low_mask(l % word_bits); else { dat[lw] ^= ~low_mask(l % word_bits); repi(w, lw + 1, rw) dat[w] = ~dat[w]; dat[rw] ^= low_mask((r - 1) % word_bits + 1); } trim(); return *this; } int count() const ; int count(int l, int r) const ; bool any() const ; bool any(int l, int r) const ; bool none() const ; bool none(int l, int r) const ; bool all() const ; bool all(int l, int r) const ; int find_next(int i, bool value = true) const { assert(-1 <= i && i < n); i++; if (i == n) return n; int w = i / word_bits, b = i % word_bits; Word x = (value ? dat[w] : ~dat[w]) & (~Word(0) << b); while (true) { if (x) { int res = w * word_bits + __builtin_ctzll(x); return min(res, n); } if (++w == SZ(dat)) return n; x = value ? dat[w] : ~dat[w]; } } int find_last(bool value = true) const { return find_prev(n, value); } int find_prev(int i, bool value = true) const { assert(0 <= i && i <= n); if (i == 0) return -1; i--; int w = i / word_bits, b = i % word_bits; Word x = (value ? dat[w] : ~dat[w]) & low_mask(b + 1); while (true) { if (x) return w * word_bits + 63 - __builtin_clzll(x); if (w-- == 0) return -1; x = value ? dat[w] : ~dat[w]; } } DynamicBitset &operator&=(const DynamicBitset &b) { assert(n == b.n); repi(i, SZ(dat)) dat[i] &= b.dat[i]; return *this; } DynamicBitset &operator|=(const DynamicBitset &b) { assert(n == b.n); repi(i, SZ(dat)) dat[i] |= b.dat[i]; return *this; } DynamicBitset &operator^=(const DynamicBitset &b) { assert(n == b.n); repi(i, SZ(dat)) dat[i] ^= b.dat[i]; return *this; } friend DynamicBitset operator&(DynamicBitset a, const DynamicBitset &b) { return a &= b; } friend DynamicBitset operator|(DynamicBitset a, const DynamicBitset &b) { return a |= b; } friend DynamicBitset operator^(DynamicBitset a, const DynamicBitset &b) { return a ^= b; } DynamicBitset operator~() const { return DynamicBitset(*this).flip(); } DynamicBitset &operator<<=(int k) { assert(k >= 0); if (k >= n) return reset(); if (k == 0) return *this; int dw = k / word_bits, db = k % word_bits; repi(i, SZ(dat) - 1, dw - 1, -1) { Word x = dat[i - dw] << db; if (db && i > dw) x |= dat[i - dw - 1] >> (word_bits - db); dat[i] = x; } fill(dat.begin(), dat.begin() + dw, 0); trim(); return *this; } DynamicBitset &operator>>=(int k) { assert(k >= 0); if (k >= n) return reset(); if (k == 0) return *this; int dw = k / word_bits, db = k % word_bits, m = SZ(dat); repi(i, m - dw) { Word x = dat[i + dw] >> db; if (db && i + dw + 1 < m) x |= dat[i + dw + 1] << (word_bits - db); dat[i] = x; } fill(dat.end() - dw, dat.end(), 0); trim(); return *this; } DynamicBitset operator<<(int k) const ; DynamicBitset operator>>(int k) const ; DynamicBitset rotated_left(int k) const { if (n == 0) return {}; k %= n; if (k < 0) k += n; if (k == 0) return *this; DynamicBitset res(n); int p = n - k; repi(i, SZ(dat)) { Word x = get_word(p); const int tail = n - p; if (tail < word_bits) x |= dat[0] << tail; res.dat[i] = x; p += word_bits; if (p >= n) p -= n; } res.trim(); return res; } [[gnu::always_inline]] DynamicBitset &xor_slice(int l, int r, const DynamicBitset &b, int bl) { if (l == bl && l % word_bits == 0 && r == n && n == b.n) { assert(0 <= l && l <= r); repi(w, l / word_bits, SZ(dat)) dat[w] ^= b.dat[w]; return *this; } return apply_slice<3>(l, r, b, bl, nullptr); } void push_back(bool value) { int i = n; resize(n + 1); if (value) set(i); } void pop_back() { assert(n > 0); resize(n - 1); } string to_string(char zero = '0', char one = '1') const { assert(zero != one); string res(n, zero); repi(i, n) if (test(i)) res[n - 1 - i] = one; return res; } vc content() const { vc res(n); repi(i, n) res[i] = test(i); return res; } friend bool operator==(const DynamicBitset &a, const DynamicBitset &b) { return a.n == b.n && a.dat == b.dat; } friend bool operator!=(const DynamicBitset &a, const DynamicBitset &b) { return !(a == b); } friend bool operator<(const DynamicBitset &a, const DynamicBitset &b) { assert(a.n == b.n); repi(i, SZ(a.dat) - 1, -1, -1) if (a.dat[i] != b.dat[i]) return a.dat[i] < b.dat[i]; return false; } friend bool operator>(const DynamicBitset &a, const DynamicBitset &b) { return b < a; } friend bool operator<=(const DynamicBitset &a, const DynamicBitset &b) { return !(b < a); } friend bool operator>=(const DynamicBitset &a, const DynamicBitset &b) { return !(a < b); } }; void rd1(DynamicBitset &a) { string s; fastio::rd1(s); a.assign(s); } void wt1(const DynamicBitset &a) { fastio::wt1(a.to_string()); } namespace internal { template struct BasisTag { using Type = Tag; Tag tag; explicit BasisTag(Tag tag) : tag(std::move(tag)) {} bool accepts(const Tag *threshold) const { return !threshold || !(tag < *threshold); } }; template <> struct BasisTag { using Type = monostate; explicit BasisTag(monostate = {}) {} bool accepts(const monostate *) const ; }; } namespace internal { template struct XorBasisVector; template struct XorBasisVector && is_unsigned_ext && !is_same_v>> { static constexpr int default_dimension = numeric_limits::digits; static_assert(default_dimension <= 128); using Coefficient = V; static bool valid(const V &value, int dimension) { return 0 <= dimension && dimension <= default_dimension && (dimension == default_dimension || (value >> dimension) == V(0)); } static V zero(int) { return V(0); } static bool empty(const V &value) { return value == V(0); } static bool test(const V &value, int i) { return (value >> i) & V(1); } static int top(const V &value) { if (value == V(0)) return -1; if constexpr (default_dimension <= 64) return 63 - __builtin_clzll(ull(value)); else { ull high = ull(value >> 64); return high ? 127 - __builtin_clzll(high) : 63 - __builtin_clzll(ull(value)); } } static Coefficient coefficient_zero(int) { return V(0); } static Coefficient coefficient_unit(int i) { return V(1) << i; } static void coefficient_add(Coefficient &a, const Coefficient &b) { a ^= b; } }; template <> struct XorBasisVector { static constexpr int default_dimension = 0; using Coefficient = DynamicBitset; static bool valid(const DynamicBitset &value, int dimension) ; static DynamicBitset zero(int dimension) ; static bool empty(const DynamicBitset &value) ; static bool test(const DynamicBitset &value, int i) { return value.test(i); } static int top(const DynamicBitset &value) ; static Coefficient coefficient_zero(int rank) ; static Coefficient coefficient_unit(int i) ; static void coefficient_add(Coefficient &a, const Coefficient &b) ; }; } template class XorBasis { template friend class XorBasis; using Traits = internal::XorBasisVector; using TagData = internal::BasisTag; using StoredTag = typename TagData::Type; static constexpr bool tagged = !is_void_v; public: using Value = V; using Coefficient = typename Traits::Coefficient; using TagType = Tag; private: using StoredCoefficient = conditional_t; struct Row : TagData { int pivot; V value; StoredCoefficient coefficient; Row(int pivot, V value, StoredCoefficient coefficient, StoredTag tag) : TagData(std::move(tag)), pivot(pivot), value(std::move(value)), coefficient(std::move(coefficient)) {} }; int dimension_; bool use_tags_ = false; vc rows_; vc original_; V reduce_impl(V value, const StoredTag *threshold, Coefficient *coefficient = nullptr) const { assert(Traits::valid(value, dimension_)); for (const auto &row : rows_) if (row.accepts(threshold) && Traits::test(value, row.pivot)) { value ^= row.value; if constexpr (Restore) if (coefficient) Traits::coefficient_add(*coefficient, row.coefficient); } return value; } optional restore_impl(V value, const StoredTag *threshold) const { Coefficient coefficient = Traits::coefficient_zero(rank()); if (!Traits::empty(reduce_impl(std::move(value), threshold, &coefficient))) return nullopt; return coefficient; } template bool insert_impl(const V &value, StoredTag tag) { assert(Traits::valid(value, dimension_)); if constexpr (!UseTags) if (rank() == dimension_) return false; V residual = value; StoredCoefficient coefficient; if constexpr (Restore) { optional c; if constexpr (UseTags) { bool may_swap = any_of(rows_.begin(), rows_.end(), [&](const Row &row) { return row.tag < tag; }); if (!may_swap && rank() == dimension_) return false; if (may_swap) c = restore_impl(value, nullptr); } coefficient = c ? std::move(*c) : Traits::coefficient_unit(rank()); } int pivot = Traits::top(residual); auto it = rows_.begin(); for (; it != rows_.end() && pivot != -1; ++it) { if (pivot < it->pivot) continue; if (pivot > it->pivot) break; if constexpr (UseTags) if (it->tag < tag) { swap(residual, it->value); swap(tag, it->tag); if constexpr (Restore) swap(coefficient, it->coefficient); } residual ^= it->value; if constexpr (Restore) Traits::coefficient_add(coefficient, it->coefficient); pivot = Traits::top(residual); } if (pivot == -1) return false; rows_.emplace(it, pivot, std::move(residual), std::move(coefficient), std::move(tag)); original_.push_back(value); return true; } template V extreme_impl(V value, const StoredTag *threshold, int start = 0) const ; template static void add_power(Index &value, int exponent) ; template optional kth_impl(Index k, V value, const StoredTag *threshold) const ; template Index count_impl(const V &key, V value, const StoredTag *threshold) const ; template optional neighbor_impl(const V &key, V value, bool strict, const StoredTag *threshold) const ; public: explicit XorBasis(int dimension = Traits::default_dimension) : dimension_(dimension) { assert(dimension >= 0); assert(Traits::valid(Traits::zero(dimension), dimension)); } XorBasis(int dimension, const vc &vectors) : XorBasis(dimension) { for (const auto &value : vectors) insert(value); } explicit XorBasis(const vc &vectors) : XorBasis([&] { if constexpr (is_same_v) return vectors.empty() ? 0 : vectors[0].size(); else return Traits::default_dimension; }(), vectors) {} CPP_DUMP_DEFINE_DATA(dimension_, use_tags_, rows_, original_); int dimension() const ; int rank() const { return int(rows_.size()); } const vc &basis() const ; void clear() { rows_.clear(), original_.clear(), use_tags_ = false; } bool insert(const V &value) { if constexpr (tagged) { if constexpr (!is_trivially_default_constructible_v) return insert(value, StoredTag{}); else if (use_tags_) return insert_impl(value, StoredTag{}); } return insert_impl(value, StoredTag{}); } V reduce(V value) const ; bool contains(const V &value) const ; template = 0> optional restore(V value) const { return restore_impl(std::move(value), nullptr); } V min_element(V affine) const ; V min_element() const ; V max_element(V affine) const ; V max_element() const ; template Index span_size() const ; template = 0> bool insert(const V &value, StoredTag tag) ; template = 0> int rank(const StoredTag &threshold) const ; template = 0> V reduce(V value, const StoredTag &threshold) const ; template = 0> bool contains(const V &value, const StoredTag &threshold) const ; template = 0> optional restore(V value, const StoredTag &threshold) const ; template = 0> V min_element(V value, const StoredTag &threshold) const ; template = 0> V max_element(V value, const StoredTag &threshold) const ; template = 0> Index span_size(const StoredTag &threshold) const ; template = 0> XorBasis basis(const StoredTag &threshold) const ; template void merge(const XorBasis &other) ; template XorBasis intersection(const XorBasis &other) const ; template XorBasis operator|(const XorBasis &other) const { auto result = *this; result.merge(other); return result; } template XorBasis operator&(const XorBasis &other) const { return intersection(other); } }; void main2() { LL(H, W); VEC2(ll, H, W, A); XorBasis basis; vpll ijs; rep(i, H) rep(j, W) if (basis.insert(A[i][j])) ijs.eb(i, j); dump(ijs | cp::index()); LL(Q); rep(_, Q) { LL(x); auto res = basis.restore(x); dump(res | cp::bin(ijs.size())); if (!res) { PRINT(-1); continue; } if (res.value() == 0) { PRINT(3); PRINT(1, 1); PRINT(2, 1); PRINT(1, 1); PRINT(2, 1); continue; } vpll ans; auto [i, j] = ijs[lsb_pos(res.value())]; ans.eb(i, j); rep(pos, lsb_pos(res.value()) + 1, 64) if (btest(res.value(), pos)) { auto [ni, nj] = ijs[pos]; if (i == ni && j == nj) { ans.eb(i, j == 0 ? 1 : 0); ans.eb(i, j); ans.eb(i, j == 0 ? 1 : 0); } else if (i == ni || j == nj) ans.eb(ni, nj); else { ans.eb(ni, j); ans.eb(ni, nj); ans.eb(ni, j); } tie(i, j) = ans.back(); } offset(ans, pll{1, 1}); PRINT(SZ(ans) - 1); // assert(0 <= SZ(ans) - 1 && SZ(ans) - 1 <= 2 * H * W - 1); PRINTV(ans); } } void test() { } template struct Main { Main() { cauto CERR = [](string val, string color) { string s = "\033[" + color + "m" + val + "\033[m"; }; CERR("\n[FAST_IO]\n\n", "32"); cout << fixed << setprecision(20); init(); CERR("\n[SINGLE_TESTCASE]\n\n", "36"); main2(); } }; Main main_dummy; int main() {} // submit-code: hash=24bf5848e64b source=24bf5848e64bf4ee72feed5cd043428df7b013117cd1979bf81d14b3ee06f503 input=8b3a0eb1582a1e581d14188c5fbf78dd06f0f21cdb9071f533b2097fbdc386d2 body=c197005769fa4787d4755414537c0f3fa945d457e35ffd8b4c7ed3450691a343