#define INF 4'000'000'000'000'000'037LL #define EPS 1e-11 #include using namespace std; namespace { using ld = decltype(EPS); using ll = long long; using uint = unsigned int; using ull = unsigned long long; using pll = pair; using tlll = tuple; using tllll = tuple; #define vc vector template using vvc = vc>; using vpll = vc; using vstr = vc; #ifdef __SIZEOF_INT128__ using i128 = __int128_t; using u128 = __uint128_t; #endif #define cauto const auto #define overload4(_1, _2, _3, _4, name, ...) name #define rep1(i, n) for (ll i = 0, nnnnn = ll(n); i < nnnnn; i++) #define rep(...) overload4(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__) #define repi1(i, n) for (int i = 0, nnnnn = int(n); i < nnnnn; i++) #define repi2(i, l, r) for (int i = int(l), rrrrr = int(r); i < rrrrr; i++) #define repi3(i, l, r, d) for (int i = int(l), rrrrr = int(r), ddddd = int(d); ddddd > 0 ? i < rrrrr : i > rrrrr; i += d) #define repi(...) overload4(__VA_ARGS__, repi3, repi2, repi1)(__VA_ARGS__) #define fe(...) for (auto __VA_ARGS__) #define fec(...) for (cauto &__VA_ARGS__) template inline bool chmin(T &a, U b) { return a > b ? a = b, true : false; } template inline constexpr T divfloor(U a, V b) { return T(a) / T(b) - (T(a) % T(b) && (T(a) ^ T(b)) < 0); } template inline constexpr T safemod(U a, V b) { return T(a) - T(b) * divfloor(a, b); } template constexpr T ipow(U a, V b) { assert(b >= 0); if (b == 0) return 1; if (a == 0 || a == 1) return a; if (a < 0 && a == -1) return b & 1 ? -1 : 1; T res = 1, tmp = a; while (true) { if (b & 1) res *= tmp; b >>= 1; if (b == 0) break; tmp *= tmp; } return res; } template T mul_limited(A a, B b, M m) { assert(a >= 0 && b >= 0 && m >= 0); if (b == 0) return 0; return T(a) > T(m) / T(b) ? T(m) : T(a) * T(b); } template T mul_limited(A a, B b) { return mul_limited(a, b, INF); } template T pow_limited(A a, B b, M m) { assert(a >= 0 && b >= 0 && m >= 0); if (a <= 1 || b == 0) return min(ipow(a, b), T(m)); T res = 1, tmp = a; while (true) { if (b & 1) { if (res > T(m) / tmp) return m; res *= tmp; } b >>= 1; if (b == 0) break; if (tmp > T(m) / tmp) return m; tmp *= tmp; } return res; } template T pow_limited(A a, B b) { return pow_limited(a, b, INF); } template vc base_repr(U val, V base) { assert(val >= 0); assert(base >= 2); if (val == 0) return {0}; vc a; while (val > 0) { a.emplace_back(val % base); val /= base; } reverse(a.begin(), a.end()); return a; } template vc base_repr(U val, V base, int n) { assert(val >= 0); assert(base >= 2); assert(n >= 0); vc a(n); repi(i, n) { a[i] = val % base; val /= base; } reverse(a.begin(), a.end()); return a; } #define ALL(a) (a).begin(), (a).end() template inline T SZ(const V &x) { return x.size(); } #define eb emplace_back template auto gen_vec(const int &n, const F &f) { vc res(n); repi(i, n) res[i] = f(i); return res; } template auto dvec(const V (&sz)[d], const T &init) { if constexpr (i < d) return vc(sz[i], dvec(sz, init)); else return init; } template T ctol(const char &c, const string &s) { repi(i, SZ(s)) if (s[i] == c) return i; return -1; } template vc concat(vc v, const vc &...vs) { (v.insert(v.end(), ALL(vs)), ...); return v; } template V reversed(const V &v) { return V(v.rbegin(), v.rend()); } #if __cplusplus < 202002L #else template V sorted(V v, Args&&... args) { ranges::sort(v, forward(args)...); return v; } #endif template void unique(V &v) { v.erase(std::unique(ALL(v)), v.end()); } template void sortunique(V &v) { sort(ALL(v)); unique(v); } template V sortuniqued(V v) { sortunique(v); return v; } template void rotate(V &v, U k) { const U n = v.size(); k = (k % n + n) % n; std::rotate(v.begin(), v.begin() + k, v.end()); } 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; } template struct MonoidAdd { using S = T; static constexpr S op(S a, S b) { return a + b; } static constexpr S e() { return 0; } }; template struct MonoidMin { using S = T; static constexpr S op(S a, S b) { return min(a, b); } static constexpr S e() { return infty; } }; template struct MonoidMax { using S = T; static constexpr S op(S a, S b) { return max(a, b); } static constexpr S e() { return -infty; } }; template vc cuml(const vc &v, int left_index = 0) { const int n = v.size(); vc res(n + 1); res[0] = M::e(); repi(i, n) res[i + 1] = M::op(res[i], v[i]); res.erase(res.begin(), res.begin() + left_index); return res; } const vpll DRULgrid = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; const vpll 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; #if __cplusplus < 202002L struct identity { template constexpr T &&operator()(T &&t) const noexcept { return forward(t); } }; namespace internal { template inline T bound_helper(const V &v, Judge judge) { int l = -1, r = v.size(); while (r - l > 1) { int m = (l + r) / 2; if (judge(m)) l = m; else r = m; } return r; } }; template , class Proj = identity> inline T LB(const V &v, const Value &val, Comp comp = {}, Proj proj = {}) { return internal::bound_helper(v, [&](int i) -> bool { return comp(proj(*(v.begin() + i)), val); }); } #define DEFAULT_COMP less<> #else template inline T LB(const V &v, const Value &val, Comp comp = {}, Proj proj = {}) { return ranges::lower_bound(v, val, comp, proj) - v.begin(); } template inline T UB(const V &v, const Value &val, Comp comp = {}, Proj proj = {}) { return ranges::upper_bound(v, val, comp, proj) - v.begin(); } #define DEFAULT_COMP ranges::less #endif #if __cplusplus < 202002L inline constexpr ull bit_width(ull x) { return x == 0 ? 0 : 64 - __builtin_clzll(x); } inline constexpr ull bit_ceil(ull x) { return x == 0 ? 1ULL : 1ULL << bit_width(x - 1); } inline constexpr ull popcount(ull x) { return __builtin_popcountll(x); } #else inline constexpr ll bit_width(ll x) { return std::bit_width((ull)x); } inline constexpr ll bit_floor(ll x) { return std::bit_floor((ull)x); } inline constexpr ll bit_ceil(ll x) { return std::bit_ceil((ull)x); } inline constexpr ll countr_zero(ll x) { assert(x != 0); return std::countr_zero((ull)x); } inline constexpr ll popcount(ll x) { return std::popcount((ull)x); } inline constexpr bool has_single_bit(ll x) { return std::has_single_bit((ull)x); } #endif #define dump(...) #define oj(...) __VA_ARGS__ namespace fastio { static constexpr uint32_t SIZ = 1 << 17; char ibuf[SIZ]; char obuf[SIZ]; char out[100]; uint32_t pil = 0, pir = 0, por = 0; struct Pre { char num[10000][4]; constexpr Pre() : num() { for (int i = 0; i < 10000; i++) { int n = i; for (int j = 3; j >= 0; j--) { num[i][j] = n % 10 | '0'; n /= 10; } } } } constexpr pre; inline void load() { memcpy(ibuf, ibuf + pil, pir - pil); pir = pir - pil + fread(ibuf + pir - pil, 1, SIZ - pir + pil, stdin); pil = 0; if (pir < SIZ) ibuf[pir++] = '\n'; } inline void flush() { fwrite(obuf, 1, por, stdout); por = 0; } void rd1(string &x) { x.clear(); char c; do { if (pil + 1 > pir) load(); c = ibuf[pil++]; } while (isspace(c)); do { x += c; if (pil == pir) load(); c = ibuf[pil++]; } while (!isspace(c)); } template void rd1_integer(T &x) { if (pil + 100 > pir) load(); char c; do c = ibuf[pil++]; while (c < '-'); bool minus = 0; if constexpr (is_signed::value || is_same_v) { if (c == '-') { minus = 1, c = ibuf[pil++]; } } x = 0; while ('0' <= c) { x = x * 10 + (c & 15), c = ibuf[pil++]; } if constexpr (is_signed::value || is_same_v) { if (minus) x = -x; } } void rd1(ll &x) { rd1_integer(x); } template void rd1(pair &p) { return rd1(p.first), rd1(p.second); } template void rd1_tuple(T &t) { if constexpr (N < std::tuple_size::value) { auto &x = std::get(t); rd1(x); rd1_tuple(t); } } template void rd1(tuple &tpl) { rd1_tuple(tpl); } template void rd1(array &x) { for (auto &d: x) rd1(d); } template void rd1(vc &x) { for (auto &d: x) rd1(d); } void read() {} template void read(H &h, T &... t) { rd1(h), read(t...); } void wt1(const char c) { if (por == SIZ) flush(); obuf[por++] = c; } void wt1(const string s) { for (char c: s) wt1(c); } template void wt1_integer(T x) { if (por > SIZ - 100) flush(); if (x < 0) { obuf[por++] = '-', x = -x; } int outi; for (outi = 96; x >= 10000; outi -= 4) { memcpy(out + outi, pre.num[x % 10000], 4); x /= 10000; } if (x >= 1000) { memcpy(obuf + por, pre.num[x], 4); por += 4; } else if (x >= 100) { memcpy(obuf + por, pre.num[x] + 1, 3); por += 3; } else if (x >= 10) { int q = (x * 103) >> 10; obuf[por] = q | '0'; obuf[por + 1] = (x - q * 10) | '0'; por += 2; } else obuf[por++] = x | '0'; memcpy(obuf + por, out + outi + 4, 96 - outi); por += 96 - outi; } template , int> = 0> void wt1(T x) { wt1_integer(x); } template void wt1(const pair &val) { wt1(val.first); wt1(' '); wt1(val.second); } template void wt1_tuple(const T &t) { if constexpr (N < std::tuple_size::value) { if constexpr (N > 0) { wt1(' '); } const auto x = std::get(t); wt1(x); wt1_tuple(t); } } template void wt1(const tuple &tpl) { wt1_tuple(tpl); } template void wt1(const array &val) { auto n = val.size(); for (size_t i = 0; i < n; i++) { if (i) wt1(' '); wt1(val[i]); } } template void wt1(const vector &val) { auto n = val.size(); for (size_t i = 0; i < n; i++) { if (i) wt1(' '); wt1(val[i]); } } void print() { wt1('\n'); } template void print(Head &&head, Tail &&... tail) { wt1(head); if (sizeof...(Tail)) wt1(' '); print(forward(tail)...); } } // namespace fastio struct Dummy { Dummy() { atexit(fastio::flush); } } dummy; namespace internal { template void READnodump(Ts &...a) { fastio::read(a...); } template void READVECnodump(int n, vc &v) { v.resize(n); READnodump(v); } template void READVECnodump(int n, vc &v, vc &...vs) { READVECnodump(n, v), READVECnodump(n, vs...); } template void READVEC2nodump(int n, int m, vvc &v) { v.assign(n, vc(m)); READnodump(v); } template void READVEC2nodump(int n, int m, vvc &v, vvc &...vs) { READVEC2nodump(n, m, v), READVEC2nodump(n, m, vs...); } template void READJAGnodump(int n, vvc &v) { v.resize(n); repi(i, n) { int k; READnodump(k); READVECnodump(k, v[i]); } } template void READJAGnodump(int n, vvc &v, vvc &...vs) { READJAGnodump(n, v), READJAGnodump(n, vs...); } }; // namespace internal #define READ(...) internal::READnodump(__VA_ARGS__); dump(__VA_ARGS__) #define IN(T, ...) T __VA_ARGS__; READ(__VA_ARGS__) #define LL(...) IN(ll, __VA_ARGS__) #define READVEC(...) internal::READVECnodump(__VA_ARGS__); dump(__VA_ARGS__) #define VEC(T, n, ...) vc __VA_ARGS__; READVEC(n, __VA_ARGS__) #define PRINT fastio::print template void PRINTV(const vc &v) { for (auto &vi : v) PRINT(vi); } template pair operator+=(pair &a, const P &b) { a.first += b.first; a.second += b.second; return a; } template pair operator+(pair &a, const P &b) { return a += b; } template array operator+=(array &a, const A &b) { for (size_t i = 0; i < n; i++) a[i] += b[i]; return a; } template array operator+(array &a, const A &b) { return a += b; } namespace internal { template auto tuple_add_impl(A &a, const B &b, const index_sequence) { ((get(a) += get(b)), ...); return a; } }; // namespace internal template tuple operator+=(tuple &a, const Tp &b) { return internal::tuple_add_impl(a, b, make_index_sequence>>{}); } template tuple operator+(tuple &a, const Tp &b) { return a += b; } template array, m> top(const vc> &vt) { const size_t n = vt.size(); array, m> tv; tv.fill(vc(n)); for (size_t i = 0; i < n; i++) for (size_t j = 0; j < m; j++) tv[j][i] = vt[i][j]; return tv; } template vc> top(const array, m> &tv) { if (tv.empty()) return {}; const size_t n = tv[0].size(); vc> vt(n); for (size_t j = 0; j < m; j++) { assert(tv[j].size() == n); for (size_t i = 0; i < n; i++) vt[i][j] = tv[j][i]; } return vt; } template pair, vc> top(const vc> &vt) { const size_t n = vt.size(); pair, vc> tv; tv.first.resize(n), tv.second.resize(n); for (size_t i = 0; i < n; i++) tie(tv.first[i], tv.second[i]) = vt[i]; return tv; } template vc> top(const pair, vc> &tv) { const size_t n = tv.first.size(); assert(n == tv.second.size()); vc> vt(n); for (size_t i = 0; i < n; i++) vt[i] = make_pair(tv.first[i], tv.second[i]); return vt; } namespace internal { template auto vt_to_tv_impl(V &tv, const Tp &t, index_sequence, size_t index) { ((get(tv)[index] = get(t)), ...); } template auto tv_to_vt_impl(const Tp &tv, index_sequence, size_t index) { return make_tuple(get(tv)[index]...); } }; template auto top(const vc> &vt) { const size_t n = vt.size(); tuple...> tv; apply([&](auto &...v) { ((v.resize(n)), ...); }, tv); for (size_t i = 0; i < n; i++) internal::vt_to_tv_impl(tv, vt[i], make_index_sequence>{}, i); return tv; } template auto top(const tuple...> &tv) { size_t n = get<0>(tv).size(); apply([&](auto &...v) { ((assert(v.size() == n)), ...); }, tv); vc> vt(n); for (size_t i = 0; i < n; i++) vt[i] = internal::tv_to_vt_impl(tv, index_sequence_for{}, i); return vt; } mt19937_64 mt; namespace internal { constexpr ll powmod32_constexpr(ll x, ll n, int m) { if (m == 1) return 0; uint _m = (uint)m; ull r = 1; ull y = safemod(x, m); while (n) { if (n & 1) r = (r * y) % _m; y = (y * y) % _m; n >>= 1; } return r; } constexpr bool isprime32_constexpr(int n) { if (n <= 1) return false; if (n == 2 || n == 7 || n == 61) return true; if (n % 2 == 0) return false; ll d = n - 1; while (d % 2 == 0) d /= 2; constexpr ll bases[3] = {2, 7, 61}; for (ll a : bases) { ll t = d; ll y = powmod32_constexpr(a, t, n); while (t != n - 1 && y != 1 && y != n - 1) { y = y * y % n; t <<= 1; } if (y != n - 1 && t % 2 == 0) return false; } return true; } template constexpr bool isprime32 = isprime32_constexpr(n); struct barrett32 { uint m; ull im; explicit barrett32(uint m) : m(m), im((ull)(-1) / m + 1) {} uint umod() const { return m; } uint mul(uint a, uint b) const { ull z = a; z *= b; ull x = (ull)((u128(z)*im) >> 64); ull y = x * m; return (uint)(z - y + (z < y ? m : 0)); } }; } namespace internal { #define REF static_cast(*this) #define CREF static_cast(*this) #define VAL *static_cast(this) template struct modint_base { mint &operator+=(const mint &rhs) { mint &self = REF; self._v += rhs._v; if (self._v >= self.umod()) self._v -= self.umod(); return self; } mint &operator-=(const mint &rhs) { mint &self = REF; self._v -= rhs._v; if (self._v >= self.umod()) self._v += self.umod(); return self; } mint &operator/=(const mint &rhs) { mint &self = REF; return self = self * rhs.inv(); } mint &operator++() { mint &self = REF; self._v++; if (self._v == self.umod()) self._v = 0; return self; } mint &operator--() { mint &self = REF; if (self._v == 0) self._v = self.umod(); self._v--; return self; } mint operator++(int) { mint res = VAL; ++REF; return res; } mint operator--(int) { mint res = VAL; --REF; return res; } mint operator+() const { return VAL; } mint operator-() const { return mint() - VAL; } mint pow(ll n) const { assert(n >= 0); mint x = VAL, r = 1; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } friend mint operator+(const mint &lhs, const mint &rhs) { return mint(lhs) += rhs; } friend mint operator-(const mint &lhs, const mint &rhs) { return mint(lhs) -= rhs; } friend mint operator*(const mint &lhs, const mint &rhs) { return mint(lhs) *= rhs; } friend mint operator/(const mint &lhs, const mint &rhs) { return mint(lhs) /= rhs; } friend bool operator==(const mint &lhs, const mint &rhs) { return mint(lhs).eq(rhs); } friend bool operator!=(const mint &lhs, const mint &rhs) { return mint(lhs).neq(rhs); } private: bool eq(const mint &rhs) { return REF._v == rhs._v; } bool neq(const mint &rhs) { return REF._v != rhs._v; } }; } template , T>, int> = 0> void rd1(T &x) { ll a; fastio::rd1(a); x = a; } template , T>, int> = 0> void wt1(const T &x) { fastio::wt1(x.val()); } template constexpr tuple extgcd(const T &a, const T &b) { if (a == 0 && b == 0) return {0, 0, 0}; T x1 = 1, y1 = 0, z1 = a; T x2 = 0, y2 = 1, z2 = b; while (z2 != 0) { T q = z1 / z2; tie(x1, x2) = make_pair(x2, x1 - q * x2); tie(y1, y2) = make_pair(y2, y1 - q * y2); tie(z1, z2) = make_pair(z2, z1 - q * z2); } if (z1 < 0) x1 = -x1, y1 = -y1, z1 = -z1; return {z1, x1, y1}; } template struct static_modint : internal::modint_base> { using mint = static_modint; private: friend struct internal::modint_base>; uint _v; static constexpr uint umod() { return m; } static constexpr bool prime = internal::isprime32; public: static constexpr int mod() { return m; } static mint raw(int v) { mint x; x._v = v; return x; } static_modint() : _v(0) {} template static_modint(T v) { if constexpr (is_signed_v) { ll x = (ll)(v % (ll)(umod())); if (x < 0) x += umod(); _v = (uint)x; } else if constexpr (is_unsigned_v) { _v = (uint)(v % umod()); } else { static_assert(is_signed_v || is_unsigned_v, "Unsupported Type"); } } int val() const { return (int)_v; } mint& operator*=(const mint &rhs) { ull z = _v; z *= rhs._v; _v = (uint)(z % umod()); return *this; } mint inv() const { if (prime) { assert(_v != 0); return CREF.pow(umod() - 2); } else { auto [g, x, y] = extgcd(_v, m); assert(g == 1); return x; } } }; template struct dynamic_modint : internal::modint_base> { using mint = dynamic_modint; private: friend struct internal::modint_base>; uint _v; static internal::barrett32 bt; static uint umod() { return bt.umod(); } public: static int mod() { return (int)(bt.umod()); } static mint raw(int v) { mint x; x._v = v; return x; } dynamic_modint() : _v(0) {} template dynamic_modint(T v) { if constexpr (is_signed_v) { ll x = (ll)(v % (ll)(umod())); if (x < 0) x += umod(); _v = (uint)x; } else if constexpr (is_unsigned_v) { _v = (uint)(v % umod()); } else { static_assert(is_signed_v || is_unsigned_v, "Unsupported Type"); } } int val() const { return (int)_v; } mint& operator*=(const mint &rhs) { _v = bt.mul(_v, rhs._v); return *this; } mint inv() const { auto [g, x, y] = extgcd(_v, mod()); assert(g == 1); return x; } }; template internal::barrett32 dynamic_modint::bt(998244353); using modint998244353 = static_modint<998244353>; template struct is_static_modint : false_type {}; template struct is_static_modint> : true_type {}; template inline constexpr bool is_static_modint_v = is_static_modint::value; template struct is_dynamic_modint : false_type {}; template struct is_dynamic_modint> : true_type {}; template inline constexpr bool is_dynamic_modint_v = is_dynamic_modint::value; template struct PowerTable { private: decltype(T::mod()) mod; T base; vc pw; public: PowerTable() {} PowerTable(T base) : mod(T::mod()), base(base), pw(1, 1) {} void reserve(int n) { if (mod != T::mod()) { mod = T::mod(); pw = {1}; } int i = pw.size(); if (n < i) return; pw.resize(n + 1); for (; i <= n; i++) pw[i] = pw[i - 1] * base; } T pow(int n) { reserve(n); return pw[n]; } }; template struct Binomial { private: static decltype(T::mod()) mod; static vc fac_, finv_, inv_; public: static void reserve(int n) { if (mod != T::mod()) { mod = T::mod(); fac_ = {1, 1}, finv_ = {1, 1}, inv_ = {0, 1}; } int i = fac_.size(); chmin(n, T::mod() - 1); if (n < i) return; fac_.resize(n + 1), finv_.resize(n + 1), inv_.resize(n + 1); for (; i <= n; i++) { fac_[i] = fac_[i - 1] * T::raw(i); inv_[i] = -inv_[T::mod() % i] * T::raw(T::mod() / i); finv_[i] = finv_[i - 1] * inv_[i]; } } static T inv(T n) { assert(n != 0); reserve(n.val()); return inv_[n.val()]; } }; template decltype(T::mod()) Binomial::mod{}; template vc Binomial::fac_{}; template vc Binomial::finv_{}; template vc Binomial::inv_{}; using mint = modint998244353; template struct CoordinateCompression { vc vals; CoordinateCompression() {} CoordinateCompression(const vc &vec) : vals(sortuniqued(vec)) {} template I get_id(const T &val) const { auto it = lower_bound(ALL(vals), val); if (it == vals.end() || *it != val) return -1; return it - vals.begin(); } template I size() const { return vals.size(); } }; template struct FlatMap { CoordinateCompression cc; vc vals; FlatMap() {} FlatMap(const vc &keys) : cc(keys), vals(cc.size()) {} Value &operator[](const Key &key) { const int i = cc.get_id(key); assert(i != -1); return vals[i]; } Value &at(const Key &key) { return operator[](key); } template inline I size() const { return cc.size(); } inline bool empty() const { return size() == 0; } struct Iterator { private: int i; const FlatMap ∓ public: Iterator(int i, const FlatMap &mp) : i(i), mp(mp) {} pair operator*() const { assert(i != mp.cc.size()); return pair{mp.cc.vals[i], mp.vals[i]}; } Iterator &operator++() { i++; return *this; } bool operator!=(const Iterator &other) const { return i != other.i; } }; Iterator begin() const { return Iterator(0, *this); } Iterator end() const { return Iterator(cc.size(), *this); } }; template struct UnionFind { friend UFData; protected: vc par; vc vdat; public: typename UFData::GData gdat; UnionFind() {} UnionFind(int n) : par(n, -1), vdat(n), gdat(n) { repi(i, n) vdat[i] = typename UFData::VData(i); } virtual int leader(int x) { assert(0 <= x && x < SZ(par)); if (par[x] < 0) return x; if constexpr (compress) return par[x] = leader(par[x]); else return leader(par[x]); } template I size(int x) { return -par[leader(x)]; } }; template struct UnionFindUndo : UnionFind { using UF = UnionFind; friend UFData; protected: using UF::par; using UF::vdat; vc> his; public: UnionFindUndo() {} UnionFindUndo(int n) : UF(n) {} using UF::gdat; using UF::leader; }; template void offline_dynamic_connectivity(int tmax, const vc> &span_obj, Add add, Undo undo, Run run) { const int tmax_ceil = bit_ceil(tmax); vvc nodes(tmax_ceil + tmax); fe([l, r, x] : span_obj) { assert(0 <= l && l <= r && r <= tmax); l += tmax_ceil, r += tmax_ceil; while (l < r) { if (l & 1) nodes[l++].eb(x); if (r & 1) nodes[--r].eb(x); l >>= 1, r >>= 1; } } auto dfs = [&](auto dfs, int i) -> void { fec(x : nodes[i]) add(x); repi(ni, 2 * i, 2 * i + 2) { if (ni < tmax_ceil + tmax) dfs(dfs, ni); } if (0 <= i - tmax_ceil && i - tmax_ceil < tmax) run(i - tmax_ceil); repi(_, nodes[i].size()) undo(); }; dfs(dfs, 1); } void init() { oj(mt.seed(random_device()())); } void main2() { LL(N, M, K); VEC(ll, N, A); vc L(N), R(N); rep(i, N) L.at(i) = max(0LL, i - M + 1), R.at(i) = min(N - M + 1, i + 1); vc cur(K); cur.at(0) = 1; vc> sta; auto add = [&](ll a) -> void { sta.eb(cur); vc nxt(cur); rep(i, K) nxt.at((i + a) % K) += cur.at(i); swap(cur, nxt); dump("add", a, cur); }; auto undo = [&]() -> void { swap(cur, sta.back()); sta.pop_back(); dump("undo", cur); }; vc ans(N - M + 1); auto run = [&](ll i) -> void { ans.at(i) = cur.at(0) - 1; dump("run", i, ans.at(i)); }; offline_dynamic_connectivity(N - M + 1, top(tuple{L, R, A}), add, undo, run); PRINTV(ans); } void test() { } template struct Main { Main() { cauto CERR = [](string val, string color) { string s = "\033[" + color + "m" + val + "\033[m"; /* コードテストで確認する際にコメントアウトを外す cerr << val; //*/ }; CERR("\n[FAST_IO]\n\n", "32"); cout << fixed << setprecision(20); test(); init(); CERR("\n[SINGLE_TESTCASE]\n\n", "36"); main2(); } }; Main main_dummy; } int main() {}