結果
問題 | No.1011 Infinite Stairs |
ユーザー | Pachicobue |
提出日時 | 2020-03-20 21:36:38 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 122 ms / 2,000 ms |
コード長 | 40,279 bytes |
コンパイル時間 | 2,830 ms |
コンパイル使用メモリ | 225,840 KB |
実行使用メモリ | 24,696 KB |
最終ジャッジ日時 | 2024-07-17 11:46:09 |
合計ジャッジ時間 | 4,437 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 4 ms
5,376 KB |
testcase_03 | AC | 79 ms
14,048 KB |
testcase_04 | AC | 40 ms
8,668 KB |
testcase_05 | AC | 91 ms
24,696 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 7 ms
5,376 KB |
testcase_08 | AC | 2 ms
5,376 KB |
testcase_09 | AC | 2 ms
5,376 KB |
testcase_10 | AC | 7 ms
5,376 KB |
testcase_11 | AC | 35 ms
8,796 KB |
testcase_12 | AC | 5 ms
5,376 KB |
testcase_13 | AC | 27 ms
8,644 KB |
testcase_14 | AC | 3 ms
5,376 KB |
testcase_15 | AC | 19 ms
8,768 KB |
testcase_16 | AC | 122 ms
24,540 KB |
testcase_17 | AC | 17 ms
5,376 KB |
testcase_18 | AC | 50 ms
13,772 KB |
testcase_19 | AC | 4 ms
5,376 KB |
testcase_20 | AC | 5 ms
5,376 KB |
testcase_21 | AC | 24 ms
8,672 KB |
testcase_22 | AC | 84 ms
13,648 KB |
testcase_23 | AC | 66 ms
13,640 KB |
testcase_24 | AC | 34 ms
8,672 KB |
testcase_25 | AC | 21 ms
8,800 KB |
testcase_26 | AC | 31 ms
8,640 KB |
ソースコード
#include <bits/stdc++.h> // created [2020/03/20] 21:31:31 #pragma GCC diagnostic ignored "-Wsign-compare" #pragma GCC diagnostic ignored "-Wsign-conversion" using i32 = int32_t; using i64 = int64_t; using u32 = uint32_t; using u64 = uint64_t; using uint = unsigned int; using usize = std::size_t; using ll = long long; using ull = unsigned long long; using ld = long double; template<typename T, usize n> using arr = T (&)[n]; template<typename T, usize n> using c_arr = const T (&)[n]; template<typename T> using max_heap = std::priority_queue<T>; template<typename T> using min_heap = std::priority_queue<T, std::vector<T>, std::greater<T>>; template<typename T> constexpr T popcount(const T u) { return u ? static_cast<T>(__builtin_popcountll(static_cast<u64>(u))) : static_cast<T>(0); } template<typename T> constexpr T log2p1(const T u) { return u ? static_cast<T>(64 - __builtin_clzll(static_cast<u64>(u))) : static_cast<T>(0); } template<typename T> constexpr T msbp1(const T u) { return log2p1(u); } template<typename T> constexpr T lsbp1(const T u) { return __builtin_ffsll(u); } template<typename T> constexpr T clog(const T u) { return u ? log2p1(u - 1) : static_cast<T>(u); } template<typename T> constexpr bool ispow2(const T u) { return u and (static_cast<u64>(u) & static_cast<u64>(u - 1)) == 0; } template<typename T> constexpr T ceil2(const T u) { return static_cast<T>(1) << clog(u); } template<typename T> constexpr T floor2(const T u) { return u == 0 ? static_cast<T>(0) : static_cast<T>(1) << (log2p1(u) - 1); } template<typename T> constexpr bool btest(const T mask, const usize ind) { return static_cast<bool>((static_cast<u64>(mask) >> ind) & static_cast<u64>(1)); } template<typename T> void bset(T& mask, const usize ind) { mask |= (static_cast<T>(1) << ind); } template<typename T> void breset(T& mask, const usize ind) { mask &= ~(static_cast<T>(1) << ind); } template<typename T> void bflip(T& mask, const usize ind) { mask ^= (static_cast<T>(1) << ind); } template<typename T> void bset(T& mask, const usize ind, const bool b) { (b ? bset(mask, ind) : breset(mask, ind)); } template<typename T> constexpr T bcut(const T mask, const usize ind) { return ind == 0 ? static_cast<T>(0) : static_cast<T>((static_cast<u64>(mask) << (64 - ind)) >> (64 - ind)); } template<typename T> bool chmin(T& a, const T& b) { return (a > b ? a = b, true : false); } template<typename T> bool chmax(T& a, const T& b) { return (a < b ? a = b, true : false); } constexpr unsigned int mod = 1000000007; template<typename T> constexpr T inf_v = std::numeric_limits<T>::max() / 4; template<typename Real> constexpr Real pi_v = Real{3.141592653589793238462643383279502884}; auto mfp = [](auto&& f) { return [=](auto&&... args) { return f(f, std::forward<decltype(args)>(args)...); }; }; template<typename T> T in() { T v; return std::cin >> v, v; } template<typename T, typename Uint, usize n, usize i> T in_v(typename std::enable_if<(i == n), c_arr<Uint, n>>::type) { return in<T>(); } template<typename T, typename Uint, usize n, usize i> auto in_v(typename std::enable_if<(i < n), c_arr<Uint, n>>::type& szs) { const usize s = (usize)szs[i]; std::vector<decltype(in_v<T, Uint, n, i + 1>(szs))> ans(s); for (usize j = 0; j < s; j++) { ans[j] = in_v<T, Uint, n, i + 1>(szs); } return ans; } template<typename T, typename Uint, usize n> auto in_v(c_arr<Uint, n> szs) { return in_v<T, Uint, n, 0>(szs); } template<typename... Types> auto in_t() { return std::tuple<std::decay_t<Types>...>{in<Types>()...}; } struct io_init { io_init() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(20); } void clear() { std::cin.tie(), std::ios::sync_with_stdio(true); } } io_setting; int out() { return 0; } template<typename T> int out(const T& v) { return std::cout << v, 0; } template<typename T> int out(const std::vector<T>& v) { for (usize i = 0; i < v.size(); i++) { if (i > 0) { std::cout << ' '; } out(v[i]); } return 0; } template<typename T1, typename T2> int out(const std::pair<T1, T2>& v) { return out(v.first), std::cout << ' ', out(v.second), 0; } template<typename T, typename... Args> int out(const T& v, const Args... args) { return out(v), std::cout << ' ', out(args...), 0; } template<typename... Args> int outln(const Args... args) { return out(args...), std::cout << '\n', 0; } template<typename... Args> int outel(const Args... args) { return out(args...), std::cout << std::endl, 0; } # define SHOW(...) static_cast<void>(0) constexpr ull TEN(const usize n) { return n == 0 ? 1ULL : TEN(n - 1) * 10ULL; } template<typename T, typename Uint, usize n, usize i> auto make_v(typename std::enable_if<(i == n), c_arr<Uint, n>>::type, const T& v = T{}) { return v; } template<typename T, typename Uint, usize n, usize i> auto make_v(typename std::enable_if<(i < n), c_arr<Uint, n>>::type szs, const T& v = T{}) { const usize s = (usize)szs[i]; return std::vector<decltype(make_v<T, Uint, n, i + 1>(szs, v))>(s, make_v<T, Uint, n, i + 1>(szs, v)); } template<typename T, typename Uint, usize n> auto make_v(c_arr<Uint, n> szs, const T& t = T{}) { return make_v<T, Uint, n, 0>(szs, t); } class xoshiro { public: using result_type = uint32_t; static constexpr result_type min() { return std::numeric_limits<result_type>::min(); } static constexpr result_type max() { return std::numeric_limits<result_type>::max(); } xoshiro() : xoshiro(std::random_device{}()) {} xoshiro(uint64_t seed) { uint64_t z = 0; for (int i = 0; i < 4; i++) { z = (seed += 0x9e3779b97f4a7c15), z = (z ^ (z >> 33)) * 0x62A9D9ED799705F5, z = (z ^ (z >> 28)) * 0xCB24D0A5C88C35B3, s[i] = static_cast<result_type>(z >> 32); } } result_type operator()() { const result_type result = rotl(s[1] * 5, 7) * 9, t = s[1] << 9; return s[2] ^= s[0], s[3] ^= s[1], s[1] ^= s[2], s[0] ^= s[3], s[2] ^= t, s[3] = rotl(s[3], 11), result; } void discard(const usize rep) { for (usize i = 0; i < rep; i++) { (*this)(); } } private: result_type s[4]; static result_type rotl(const result_type x, const int k) { return (x << k) | (x >> (32 - k)); } }; class xoshiro_64 { public: using result_type = uint64_t; static constexpr result_type min() { return std::numeric_limits<result_type>::min(); } static constexpr result_type max() { return std::numeric_limits<result_type>::max(); } xoshiro_64() : xoshiro_64(std::random_device{}()) {} xoshiro_64(uint64_t seed) { uint64_t z = 0; for (int i = 0; i < 4; i++) { z = (seed += 0x9e3779b97f4a7c15), z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9, z = (z ^ (z >> 27)) * 0x94d049bb133111eb, s[i] = static_cast<result_type>(z ^ (z >> 31)); } } result_type operator()() { const result_type result = rotl(s[1] * 5, 7) * 9, t = s[1] << 17; return s[2] ^= s[0], s[3] ^= s[1], s[1] ^= s[2], s[0] ^= s[3], s[2] ^= t, s[3] = rotl(s[3], 45), result; } void discard(const usize rep) { for (usize i = 0; i < rep; i++) { (*this)(); } } private: result_type s[4]; static result_type rotl(const result_type x, const int k) { return (x << k) | (x >> (64 - k)); } }; template<typename Rng> class rng_base { public: using rng_type = Rng; using result_type = typename rng_type::result_type; static constexpr result_type min() { return rng_type::min(); } static constexpr result_type max() { return rng_type::max(); } rng_base() : rng_base(std::random_device{}()) {} rng_base(const u64 seed) : rng(seed) {} ~rng_base() = default; result_type operator()(const result_type max = std::numeric_limits<result_type>::max()) { if (max == std::numeric_limits<result_type>::max()) { return static_cast<result_type>(rng()); } if (ispow2(max + 1)) { return static_cast<result_type>(rng() & max); } const result_type mask = static_cast<result_type>(ceil2(static_cast<u64>(max + 1))) - 1; while (true) { const result_type ans = static_cast<result_type>(rng() & mask); if (ans <= max) { return ans; } } } template<typename Int = result_type> Int operator()(const Int min, const Int max) { return min + (Int)(*this)(max - min); } operator bool() { return (bool)(*this)(0, 1); } template<typename Int> std::pair<Int, Int> pair(const Int min, const Int max) { return std::pair<Int, Int>{*this(min, max), *this(min, max)}; } template<typename Int> std::vector<Int> vec(const usize n, const Int min, const Int max) { std::vector<Int> v(n); for (usize i = 0; i < n; i++) { v[i] = (*this)(min, max); } return v; } std::vector<usize> perm(const usize n) { std::vector<usize> ans(n); std::iota(ans.begin(), ans.end(), 0UL); std::shuffle(ans.begin(), ans.end(), rng); return ans; } private: Rng rng; }; using rng_mt = rng_base<std::mt19937>; using rng_mt64 = rng_base<std::mt19937_64>; using rng_xoshiro = rng_base<xoshiro>; using rng_xoshiro64 = rng_base<xoshiro_64>; rng_mt g_rng_mt; rng_mt64 g_rng_mt64; rng_xoshiro g_rng_xo; rng_xoshiro64 g_rng_xo64; template<typename mint> mint modsqrt(const mint& x) { if (x == 0) { return 0; } const uint p = mint::mod(); if (p == 2) { return 1; } if ((x ^ ((p - 1) / 2)) != 1) { return 0; } uint Q = p - 1, S = 0; while (Q % 2 == 0) { Q /= 2, S++; } mint z = 1; while (true) { z = mint(g_rng_xo(2U, p - 1)); if ((z ^ ((p - 1) / 2)) != 1) { break; } } uint M = S; mint c = z ^ Q, t = x ^ Q, R = x ^ ((Q + 1) / 2); while (true) { if (t == 0) { return 0; } if (t == 1) { return R; } mint s = t * t; uint i = 1; for (;; i++) { if (s == 1) { break; } s *= s; } mint b = c; for (uint j = 0; j < M - i - 1; j++) { b *= b; } M = i, c = b * b, t *= b * b, R *= b; } return 0; } template<typename Real> struct complex { using value_type = Real; complex() : real{Real{0}}, imag{Real{0}} {} complex(const complex&) = default; complex(const Real& theta) : real(std::cos(theta)), imag(std::sin(theta)) {} complex(const Real& r, const Real& i) : real{r}, imag{i} {} ~complex() = default; friend complex operator+(const complex& c) { return c; } friend complex operator-(const complex& c) { return complex{-c.real, -c.imag}; } friend complex operator+(const complex& c1, const complex& c2) { return complex{c1.real + c2.real, c1.imag + c2.imag}; } friend complex operator-(const complex& c1, const complex& c2) { return complex{c1.real - c2.real, c1.imag - c2.imag}; } friend complex operator*(const complex& c1, const complex& c2) { return complex{c1.real * c2.real - c1.imag * c2.imag, c1.real * c2.imag + c1.imag * c2.real}; } friend complex operator*(const complex& c, const Real& r) { return complex{c.real * r, c.imag * r}; } friend complex operator/(complex& c1, complex& c2) { c1* c2.conj() / c2.norm(); } friend bool operator==(const complex& c1, const complex& c2) { return c1.real == c2.real and c1.imag == c2.imag; } friend bool operator!=(const complex& c1, const complex& c2) { return not(c1 == c2); } friend complex& operator+=(complex& c1, const complex& c2) { return c1.real += c2.real, c1.imag += c2.imag, c1; } friend complex& operator-=(complex& c1, const complex& c2) { return c1.real += c2.real, c1.imag += c2.imag, c1; } friend complex& operator*=(complex& c1, const complex& c2) { return c1 = c1 * c2; } friend complex& operator*=(complex& c, const Real& r) { return c = c * r; } friend complex& operator/=(complex& c1, const complex& c2) { return c1 = c1 / c2; } complex conj() const { return complex{real, -imag}; } Real norm() const { return real * real + imag * imag; } Real abs() const { return std::sqrt(norm()); } Real arg() const { return std::atan2(imag, real); } friend std::ostream& operator<<(std::ostream& os, const complex& c) { return os << c.real << "+" << c.imag << "i"; } Real real, imag; }; template<typename T> T gcd(const T& a, const T& b) { return a < 0 ? gcd(-a, b) : b < 0 ? gcd(a, -b) : (a > b ? gcd(b, a) : a == 0 ? b : gcd(b % a, a)); } template<typename T> T lcm(const T& a, const T& b) { return a / gcd(a, b) * b; } template<typename T> constexpr std::pair<T, T> extgcd(const T a, const T b) { if (b == 0) { return std::pair<T, T>{1, 0}; } const auto g = gcd(a, b), da = std::abs(b) / g; const auto p = extgcd(b, a % b); const auto x = (da + p.second % da) % da, y = (g - a * x) / b; return {x, y}; } template<typename T> constexpr T inverse(const T a, const T mod) { return extgcd(a, mod).first; } template<uint mod_value, bool dynamic = false> class modint_base { public: template<typename UInt = uint> static std::enable_if_t<dynamic, const UInt> mod() { return mod_ref(); } template<typename UInt = uint> static constexpr std::enable_if_t<not dynamic, const UInt> mod() { return mod_value; } template<typename UInt = uint> static void set_mod(const std::enable_if_t<dynamic, const UInt> mod) { mod_ref() = mod, inv_ref() = {1, 1}; } modint_base() : v{0} {} modint_base(const ll val) : v{norm(static_cast<uint>(val % static_cast<ll>(mod()) + static_cast<ll>(mod())))} {} modint_base(const modint_base& n) : v{n()} {} explicit operator bool() const { return v != 0; } bool operator!() const { return not static_cast<bool>(*this); } modint_base& operator=(const modint_base& m) { return v = m(), (*this); } modint_base& operator=(const ll val) { return v = norm(uint(val % static_cast<ll>(mod()) + static_cast<ll>(mod()))), (*this); } friend modint_base operator+(const modint_base& m) { return m; } friend modint_base operator-(const modint_base& m) { return make(norm(mod() - m.v)); } friend modint_base operator+(const modint_base& m1, const modint_base& m2) { return make(norm(m1.v + m2.v)); } friend modint_base operator-(const modint_base& m1, const modint_base& m2) { return make(norm(m1.v + mod() - m2.v)); } friend modint_base operator*(const modint_base& m1, const modint_base& m2) { return make(static_cast<uint>(static_cast<ll>(m1.v) * static_cast<ll>(m2.v) % static_cast<ll>(mod()))); } friend modint_base operator/(const modint_base& m1, const modint_base& m2) { return m1 * inv(m2.v); } friend modint_base operator+(const modint_base& m, const ll val) { return modint_base{static_cast<ll>(m.v) + val}; } friend modint_base operator-(const modint_base& m, const ll val) { return modint_base{static_cast<ll>(m.v) - val}; } friend modint_base operator*(const modint_base& m, const ll val) { return modint_base{static_cast<ll>(m.v) * (val % static_cast<ll>(mod()))}; } friend modint_base operator/(const modint_base& m, const ll val) { return modint_base{static_cast<ll>(m.v) * inv(val)}; } friend modint_base operator+(const ll val, const modint_base& m) { return modint_base{static_cast<ll>(m.v) + val}; } friend modint_base operator-(const ll val, const modint_base& m) { return modint_base{-static_cast<ll>(m.v) + val}; } friend modint_base operator*(const ll val, const modint_base& m) { return modint_base{static_cast<ll>(m.v) * (val % static_cast<ll>(mod()))}; } friend modint_base operator/(const ll val, const modint_base& m) { return modint_base{val * inv(static_cast<ll>(m.v))}; } friend modint_base& operator+=(modint_base& m1, const modint_base& m2) { return m1 = m1 + m2; } friend modint_base& operator-=(modint_base& m1, const modint_base& m2) { return m1 = m1 - m2; } friend modint_base& operator*=(modint_base& m1, const modint_base& m2) { return m1 = m1 * m2; } friend modint_base& operator/=(modint_base& m1, const modint_base& m2) { return m1 = m1 / m2; } friend modint_base& operator+=(modint_base& m, const ll val) { return m = m + val; } friend modint_base& operator-=(modint_base& m, const ll val) { return m = m - val; } friend modint_base& operator*=(modint_base& m, const ll val) { return m = m * val; } friend modint_base& operator/=(modint_base& m, const ll val) { return m = m / val; } friend modint_base operator^(const modint_base& m, const ll n) { return power(m.v, n); } friend modint_base& operator^=(modint_base& m, const ll n) { return m = m ^ n; } friend bool operator==(const modint_base& m1, const modint_base& m2) { return m1.v == m2.v; } friend bool operator!=(const modint_base& m1, const modint_base& m2) { return not(m1 == m2); } friend bool operator==(const modint_base& m, const ll val) { return m.v == norm(static_cast<uint>(static_cast<ll>(mod()) + val % static_cast<ll>(mod()))); } friend bool operator!=(const modint_base& m, const ll val) { return not(m == val); } friend bool operator==(const ll val, const modint_base& m) { return m.v == norm(static_cast<uint>(static_cast<ll>(mod()) + val % static_cast<ll>(mod()))); } friend bool operator!=(const ll val, const modint_base& m) { return not(m == val); } friend std::istream& operator>>(std::istream& is, modint_base& m) { ll v; return is >> v, m = v, is; } friend std::ostream& operator<<(std::ostream& os, const modint_base& m) { return os << m(); } uint operator()() const { return v; } static modint_base small_inv(const usize n) { auto& in = inv_ref(); if (n < in.size()) { return in[n]; } for (usize i = in.size(); i <= n; i++) { in.push_back(-in[modint_base::mod() % i] * (modint_base::mod() / i)); } return in.back(); } std::pair<ll, ll> quad() const { const auto ans = quad_r(v, mod()); ll x = std::get<0>(ans), y = std::get<1>(ans); if (y < 0) { x = -x, y = -y; } return {x, y}; } private: static std::tuple<ll, ll, ll> quad_r(const ll r, const ll p) // r = x/y (mod p), (x,y,z) s.t. x=yr+pz { if (std::abs(r) <= 1000) { return {r, 1, 0}; } ll nr = p % r, q = p / r; if (nr * 2LL >= r) { nr -= r, q++; } if (nr * 2LL <= -r) { nr += r, q--; } const auto sub = quad_r(nr, r); const ll x = std::get<0>(sub), z = std::get<1>(sub), y = std::get<2>(sub); return {x, y - q * z, z}; } template<typename UInt = uint> static std::enable_if_t<dynamic, UInt&> mod_ref() { static UInt mod = 0; return mod; } static uint norm(const uint x) { return x < mod() ? x : x - mod(); } static modint_base make(const uint x) { modint_base m; return m.v = x, m; } static modint_base power(modint_base x, ull n) { modint_base ans = 1; for (; n; n >>= 1, x *= x) { if (n & 1) { ans *= x; } } return ans; } static modint_base inv(const ll v) { return v <= 2000000 ? small_inv(static_cast<usize>(v)) : modint_base{inverse(v, static_cast<ll>(mod()))}; } static std::vector<modint_base>& inv_ref() { static std::vector<modint_base> in{1, 1}; return in; } uint v; }; template<uint mod> using modint = modint_base<mod, false>; template<uint id> using dynamic_modint = modint_base<id, true>; template<typename Real = double> class fft { private: static constexpr usize depth = 30; static constexpr Real pi = pi_v<Real>; static void transform(std::vector<complex<Real>>& a, const usize lg, const bool rev) { static std::vector<complex<Real>> root[depth]; const usize sz = a.size(); assert((1UL << lg) == sz); if (root[lg].empty()) { root[lg].reserve(sz), root[lg].resize(sz); for (usize i = 0; i < sz; i++) { root[lg][i] = complex<Real>(pi * Real(2 * i) / Real(sz)); } } std::vector<complex<Real>> tmp(sz); for (usize w = (sz >> 1); w > 0; w >>= 1) { for (usize y = 0; y < (sz >> 1); y += w) { const complex<Real> r = rev ? root[lg][y].conj() : root[lg][y]; for (usize x = 0; x < w; x++) { const auto u = a[y << 1 | x], v = a[y << 1 | x | w] * r; tmp[y | x] = u + v, tmp[y | x | (sz >> 1)] = u - v; } } std::swap(tmp, a); } } public: using value_type = Real; fft() = delete; template<typename T = ll, typename I = int> static std::vector<T> simple_convolute(const std::vector<I>& a, const std::vector<I>& b) { const usize need = a.size() + b.size() - 1, lg = clog(need), sz = 1UL << lg; std::vector<complex<Real>> x(sz), y(sz); for (usize i = 0; i < a.size(); i++) { x[i] = {(Real)a[i], (Real)0}; } for (usize i = 0; i < b.size(); i++) { y[i] = {(Real)b[i], (Real)0}; } transform(x, lg, false), transform(y, lg, false); for (usize i = 0; i < sz; i++) { x[i] *= y[i]; } transform(x, lg, true); std::vector<T> ans(need); for (usize i = 0; i < need; i++) { ans[i] = (T)std::round(x[i].real / (Real)sz); } return ans; } template<typename T = ll, usize division = 2, typename I = int> static std::vector<T> convolute(const std::vector<I>& a, const std::vector<I>& b) { constexpr usize bitnum = (depth + division - 1) / division; const usize need = a.size() + b.size() - 1, lg = clog(need), sz = 1UL << lg; std::vector<complex<value_type>> x[division], y[division], tmp(sz); for (usize i = 0; i < division; i++) { x[i].reserve(sz), x[i].resize(sz), y[i].reserve(sz), y[i].resize(sz); std::fill(tmp.begin() + std::min(a.size(), b.size()), tmp.end(), complex<value_type>{}); for (usize j = 0; j < a.size(); j++) { tmp[j].real = value_type((a[j] >> (bitnum * i)) & ((1 << bitnum) - 1)); } for (usize j = 0; j < b.size(); j++) { tmp[j].imag = value_type((b[j] >> (bitnum * i)) & ((1 << bitnum) - 1)); } transform(tmp, lg, false); for (usize j = 0; j < sz; j++) { tmp[j] *= value_type(0.5); } for (usize j = 0; j < sz; j++) { const usize k = j == 0 ? 0UL : sz - j; x[i][j] = complex<value_type>{tmp[j].real + tmp[k].real, tmp[j].imag - tmp[k].imag}, y[i][j] = complex<value_type>{tmp[j].imag + tmp[k].imag, -tmp[j].real + tmp[k].real}; } } std::vector<complex<value_type>> z[division]; for (usize i = 0; i < division; i++) { z[i].reserve(sz), z[i].resize(sz); } for (usize a = 0; a < division; a++) { for (usize b = 0; b < division; b++) { for (usize i = 0; i < sz; i++) { if (a + b < division) { z[a + b][i] += x[a][i] * y[b][i]; } else { z[a + b - division][i] += x[a][i] * y[b][i] * complex<value_type>(0, 1); } } } } for (usize i = 0; i < division; i++) { transform(z[i], lg, true); } std::vector<T> ans(need); T base = 1; for (usize k = 0; k < 2 * division - 1; k++, base *= (1LL << bitnum)) { for (usize i = 0; i < need; i++) { if (k < division) { ans[i] += base * T(std::round(z[k][i].real / value_type(sz))); } else { ans[i] += base * T(std::round(z[k - division][i].imag / value_type(sz))); } } } return ans; } template<uint mod, bool dynamic = false, usize division = 2> static std::vector<modint_base<mod, dynamic>> convolute(const std::vector<modint_base<mod, dynamic>>& a, const std::vector<modint_base<mod, dynamic>>& b) { using mint = modint_base<mod, dynamic>; constexpr usize bitnum = (depth + division - 1) / division; const usize need = a.size() + b.size() - 1, lg = clog(need), sz = 1UL << lg; std::vector<complex<value_type>> x[division], y[division], tmp(sz); for (usize i = 0; i < division; i++) { x[i].reserve(sz), x[i].resize(sz), y[i].reserve(sz), y[i].resize(sz); std::fill(tmp.begin() + std::min(a.size(), b.size()), tmp.end(), complex<value_type>{}); for (usize j = 0; j < a.size(); j++) { tmp[j].real = value_type((a[j]() >> (bitnum * i)) & ((1 << bitnum) - 1)); } for (usize j = 0; j < b.size(); j++) { tmp[j].imag = value_type((b[j]() >> (bitnum * i)) & ((1 << bitnum) - 1)); } transform(tmp, lg, false); for (usize j = 0; j < sz; j++) { tmp[j] *= value_type(0.5); } for (usize j = 0; j < sz; j++) { const usize k = j == 0 ? 0UL : sz - j; x[i][j] = complex<value_type>{tmp[j].real + tmp[k].real, tmp[j].imag - tmp[k].imag}, y[i][j] = complex<value_type>{tmp[j].imag + tmp[k].imag, -tmp[j].real + tmp[k].real}; } } std::vector<complex<value_type>> z[division]; for (usize i = 0; i < division; i++) { z[i].reserve(sz), z[i].resize(sz); } for (usize a = 0; a < division; a++) { for (usize b = 0; b < division; b++) { for (usize i = 0; i < sz; i++) { if (a + b < division) { z[a + b][i] += x[a][i] * y[b][i]; } else { z[a + b - division][i] += x[a][i] * y[b][i] * complex<value_type>(0, 1); } } } } for (usize i = 0; i < division; i++) { transform(z[i], lg, true); } std::vector<mint> ans(need); mint base = 1; for (usize k = 0; k < 2 * division - 1; k++, base *= (1LL << bitnum)) { for (usize i = 0; i < need; i++) { if (k < division) { ans[i] += int((base * ll(std::round(z[k][i].real / value_type(sz))))()); } else { ans[i] += int((base * ll(std::round(z[k - division][i].imag / value_type(sz))))()); } } } return ans; } }; template<uint mod = 998244353, uint root = 3> class ntt { private: using value_type = modint<mod>; static constexpr usize depth = 30; static void transform(std::vector<value_type>& a, const usize lg, const bool rev) { const usize N = a.size(); assert(1UL << lg == N); static std::vector<value_type> R[depth]; if (R[lg].empty()) { R[lg].reserve(N), R[lg].resize(N, value_type(1)); const value_type r = value_type(root) ^ ((mod - 1) / N); for (usize i = 1; i < N; i++) { R[lg][i] = R[lg][i - 1] * r; } } std::vector<value_type> tmp(N); for (usize w = (N >> 1); w > 0; w >>= 1) { for (usize y = 0; y < (N >> 1); y += w) { const value_type r = rev ? R[lg][y == 0 ? 0 : N - y] : R[lg][y]; for (usize x = 0; x < w; x++) { const auto u = a[y << 1 | x], v = a[y << 1 | x | w]() * r; tmp[y | x] = u + v, tmp[y | x | (N >> 1)] = u - v; } } std::swap(tmp, a); } if (rev) { for (usize i = 0; i < N; i++) { a[i] /= value_type(N); } } } public: ntt() = delete; static std::vector<value_type> convolute(const std::vector<value_type>& a, const std::vector<value_type>& b) { const usize need = a.size() + b.size() - 1, lg = clog(need), sz = 1UL << lg; std::vector<value_type> A(sz, 0), B(sz, 0); for (usize i = 0; i < a.size(); i++) { A[i] = a[i](); } for (usize i = 0; i < b.size(); i++) { B[i] = b[i](); } transform(A, lg, false), transform(B, lg, false); for (usize i = 0; i < sz; i++) { A[i] *= B[i]; } transform(A, lg, true); std::vector<value_type> ans(need); for (usize i = 0; i < need; i++) { ans[i] = int(A[i]()); } return ans; } }; template<uint mod, uint root, bool dynamic, uint fft_division> class polynomial_base { public: using value_type = modint_base<mod, dynamic>; polynomial_base() : v(0) {} polynomial_base(const value_type& r) : v{r} { shrink(); } polynomial_base(const polynomial_base& p) : v{p()} {} template<typename InIt> polynomial_base(const InIt first, const InIt last) : v{first, last} { shrink(); } polynomial_base(const std::initializer_list<value_type>&& list) : v{list} { shrink(); } const std::vector<value_type>& operator()() const { return v; } value_type& operator[](const usize i) { return v[i]; } const value_type operator[](const usize i) const { return (i < size() ? v[i] : value_type(0)); } void set(const usize k, const value_type& c) { if (k >= size()) { v.resize(k + 1); } v[k] = c; } polynomial_base& operator=(const polynomial_base& m) { return v = m(), *this; } polynomial_base& operator=(const value_type& val) { return (*this) = polynomial_base(val); } friend polynomial_base operator+(const polynomial_base& p) { return p; } friend polynomial_base operator-(const polynomial_base& p) { polynomial_base ans; for (usize i = 0; i < p.size(); i++) { ans.set(i, -p[i]); } return ans; } friend polynomial_base operator+(const polynomial_base& p, const polynomial_base& q) { const usize sz = std::max(p.size(), q.size()); polynomial_base ans; for (usize i = 0; i < sz; i++) { ans.set(i, p[i] + q[i]); } return ans; } friend polynomial_base operator-(const polynomial_base& p, const polynomial_base& q) { const usize sz = std::max(p.size(), q.size()); polynomial_base ans; for (usize i = 0; i < sz; i++) { ans.set(i, p[i] - q[i]); } return ans; } friend polynomial_base operator*(const polynomial_base& p, const polynomial_base& q) { constexpr int L = 300; return p.size() <= L or q.size() <= L ? naive_multiply(p, q) : fft_multiply(p, q); } friend polynomial_base operator+(const polynomial_base& p, const value_type& r) { polynomial_base ans = p; ans.set(0, p[0] + r); return ans; } friend polynomial_base operator-(const polynomial_base& p, const value_type& r) { polynomial_base ans = p; ans.set(0, p[0] - r); return ans; } friend polynomial_base operator*(const polynomial_base& p, const value_type& r) { polynomial_base ans; for (usize i = 0; i < p.size(); i++) { ans.set(i, p[i] * r); } return ans; } friend polynomial_base operator/(const polynomial_base& p, const value_type& r) { polynomial_base ans; for (usize i = 0; i < p.size(); i++) { ans.set(i, p[i] / r); } return ans; } friend polynomial_base operator+(const value_type& r, const polynomial_base& q) { return q + r; } friend polynomial_base operator-(const value_type& r, const polynomial_base& q) { return -q + r; } friend polynomial_base operator*(const value_type& r, const polynomial_base& q) { return q * r; } friend polynomial_base operator>>(const polynomial_base& p, const usize s) { return quot_by_pow(p, s); } friend polynomial_base operator<<(const polynomial_base& p, const usize s) { return prod_by_pow(p, s); } friend polynomial_base operator/(const polynomial_base& p, const polynomial_base& q) { return div(p, q); } friend polynomial_base operator%(const polynomial_base& p, const polynomial_base& q) { return rem(p, q); } friend bool operator==(const polynomial_base& p, const polynomial_base& q) { return p() == q(); } friend bool operator!=(const polynomial_base& p, const polynomial_base& q) { return not(p == q); } friend polynomial_base& operator+=(polynomial_base& p, const polynomial_base& q) { return p = p + q; } friend polynomial_base& operator-=(polynomial_base& p, const polynomial_base& q) { return p = p - q; } friend polynomial_base& operator*=(polynomial_base& p, const polynomial_base& q) { return p = p * q; } friend polynomial_base& operator+=(polynomial_base& p, const value_type& r) { return p = p + r; } friend polynomial_base& operator-=(polynomial_base& p, const value_type& r) { return p = p - r; } friend polynomial_base& operator*=(polynomial_base& p, const value_type& r) { return p = p * r; } friend polynomial_base& operator/=(polynomial_base& p, const value_type& r) { return p = p / r; } friend polynomial_base& operator>>=(polynomial_base& p, const usize s) { return p = (p >> s); } friend polynomial_base& operator<<=(polynomial_base& p, const usize s) { return p = (p << s); } friend polynomial_base& operator/=(polynomial_base& p, const polynomial_base& q) { return p = p / q; } friend polynomial_base& operator%=(polynomial_base& p, const polynomial_base& q) { return p = p % q; } static polynomial_base prod_by_pow(const polynomial_base p, const usize s) { const usize sz = p.size(); if (sz == 0) { return polynomial_base(); } polynomial_base ans; for (usize i = 0; i < sz; i++) { ans.set(i + s, p[i]); } return ans; } static polynomial_base quot_by_pow(const polynomial_base& p, const usize s) { const usize N = p.size(); if (N <= s) { return polynomial_base(); } polynomial_base ans; for (usize i = 0; i < N - s; i++) { ans.set(i, p[i + s]); } return ans; } static polynomial_base rem_by_pow(const polynomial_base& p, const usize k) { return p.size() <= k ? p : polynomial_base(p().begin(), p().begin() + k); } static polynomial_base derivative_of(const polynomial_base& p) { if (p.size() <= 1) { return polynomial_base{0}; } polynomial_base ans; for (usize i = 1; i < p.size(); i++) { ans.set(i - 1, p[i] * i); } return ans; } static polynomial_base primitive_of(const polynomial_base& p) { polynomial_base ans; for (usize i = 1; i <= p.size(); i++) { ans.set(i, p[i - 1] / i); } return ans; } static polynomial_base inv(const polynomial_base& p, const usize k) { polynomial_base q{value_type(1) / p[0]}; for (usize i = 1; i < k; i *= 2) { q = rem_by_pow(q * (2 - rem_by_pow(p, 2 * i) * q), 2 * i); } return rem_by_pow(q, k); } static polynomial_base log(const polynomial_base& p, const usize k) { return k == 0 ? polynomial_base{} : primitive_of(rem_by_pow(derivative_of(rem_by_pow(p, k)) * inv(p, k - 1), k - 1)); } static polynomial_base exp(const polynomial_base& p, const usize k) { polynomial_base q{value_type(1)}; for (usize i = 1; i < k; i *= 2) { q = rem_by_pow(q * (rem_by_pow(p, 2 * i) + 1 - log(q, 2 * i)), 2 * i); } return rem_by_pow(q, k); } static polynomial_base sqrt(const polynomial_base& p_, usize k) { if (p_.size() == 0) { return polynomial_base{}; } usize z = 0; for (; z < p_.size(); z++) { if (p_[z] != 0) { break; } } if (z % 2 == 1 or z >= 2 * k) { return polynomial_base{}; } const auto p = quot_by_pow(p_, z); k -= z / 2; const value_type rt = modsqrt(p[0]); if (rt * rt != p[0]) { return polynomial_base{}; } polynomial_base q{rt}; for (usize i = 1; i < k; i *= 2) { q = rem_by_pow(q + rem_by_pow(p, 2 * i) * inv(q, 2 * i), 2 * i) / 2; } return prod_by_pow(rem_by_pow(q, k), z / 2); } template<typename Int> static polynomial_base pow(const polynomial_base& p, const Int k, const usize s) { if (k == 0) { return polynomial_base(1); } if (k % 2 == 1) { return rem_by_pow(pow(p, k - 1, s) * p, s); } else { const auto q = pow(p, k / 2, s); return rem_by_pow(q * q, s); } } template<typename Int> static polynomial_base rem_of_pow(const polynomial_base& p, const Int k) { const usize B = p.size() * 2 - 1; const auto q = p.pseudo_inv(B); polynomial_base ans{1}; const usize D = log2p1<usize>(k); for (usize i = 0; i < D; i++) { if (k & (static_cast<Int>(1) << (D - i - 1))) { ans = (ans.multiply_power(1)).rem(p, q, B); } if (D - i - 1) { ans = (ans * ans).rem(p, q, B); } } return ans; } usize size() const { return v.size(); } friend std::ostream& operator<<(std::ostream& os, const polynomial_base& p) { if (p.size() == 0) { return os << "0"; } for (usize i = 0; i < p.size(); i++) { os << (i != 0 ? "+" : "") << p[i] << (i != 0 ? i == 1 ? "X" : "X^" + std::to_string(i) : ""); } return os; } private: static std::vector<value_type> naive_convolute(const std::vector<value_type>& a, const std::vector<value_type>& b) { std::vector<value_type> ans(a.size() + b.size() - 1, 0); for (usize i = 0; i < a.size(); i++) { for (usize j = 0; j < b.size(); j++) { ans[i + j] += a[i] * b[j]; } } return ans; } static polynomial_base naive_multiply(const polynomial_base& p, const polynomial_base& q) { if (p.size() == 0 or q.size() == 0) { return polynomial_base{}; } const auto v = naive_convolute(p(), q()); return polynomial_base(v.begin(), v.end()); } template<typename Poly = polynomial_base> static std::enable_if_t<root == 0, Poly> fft_multiply(const polynomial_base& p, const polynomial_base& q) { if (p.size() == 0 or q.size() == 0) { return polynomial_base{}; } const auto v = fft<double>::convolute<mod, dynamic, fft_division>(p(), q()); return polynomial_base(v.begin(), v.end()); } template<typename Poly = polynomial_base> static std::enable_if_t<root != 0, Poly> fft_multiply(const polynomial_base& p, const polynomial_base& q) { if (p.size() == 0 or q.size() == 0) { return polynomial_base{}; } const auto v = ntt<mod, root>::convolute(p(), q()); return polynomial_base(v.begin(), v.end()); } static polynomial_base rev(const polynomial_base& p, const usize l) { std::vector<value_type> ans = p(); ans.resize(l), std::reverse(ans.begin(), ans.end()); return polynomial_base(ans.begin(), ans.end()); } static polynomial_base div(const polynomial_base& p, const polynomial_base& q) { assert(q.size() > 0); if (p.size() < q.size()) { return polynomial_base(); } const usize N = p.size(); const auto iq = pseudoInv(q, N); return div_by_pow(p * iq, N - 1); } static polynomial_base rem(const polynomial_base& p, const polynomial_base& q) { return p - div(p, q) * q; } static polynomial_base rem(const polynomial_base& p, const polynomial_base& q, const polynomial_base& iq, const usize B) { return p - q * (quot_by_pow(p * iq, B - 1)); } void shrink() { for (; not v.empty() and v.back() == 0; v.pop_back()) {} } static polynomial_base pseudo_inv(const polynomial_base& p, const usize B) { const usize N = p.size(); return rev(inv(rev(p, N), B + 2 > N ? clog(B - N + 2) : 0), B + 1 - N); } std::vector<value_type> v; }; template<uint mod, uint fft_division = 2> using polynomial = polynomial_base<mod, 0, false, fft_division>; template<uint mod, uint fft_division = 2> using dynamic_polynomial = polynomial_base<mod, 0, true, fft_division>; template<uint mod = 998244353, uint root = 3> using ntt_polynomial = polynomial_base<mod, root, false, 0>; int main() { const auto [N, d, K] = in_t<int, int, int>(); using poly = polynomial<mod>; std::vector<modint<mod>> v(d + 1, 0); for (int i = 1; i <= d; i++) { v[i] = 1; } poly f(v.begin(), v.end()); SHOW(f); f = poly::pow(f, N, K + 1); outln(f[K]); return 0; }