結果
問題 | No.2215 Slide Subset Sum |
ユーザー |
![]() |
提出日時 | 2025-04-26 21:41:50 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2,316 ms / 3,000 ms |
コード長 | 40,140 bytes |
コンパイル時間 | 6,069 ms |
コンパイル使用メモリ | 337,960 KB |
実行使用メモリ | 354,056 KB |
最終ジャッジ日時 | 2025-04-26 21:42:33 |
合計ジャッジ時間 | 41,550 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 45 |
ソースコード
#define INF 4'000'000'000'000'000'037LL #include <bits/stdc++.h> using namespace std; namespace { using ll = long long; using uint = unsigned int; using ull = unsigned long long; using pll = pair<ll, ll>; #define vc vector template <class T> using vvc = vc<vc<T>>; using vpll = vc<pll>; #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 fec(...) for (cauto &__VA_ARGS__) #define fem(...) for (auto &__VA_ARGS__) template <class T, class U> inline bool chmin(T &a, U b) { return a > b ? a = b, true : false; } template <class T, class U> inline bool chmax(T &a, U b) { return a < b ? a = b, true : false; } template <class T = ll, class U, class V> inline constexpr T divfloor(U a, V b) { return T(a) / T(b) - (T(a) % T(b) && (T(a) ^ T(b)) < 0); } template <class T = ll, class U, class V> inline constexpr T safemod(U a, V b) { return T(a) - T(b) * divfloor<T>(a, b); } template <class T = ll, class U, class V> 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 <class T = ll, class A, class B, class M> 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 <class T = ll, class A, class B> T mul_limited(A a, B b) { return mul_limited<T>(a, b, INF); } template <class T = ll, class A, class B, class M> T pow_limited(A a, B b, M m) { assert(a >= 0 && b >= 0 && m >= 0); if (a <= 1 || b == 0) return min(ipow<T>(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 <class T = ll, class A, class B> T pow_limited(A a, B b) { return pow_limited<T>(a, b, INF); } #define ALL(a) (a).begin(), (a).end() template <class T = ll, class V> inline T SZ(const V &x) { return x.size(); } template <class T, size_t d, size_t i = 0, class V> auto dvec(const V (&sz)[d], const T &init) { if constexpr (i < d) return vc(sz[i], dvec<T, d, i + 1>(sz, init)); else return init; } template <class V> auto MAX(const V &v) { return *max_element(ALL(v)); } #if __cplusplus < 202002L #else #endif template <class V> void unique(V &v) { v.erase(std::unique(ALL(v)), v.end()); } template <class V, class U> 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 <class T> struct MonoidAdd { using S = T; static constexpr S op(S a, S b) { return a + b; } static constexpr S e() { return 0; } }; template <class M> vc<typename M::S> cuml(const vc<typename M::S> &v, int left_index = 0) { const int n = v.size(); vc<typename M::S> 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; } template <class T> vc<T> cumlsum(const vc<T> &v, int left_index = 0) { return cuml<MonoidAdd<T>>(v, left_index); } const vpll DRULgrid = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; const vpll DRULplane = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}; template <class T> struct is_random_access_iterator { static constexpr bool value = is_same_v< typename iterator_traits<T>::iterator_category, random_access_iterator_tag >; }; template <class T> constexpr bool is_random_access_iterator_v = is_random_access_iterator<T>::value; #if __cplusplus < 202002L struct identity { template <class T> constexpr T &&operator()(T &&t) const noexcept { return forward<T>(t); } }; namespace internal { template <class T = ll, class V, class Judge> 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 T = ll, class V, class Value, class Comp = less<>, 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); }); } template <class T = ll, class V, class Value, class Comp = less<>, class Proj = identity> inline T UB(const V &v, const Value &val, Comp comp = {}, Proj proj = {}) { return internal::bound_helper(v, [&](int i) -> bool { return !comp(val, proj(*(v.begin() + i))); }); } #define DEFAULT_COMP less<> #else template <class T = ll, class V, class Value, class Comp = ranges::less, class Proj = identity> inline T LB(const V &v, const Value &val, Comp comp = {}, Proj proj = {}) { return ranges::lower_bound(v, val, comp, proj) - v.begin(); } template <class T = ll, class V, class Value, class Comp = ranges::less, class Proj = identity> 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 template <class T = ll, class V, class Value, class Comp = DEFAULT_COMP, class Proj = identity> inline auto lt_max(const V &v, const Value &val, Comp comp = {}, Proj proj = {}) -> enable_if_t<is_random_access_iterator_v<typename V::iterator>, T> { return LB<T>(v, val, comp, proj) - 1; } template <class T = ll, class V, class Value, class Comp = DEFAULT_COMP, class Proj = identity> inline auto leq_max(const V &v, const Value &val, Comp comp = {}, Proj proj = {}) -> enable_if_t<is_random_access_iterator_v<typename V::iterator>, T> { return UB<T>(v, val, comp, proj) - 1; } template <class T = ll, class V, class Value, class Comp = DEFAULT_COMP, class Proj = identity> inline auto gt_min(const V &v, const Value &val, Comp comp = {}, Proj proj = {}) -> enable_if_t<is_random_access_iterator_v<typename V::iterator>, T> { return UB<T>(v, val, comp, proj); } template <class T = ll, class V, class Value, class Comp = DEFAULT_COMP, class Proj = identity> inline auto geq_min(const V &v, const Value &val, Comp comp = {}, Proj proj = {}) -> enable_if_t<is_random_access_iterator_v<typename V::iterator>, T> { return LB<T>(v, val, comp, proj); } template <class T = ll, class V, class Value, class Comp = DEFAULT_COMP, class Proj = identity> inline auto lt_cnt(const V &v, const Value &val, Comp comp = {}, Proj proj = {}) -> enable_if_t<is_random_access_iterator_v<typename V::iterator>, T> { return LB<T>(v, val, comp, proj); } template <class T = ll, class V, class Value, class Comp = DEFAULT_COMP, class Proj = identity> inline auto leq_cnt(const V &v, const Value &val, Comp comp = {}, Proj proj = {}) -> enable_if_t<is_random_access_iterator_v<typename V::iterator>, T> { return UB<T>(v, val, comp, proj); } template <class T = ll, class V, class Value, class Comp = DEFAULT_COMP, class Proj = identity> inline auto gt_cnt(const V &v, const Value &val, Comp comp = {}, Proj proj = {}) -> enable_if_t<is_random_access_iterator_v<typename V::iterator>, T> { return SZ<T>(v) - UB<T>(v, val, comp, proj); } template <class T = ll, class V, class Value, class Comp = DEFAULT_COMP, class Proj = identity> inline auto geq_cnt(const V &v, const Value &val, Comp comp = {}, Proj proj = {}) -> enable_if_t<is_random_access_iterator_v<typename V::iterator>, T> { return SZ<T>(v) - LB<T>(v, val, comp, proj); } template <class T = ll, class V, class L, class R, class Comp = DEFAULT_COMP, class Proj = identity> inline auto in_cnt(const V &v, L l, R r, Comp comp = {}, Proj proj = {}) -> enable_if_t<is_random_access_iterator_v<typename V::iterator>, T> { if (l > r) return 0; return lt_cnt<T>(v, r, comp, proj) - lt_cnt<T>(v, l, comp, proj); } template <class V, class Value> inline auto lt_max(const V &v, const Value &val) -> enable_if_t<!is_random_access_iterator_v<typename V::iterator>, typename V::const_iterator> { auto it = v.lower_bound(val); return it == v.begin() ? v.end() : prev(it); } template <class V, class Value> inline auto leq_max(const V &v, const Value &val) -> enable_if_t<!is_random_access_iterator_v<typename V::iterator>, typename V::const_iterator> { auto it = v.upper_bound(val); return it == v.begin() ? v.end() : prev(it); } template <class V, class Value> inline auto gt_min(const V &v, const Value &val) -> enable_if_t<!is_random_access_iterator_v<typename V::iterator>, typename V::const_iterator> { return v.upper_bound(val); } template <class V, class Value> inline auto geq_min(const V &v, const Value &val) -> enable_if_t<!is_random_access_iterator_v<typename V::iterator>, typename V::const_iterator> { return v.lower_bound(val); } #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 countr_zero(ull x) { assert(x != 0); return __builtin_ctzll(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 inline constexpr ull msb_pos(ull x) { assert(x != 0); return bit_width(x) - 1; } #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; } template <typename T> 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<T>::value || is_same_v<T, i128>) { if (c == '-') { minus = 1, c = ibuf[pil++]; } } x = 0; while ('0' <= c) { x = x * 10 + (c & 15), c = ibuf[pil++]; } if constexpr (is_signed<T>::value || is_same_v<T, i128>) { if (minus) x = -x; } } void rd1(ll &x) { rd1_integer(x); } template <class T, class U> void rd1(pair<T, U> &p) { return rd1(p.first), rd1(p.second); } template <size_t N = 0, typename T> void rd1_tuple(T &t) { if constexpr (N < std::tuple_size<T>::value) { auto &x = std::get<N>(t); rd1(x); rd1_tuple<N + 1>(t); } } template <class... T> void rd1(tuple<T...> &tpl) { rd1_tuple(tpl); } template <size_t N = 0, typename T> void rd1(array<T, N> &x) { for (auto &d: x) rd1(d); } template <class T> void rd1(vc<T> &x) { for (auto &d: x) rd1(d); } void read() {} template <class H, class... T> void read(H &h, T &... t) { rd1(h), read(t...); } void wt1(const char c) { if (por == SIZ) flush(); obuf[por++] = c; } template <typename T> 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 <class T, enable_if_t<is_integral_v<T>, int> = 0> void wt1(T x) { wt1_integer(x); } template <class T, class U> void wt1(const pair<T, U> &val) { wt1(val.first); wt1(' '); wt1(val.second); } template <size_t N = 0, typename T> void wt1_tuple(const T &t) { if constexpr (N < std::tuple_size<T>::value) { if constexpr (N > 0) { wt1(' '); } const auto x = std::get<N>(t); wt1(x); wt1_tuple<N + 1>(t); } } template <class... T> void wt1(const tuple<T...> &tpl) { wt1_tuple(tpl); } template <class T, size_t S> void wt1(const array<T, S> &val) { auto n = val.size(); for (size_t i = 0; i < n; i++) { if (i) wt1(' '); wt1(val[i]); } } template <class T> void wt1(const vector<T> &val) { auto n = val.size(); for (size_t i = 0; i < n; i++) { if (i) wt1(' '); wt1(val[i]); } } void print() { wt1('\n'); } template <class Head, class... Tail> void print(Head &&head, Tail &&... tail) { wt1(head); if (sizeof...(Tail)) wt1(' '); print(forward<Tail>(tail)...); } } // namespace fastio struct Dummy { Dummy() { atexit(fastio::flush); } } dummy; namespace internal { template <class... Ts> void READnodump(Ts &...a) { fastio::read(a...); } template <class T> void READVECnodump(int n, vc<T> &v) { v.resize(n); READnodump(v); } template <class T, class... Ts> void READVECnodump(int n, vc<T> &v, vc<Ts> &...vs) { READVECnodump(n, v), READVECnodump(n, vs...); } template <class T> void READVEC2nodump(int n, int m, vvc<T> &v) { v.assign(n, vc<T>(m)); READnodump(v); } template <class T, class... Ts> void READVEC2nodump(int n, int m, vvc<T> &v, vvc<Ts> &...vs) { READVEC2nodump(n, m, v), READVEC2nodump(n, m, vs...); } template <class T> void READJAGnodump(int n, vvc<T> &v) { v.resize(n); repi(i, n) { int k; READnodump(k); READVECnodump(k, v[i]); } } template <class T, class... Ts> void READJAGnodump(int n, vvc<T> &v, vvc<Ts> &...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<T> __VA_ARGS__; READVEC(n, __VA_ARGS__) #define PRINT fastio::print template <class T, class U, class P> pair<T, U> operator+=(pair<T, U> &a, const P &b) { a.first += b.first; a.second += b.second; return a; } template <class T, class U, class P> pair<T, U> operator+(pair<T, U> &a, const P &b) { return a += b; } template <class T, size_t n, class A> array<T, n> operator+=(array<T, n> &a, const A &b) { for (size_t i = 0; i < n; i++) a[i] += b[i]; return a; } template <class T, size_t n, class A> array<T, n> operator+(array<T, n> &a, const A &b) { return a += b; } namespace internal { template <size_t... I, class A, class B> auto tuple_add_impl(A &a, const B &b, const index_sequence<I...>) { ((get<I>(a) += get<I>(b)), ...); return a; } }; // namespace internal template <class... Ts, class Tp> tuple<Ts...> operator+=(tuple<Ts...> &a, const Tp &b) { return internal::tuple_add_impl(a, b, make_index_sequence<tuple_size_v<tuple<Ts...>>>{}); } template <class... Ts, class Tp> tuple<Ts...> operator+(tuple<Ts...> &a, const Tp &b) { return a += b; } namespace internal { }; 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 <int n> 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<mint &>(*this) #define CREF static_cast<const mint &>(*this) #define VAL *static_cast<const mint *>(this) template <class mint> 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 <typename T, std::enable_if_t<std::is_base_of_v<internal::modint_base<T>, T>, int> = 0> void rd1(T &x) { ll a; fastio::rd1(a); x = a; } template <typename T, std::enable_if_t<std::is_base_of_v<internal::modint_base<T>, T>, int> = 0> void wt1(const T &x) { fastio::wt1(x.val()); } template <class T = ll> constexpr tuple<T, T, T> 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 <int m> struct static_modint : internal::modint_base<static_modint<m>> { using mint = static_modint; private: friend struct internal::modint_base<static_modint<m>>; uint _v; static constexpr uint umod() { return m; } static constexpr bool prime = internal::isprime32<m>; public: static constexpr int mod() { return m; } static mint raw(int v) { mint x; x._v = v; return x; } static_modint() : _v(0) {} template <class T> static_modint(T v) { if constexpr (is_signed_v<T>) { ll x = (ll)(v % (ll)(umod())); if (x < 0) x += umod(); _v = (uint)x; } else if constexpr (is_unsigned_v<T>) { _v = (uint)(v % umod()); } else { static_assert(is_signed_v<T> || is_unsigned_v<T>, "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<int>(_v, m); assert(g == 1); return x; } } }; template <int id> struct dynamic_modint : internal::modint_base<dynamic_modint<id>> { using mint = dynamic_modint; private: friend struct internal::modint_base<dynamic_modint<id>>; 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 <class T> dynamic_modint(T v) { if constexpr (is_signed_v<T>) { ll x = (ll)(v % (ll)(umod())); if (x < 0) x += umod(); _v = (uint)x; } else if constexpr (is_unsigned_v<T>) { _v = (uint)(v % umod()); } else { static_assert(is_signed_v<T> || is_unsigned_v<T>, "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<int>(_v, mod()); assert(g == 1); return x; } }; template <int id> internal::barrett32 dynamic_modint<id>::bt(998244353); using modint998244353 = static_modint<998244353>; template <class T> struct is_static_modint : false_type {}; template <int m> struct is_static_modint<static_modint<m>> : true_type {}; template <class T> inline constexpr bool is_static_modint_v = is_static_modint<T>::value; template <class T> struct is_dynamic_modint : false_type {}; template <int id> struct is_dynamic_modint<dynamic_modint<id>> : true_type {}; template <class T> inline constexpr bool is_dynamic_modint_v = is_dynamic_modint<T>::value; template <class T> struct PowerTable { private: decltype(T::mod()) mod; T base; vc<T> 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 <class T> struct Binomial { private: static decltype(T::mod()) mod; static vc<T> 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 <class T> decltype(T::mod()) Binomial<T>::mod{}; template <class T> vc<T> Binomial<T>::fac_{}; template <class T> vc<T> Binomial<T>::finv_{}; template <class T> vc<T> Binomial<T>::inv_{}; using mint = modint998244353; template <class mint, class T = ll, class U1, class U2, size_t n> constexpr pair<mint, mint> crt_mod_constexpr(const array<U1, n> &rs, const array<U2, n> &ms) { assert(rs.size() == ms.size()); mint r = 0, m = 1; array<T, n> rr{}, mm; fill(ALL(mm), 1); repi(i, n) { assert(ms[i] >= U2(1)); assert(U1(0) <= rs[i] && U2(rs[i]) < ms[i]); auto [g, im, _] = extgcd<T>(mm[i], ms[i]); assert(g == 1); T t = safemod((rs[i] - rr[i]) * im, ms[i]); r += t * m, m *= ms[i]; repi(j, i + 1, n) { rr[j] += t * mm[j] % ms[j]; if (rr[j] >= ms[j]) rr[j] -= ms[j]; mm[j] *= ms[i], mm[j] %= ms[j]; } } return {r, m}; } namespace internal { constexpr int primitive_root_constexpr(int m) { if (m == 2) return 1; if (m == 167772161) return 3; if (m == 469762049) return 3; if (m == 754974721) return 11; if (m == 998244353) return 3; if (m == 1107296257) return 10; if (m == 1711276033) return 29; if (m == 1811939329) return 13; if (m == 2013265921) return 31; if (m == 2113929217) return 5; int divs[20] = {}; divs[0] = 2; int cnt = 1; int x = (m - 1) / 2; while (x % 2 == 0) x /= 2; for (int i = 3; (long long)(i)*i <= x; i += 2) { if (x % i == 0) { divs[cnt++] = i; while (x % i == 0) { x /= i; } } } if (x > 1) { divs[cnt++] = x; } for (int g = 2;; g++) { bool ok = true; for (int i = 0; i < cnt; i++) { if (powmod32_constexpr(g, (m - 1) / divs[i], m) == 1) { ok = false; break; } } if (ok) return g; } } template <int m> constexpr int primitive_root_for_convolution = primitive_root_constexpr(m); template <class mint, int g = internal::primitive_root_for_convolution<mint::mod()>> struct fft_info { static constexpr int rank2 = countr_zero(mint::mod() - 1); std::array<mint, rank2 + 1> root; // root[i]^(2^i) == 1 std::array<mint, rank2 + 1> iroot; // root[i] * iroot[i] == 1 std::array<mint, std::max(0, rank2 - 2 + 1)> rate2; std::array<mint, std::max(0, rank2 - 2 + 1)> irate2; std::array<mint, std::max(0, rank2 - 3 + 1)> rate3; std::array<mint, std::max(0, rank2 - 3 + 1)> irate3; fft_info() { root[rank2] = mint(g).pow((mint::mod() - 1) >> rank2); iroot[rank2] = root[rank2].inv(); for (int i = rank2 - 1; i >= 0; i--) { root[i] = root[i + 1] * root[i + 1]; iroot[i] = iroot[i + 1] * iroot[i + 1]; } { mint prod = 1, iprod = 1; for (int i = 0; i <= rank2 - 2; i++) { rate2[i] = root[i + 2] * prod; irate2[i] = iroot[i + 2] * iprod; prod *= iroot[i + 2]; iprod *= root[i + 2]; } } { mint prod = 1, iprod = 1; for (int i = 0; i <= rank2 - 3; i++) { rate3[i] = root[i + 3] * prod; irate3[i] = iroot[i + 3] * iprod; prod *= iroot[i + 3]; iprod *= root[i + 3]; } } } }; } // namespace internal template <class mint> bool ntt_ok(int n) { if constexpr (is_static_modint_v<mint>) { if constexpr (!internal::isprime32<mint::mod()>) return false; static constexpr int rank2 = countr_zero(mint::mod() - 1); return n <= (1 << rank2); } else return false; } template <int id> void ntt(vc<dynamic_modint<id>> &) {} template <int id> void intt(vc<dynamic_modint<id>> &) {} template <int mod> void ntt(vc<static_modint<mod>> &a) { using mint = static_modint<mod>; int n = int(a.size()); int h = countr_zero((unsigned int)n); static const internal::fft_info<mint> info; int len = 0; // a[i, i+(n>>len), i+2*(n>>len), ..] is transformed while (len < h) { if (h - len == 1) { int p = 1 << (h - len - 1); mint rot = 1; for (int s = 0; s < (1 << len); s++) { int offset = s << (h - len); for (int i = 0; i < p; i++) { auto l = a[i + offset]; auto r = a[i + offset + p] * rot; a[i + offset] = l + r; a[i + offset + p] = l - r; } if (s + 1 != (1 << len)) rot *= info.rate2[countr_zero(~(unsigned int)(s))]; } len++; } else { int p = 1 << (h - len - 2); mint rot = 1, imag = info.root[2]; for (int s = 0; s < (1 << len); s++) { mint rot2 = rot * rot; mint rot3 = rot2 * rot; int offset = s << (h - len); for (int i = 0; i < p; i++) { auto mod2 = 1ULL * mint::mod() * mint::mod(); auto a0 = 1ULL * a[i + offset].val(); auto a1 = 1ULL * a[i + offset + p].val() * rot.val(); auto a2 = 1ULL * a[i + offset + 2 * p].val() * rot2.val(); auto a3 = 1ULL * a[i + offset + 3 * p].val() * rot3.val(); auto a1na3imag = 1ULL * mint(a1 + mod2 - a3).val() * imag.val(); auto na2 = mod2 - a2; a[i + offset] = a0 + a2 + a1 + a3; a[i + offset + 1 * p] = a0 + a2 + (2 * mod2 - (a1 + a3)); a[i + offset + 2 * p] = a0 + na2 + a1na3imag; a[i + offset + 3 * p] = a0 + na2 + (mod2 - a1na3imag); } if (s + 1 != (1 << len)) rot *= info.rate3[countr_zero(~(unsigned int)(s))]; } len += 2; } } } template <int mod> void intt(vc<static_modint<mod>> &a) { using mint = static_modint<mod>; int n = int(a.size()); int h = countr_zero((unsigned int)n); static const internal::fft_info<mint> info; int len = h; // a[i, i+(n>>len), i+2*(n>>len), ..] is transformed while (len) { if (len == 1) { int p = 1 << (h - len); mint irot = 1; for (int s = 0; s < (1 << (len - 1)); s++) { int offset = s << (h - len + 1); for (int i = 0; i < p; i++) { auto l = a[i + offset]; auto r = a[i + offset + p]; a[i + offset] = l + r; a[i + offset + p] = (unsigned long long)(mint::mod() + l.val() - (uint)r.val()) * irot.val(); ; } if (s + 1 != (1 << (len - 1))) irot *= info.irate2[countr_zero(~(unsigned int)(s))]; } len--; } else { int p = 1 << (h - len); mint irot = 1, iimag = info.iroot[2]; for (int s = 0; s < (1 << (len - 2)); s++) { mint irot2 = irot * irot; mint irot3 = irot2 * irot; int offset = s << (h - len + 2); for (int i = 0; i < p; i++) { auto a0 = 1ULL * a[i + offset + 0 * p].val(); auto a1 = 1ULL * a[i + offset + 1 * p].val(); auto a2 = 1ULL * a[i + offset + 2 * p].val(); auto a3 = 1ULL * a[i + offset + 3 * p].val(); auto a2na3iimag = 1ULL * mint((mint::mod() + a2 - a3) * iimag.val()).val(); a[i + offset] = a0 + a1 + a2 + a3; a[i + offset + 1 * p] = (a0 + (mint::mod() - a1) + a2na3iimag) * irot.val(); a[i + offset + 2 * p] = (a0 + a1 + (mint::mod() - a2) + (mint::mod() - a3)) * irot2.val(); a[i + offset + 3 * p] = (a0 + (mint::mod() - a1) + (mint::mod() - a2na3iimag)) * irot3.val(); } if (s + 1 != (1 << (len - 2))) irot *= info.irate3[countr_zero(~(unsigned int)(s))]; } len -= 2; } } mint in = mint(n).inv(); fem(ai : a) ai *= in; } namespace internal { template <class mint> vc<mint> convolution_naive(const vc<mint> &a, const vc<mint> &b) { const int n = a.size(), m = b.size(); const int cnta = n - count(ALL(a), 0), cntb = m - count(ALL(b), 0); vc<mint> c(n + m - 1); if ((ll)m * cnta > (ll)n * cntb) { repi(j, m) { if (b[j] == 0) continue; repi(i, n) c[i + j] += a[i] * b[j]; } } else { repi(i, n) { if (a[i] == 0) continue; repi(j, m) c[i + j] += a[i] * b[j]; } } return c; } template <class mint> vc<mint> convolution_ntt(vc<mint> a, vc<mint> b) { const int n = a.size(), m = b.size(); const int z = bit_ceil(n + m - 1); a.resize(z), b.resize(z); ntt(a), ntt(b); repi(i, z) a[i] *= b[i]; intt(a); a.resize(n + m - 1); return a; } template <size_t j, int mod, class T, size_t k> void convolution_crt_helper(const vc<T> &a, const vc<T> &b, vc<array<T, k>> &cs) { using mint = static_modint<mod>; const int n = a.size(), m = b.size(); auto c = convolution_ntt(vc<mint>(ALL(a)), vc<mint>(ALL(b))); repi(i, n + m - 1) cs[i][j] = c[i].val(); } template <class mint, int... ms, class T> vc<mint> convolution_crt_mod(const vc<T> &a, const vc<T> &b) { const int n = a.size(), m = b.size(); constexpr size_t k = sizeof...(ms); vc<array<T, k>> cs(n + m - 1); constexpr array<int, k> ms_arr = {ms...}; [&]<size_t... Is>(index_sequence<Is...>) { (convolution_crt_helper<Is, ms_arr[Is], T, k>(a, b, cs), ...); }(make_index_sequence<k>{}); vc<mint> c(n + m - 1); repi(i, n + m - 1) c[i] = crt_mod_constexpr<mint>(cs[i], ms_arr).first; return c; } } // namespace internal template <class mint> vc<mint> convolution(const vc<mint> &a, const vc<mint> &b) { const int n = a.size(), m = b.size(); const int cnta = n - count(ALL(a), 0), cntb = m - count(ALL(b), 0); if (n == 0 || m == 0) return {}; if (ntt_ok<mint>(n + m - 1)) { if (min(cnta, cntb) <= 60) return internal::convolution_naive(a, b); return internal::convolution_ntt(a, b); } else { if (min(cnta, cntb) <= 300) return internal::convolution_naive(a, b); assert(ntt_ok<static_modint<469762049>>(n + m - 1) && "|a| + |b| - 1 <= 2^26"); vc<ll> a_(n), b_(m); repi(i, n) a_[i] = a[i].val(); repi(j, m) b_[j] = b[j].val(); return internal::convolution_crt_mod<mint, 469762049, 1811939329, 2013265921>(a_, b_); } } template <int mod = 998244353, class T, typename = enable_if_t<is_integral<T>::value>> vc<T> convolution(const vc<T> &a, const vc<T> &b) { using mint = static_modint<mod>; auto c = convolution(vc<mint>(ALL(a)), vc<mint>(ALL(b))); vc<T> c_(c.size()); repi(i, c.size()) c_[i] = c[i].val(); return c_; } ll K; struct Mn { using S = vc<mint>; static constexpr S op(const S &f, const S &g) { auto h = convolution(f, g); vc<mint> res(K); rep(i, SZ(h)) res.at(i % K) += h.at(i); return res; } static constexpr S e() { vc<mint> res(K); res.at(0) = 1; return res; } }; template <class T> struct CSR { private: int n; vc<int> start; vc<T> elist; struct Row { using iterator = typename vc<T>::const_iterator; private: iterator begi, endi; public: Row(const iterator &begi, const iterator &endi) : begi(begi), endi(endi) {} inline iterator begin() const { return begi; } inline iterator end() const { return endi; } template <class I = ll> inline I size() const { return endi - begi; } inline bool empty() const { return size() == 0; } inline T get(int i) const { assert(0 <= i && i < size()); return *(begi + i); } inline T back() const { assert(!empty()); return *prev(endi); } }; public: CSR() {} template <class I> CSR(int n, const vc<pair<I, T>> &ies) : n(n), elist(ies.size()) { assert(n >= 0); start.assign(n, 0); fec([ i, e ] : ies) { assert(0 <= i && i < n); start[i]++; } start = cumlsum(start); auto cnt = start; fec([ i, e ] : ies) elist[cnt[i]++] = e; } CSR(const vvc<T> &vv) : n(vv.size()), start(n + 1) { int m = 0; fec(row : vv) m += row.size(); elist.resize(m); int k = 0; repi(i, n) { start[i] = k; fec(e : vv[i]) elist[k++] = e; } start.back() = m; } Row row(int i) const { if (!(0 <= i && i < n)) return Row(elist.begin(), elist.begin()); return Row(elist.begin() + start[i], elist.begin() + start[i + 1]); } template <class I = ll> I size() const { return n; } vvc<T> to_vv() const { vvc<T> res(n); repi(i, n) res[i] = {elist.begin() + start[i], elist.begin() + start[i + 1]}; return res; } }; struct GroupIndex { private: int n, m; CSR<int> csr; public: GroupIndex() {} template <class T> GroupIndex(const vc<T> &a) : n(a.size()), m(a.empty() ? 0 : MAX(a) + 1) { vc<pair<int, int>> ies(n); repi(i, n) { assert(0 <= a[i]); ies[i] = {a[i], i}; } csr = CSR(m, ies); } auto idxs(int val) const { return csr.row(val); } template <class I = ll> I lt_max(int val, int i) const { auto is = idxs(val); ll j = ::lt_max(is, i); return j == -1 ? -1 : is.get(j); } template <class I = ll> I leq_max(int val, int i) const { auto is = idxs(val); ll j = ::leq_max(is, i); return j == -1 ? -1 : is.get(j); } template <class I = ll> I gt_min(int val, int i) const { auto is = idxs(val); ll j = ::gt_min(is, i); return j == is.size() ? n : is.get(j); } template <class I = ll> I geq_min(int val, int i) const { auto is = idxs(val); ll j = ::geq_min(is, i); return j == is.size() ? n : is.get(j); } template <class I = ll> I lt_cnt(int val, int i) const { return ::lt_cnt(idxs(val), i); } template <class I = ll> I leq_cnt(int val, int i) const { return ::leq_cnt(idxs(val), i); } template <class I = ll> I gt_cnt(int val, int i) const { return ::gt_cnt(idxs(val), i); } template <class I = ll> I geq_cnt(int val, int i) const { return ::geq_cnt(idxs(val), i); } template <class I = ll> I in_cnt(int val, int l, int r) const { return ::in_cnt(idxs(val), l, r); } template <class I = ll> vvc<I> to_vv() const { auto res = csr.to_vv(); vvc<I> res2(res.size()); rep(i, res.size()) res2[i] = vc<I>(ALL(res[i])); return res2; } }; template <class M, class I> vc<pair<typename M::S, typename M::S>> dc_range_prod_left_right (const vc<typename M::S> &v, const vc<pair<I, I>> &lrs) { using S = typename M::S; const int n = v.size(), q = lrs.size(); vc<pair<S, S>> res(q); vc<int> mids(q, n + 1); rep(qi, q) { auto [l, r] = lrs[qi]; if (l == r) { res[qi] = pair{M::e(), M::e()}; continue; } if (l + 1 == r) { res[qi] = pair{v[l], M::e()}; continue; } const int j = msb_pos(l ^ (r - 1)); mids[qi] = (l | (1 << j)) & ~((1 << j) - 1); } vc<S> dat(n); GroupIndex grp(mids); repi(mid, 1, n) { auto qis = grp.idxs(mid); int l = n + 1, r = -1; fec(qi : qis) { auto [l_, r_] = lrs[qi]; chmin(l, l_), chmax(r, r_); } dat[mid - 1] = v[mid - 1]; repi(i, mid - 2, l - 1, -1) dat[i] = M::op(v[i], dat[i + 1]); dat[mid] = v[mid]; repi(i, mid + 1, r) dat[i] = M::op(dat[i - 1], v[i]); fec(qi : qis) { auto [l_, r_] = lrs[qi]; res[qi] = {dat[l_], dat[r_ - 1]}; } } return res; } void init() { oj(mt.seed(random_device()())); } void main2() { LL(N, M, K_); K = K_; VEC(ll, N, A); vvc<mint> fs(N); rep(i, N) { vc<mint> f(K); f.at(0) += 1; f.at(A.at(i)) += 1; fs.at(i) = f; } vc<pll> LR(N - M + 1); rep(i, N - M + 1) LR.at(i) = {i, i + M}; auto res = dc_range_prod_left_right<Mn>(fs, LR); fec([f, g] : res) { mint ans = 0; rep(i, K) ans += f.at(i) * g.at((K - i) % K); PRINT(ans - 1); } } void test() { } template <auto init, auto main2, auto test> 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<init, main2, test> main_dummy; } int main() {}