結果
| 問題 |
No.3213 depth max K
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-07-25 23:19:27 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 23 ms / 2,000 ms |
| コード長 | 65,094 bytes |
| コンパイル時間 | 1,811 ms |
| コンパイル使用メモリ | 160,264 KB |
| 実行使用メモリ | 7,720 KB |
| 最終ジャッジ日時 | 2025-07-25 23:19:33 |
| 合計ジャッジ時間 | 3,281 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 41 |
ソースコード
#include <deque>
#include <numeric>
#include <unordered_map>
#include <iostream>
#include <functional>
#include <vector>
#include <istream>
#include <set>
#include <string>
#include <ostream>
#include <map>
#include <fstream>
#include <array>
#include <optional>
#include <cassert>
#include <bitset>
#include <queue>
#include <utility>
#include <type_traits>
#include <iomanip>
#include <stack>
#include <list>
#include <unordered_set>
#include <cstdint>
#include <algorithm>
#include <iterator>
#ifndef KK2_TEMPLATE_PROCON_HPP
#define KK2_TEMPLATE_PROCON_HPP 1
#ifndef KK2_TEMPLATE_CONSTANT_HPP
#define KK2_TEMPLATE_CONSTANT_HPP 1
#ifndef KK2_TEMPLATE_TYPE_ALIAS_HPP
#define KK2_TEMPLATE_TYPE_ALIAS_HPP 1
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
using pi = std::pair<int, int>;
using pl = std::pair<i64, i64>;
using pil = std::pair<int, i64>;
using pli = std::pair<i64, int>;
template <class T> using vc = std::vector<T>;
template <class T> using vvc = std::vector<vc<T>>;
template <class T> using vvvc = std::vector<vvc<T>>;
template <class T> using vvvvc = std::vector<vvvc<T>>;
template <class T> using pq = std::priority_queue<T>;
template <class T> using pqi = std::priority_queue<T, std::vector<T>, std::greater<T>>;
#endif // KK2_TEMPLATE_TYPE_ALIAS_HPP
template <class T> constexpr T infty = 0;
template <> constexpr int infty<int> = (1 << 30) - 123;
template <> constexpr i64 infty<i64> = (1ll << 62) - (1ll << 31);
template <> constexpr i128 infty<i128> = (i128(1) << 126) - (i128(1) << 63);
template <> constexpr u32 infty<u32> = infty<int>;
template <> constexpr u64 infty<u64> = infty<i64>;
template <> constexpr u128 infty<u128> = infty<i128>;
template <> constexpr double infty<double> = infty<i64>;
template <> constexpr long double infty<long double> = infty<i64>;
constexpr int mod = 998244353;
constexpr int modu = 1e9 + 7;
constexpr long double PI = 3.14159265358979323846;
#endif // KK2_TEMPLATE_CONSTANT_HPP
#ifndef KK2_TEMPLATE_FUNCTION_UTIL_HPP
#define KK2_TEMPLATE_FUNCTION_UTIL_HPP 1
#ifndef KK2_MATH_MONOID_MAX_HPP
#define KK2_MATH_MONOID_MAX_HPP 1
#ifndef KK2_TYPE_TRAITS_IO_HPP
#define KK2_TYPE_TRAITS_IO_HPP 1
namespace kk2 {
namespace type_traits {
struct istream_tag {};
struct ostream_tag {};
} // namespace type_traits
template <typename T> using is_standard_istream =
typename std::conditional<std::is_same<T, std::istream>::value
|| std::is_same<T, std::ifstream>::value,
std::true_type,
std::false_type>::type;
template <typename T> using is_standard_ostream =
typename std::conditional<std::is_same<T, std::ostream>::value
|| std::is_same<T, std::ofstream>::value,
std::true_type,
std::false_type>::type;
template <typename T> using is_user_defined_istream = std::is_base_of<type_traits::istream_tag, T>;
template <typename T> using is_user_defined_ostream = std::is_base_of<type_traits::ostream_tag, T>;
template <typename T> using is_istream =
typename std::conditional<is_standard_istream<T>::value || is_user_defined_istream<T>::value,
std::true_type,
std::false_type>::type;
template <typename T> using is_ostream =
typename std::conditional<is_standard_ostream<T>::value || is_user_defined_ostream<T>::value,
std::true_type,
std::false_type>::type;
template <typename T> using is_istream_t = std::enable_if_t<is_istream<T>::value>;
template <typename T> using is_ostream_t = std::enable_if_t<is_ostream<T>::value>;
} // namespace kk2
#endif // KK2_TYPE_TRAITS_IO_HPP
namespace kk2 {
namespace monoid {
template <class S, class Compare = std::less<S>> struct Max {
static constexpr bool commutative = true;
using M = Max;
S a;
bool is_unit;
Max() : a(S()), is_unit(true) {}
Max(S a_) : a(a_), is_unit(false) {}
operator S() const { return a; }
inline static M op(M l, M r) {
if (l.is_unit or r.is_unit) return l.is_unit ? r : l;
return Compare{}(l.a, r.a) ? r : l;
}
inline static M unit() { return M(); }
bool operator==(const M &rhs) const {
return is_unit == rhs.is_unit and (is_unit or a == rhs.a);
}
bool operator!=(const M &rhs) const {
return is_unit != rhs.is_unit or (!is_unit and a != rhs.a);
}
template <class OStream, is_ostream_t<OStream> * = nullptr>
friend OStream &operator<<(OStream &os, const M &x) {
if (x.is_unit) os << "-inf";
else os << x.a;
return os;
}
template <class IStream, is_istream_t<IStream> * = nullptr>
friend IStream &operator>>(IStream &is, M &x) {
is >> x.a;
x.is_unit = false;
return is;
}
};
} // namespace monoid
} // namespace kk2
#endif // MATH_MONOID_MAX_HPP
#ifndef KK2_MATH_MONOID_MIN_HPP
#define KK2_MATH_MONOID_MIN_HPP 1
namespace kk2 {
namespace monoid {
template <class S, class Compare = std::less<S>> struct Min {
static constexpr bool commutative = true;
using M = Min;
S a;
bool is_unit;
Min() : a(S()), is_unit(true) {}
Min(S a_) : a(a_), is_unit(false) {}
operator S() const { return a; }
inline static M op(M l, M r) {
if (l.is_unit or r.is_unit) return l.is_unit ? r : l;
return Compare{}(l.a, r.a) ? l : r;
}
inline static M unit() { return M(); }
bool operator==(const M &rhs) const {
return is_unit == rhs.is_unit and (is_unit or a == rhs.a);
}
bool operator!=(const M &rhs) const {
return is_unit != rhs.is_unit or (!is_unit and a != rhs.a);
}
template <class OStream, is_ostream_t<OStream> * = nullptr>
friend OStream &operator<<(OStream &os, const M &x) {
if (x.is_unit) os << "inf";
else os << x.a;
return os;
}
template <class IStream, is_istream_t<IStream> * = nullptr>
friend IStream &operator>>(IStream &is, M &x) {
is >> x.a;
x.is_unit = false;
return is;
}
};
} // namespace monoid
} // namespace kk2
#endif // KK2_MATH_MONOID_MIN_HPP
#ifndef KK2_TYPE_TRAITS_CONTAINER_TRAITS_HPP
#define KK2_TYPE_TRAITS_CONTAINER_TRAITS_HPP 1
namespace kk2 {
template <typename T> struct is_vector : std::false_type {};
template <typename T, typename Alloc> struct is_vector<std::vector<T, Alloc>> : std::true_type {};
// コンテナかどうかを判定するtraits
template <typename T> struct is_container : std::false_type {};
// 基本的なコンテナ型の特殊化
template <typename T, typename Alloc> struct is_container<std::vector<T, Alloc>> : std::true_type {
};
template <typename CharT, typename Traits, typename Alloc>
struct is_container<std::basic_string<CharT, Traits, Alloc>> : std::true_type {};
template <typename T, std::size_t N> struct is_container<std::array<T, N>> : std::true_type {};
template <typename T, typename Alloc> struct is_container<std::deque<T, Alloc>> : std::true_type {};
template <typename T, typename Alloc> struct is_container<std::list<T, Alloc>> : std::true_type {};
// SFINAEでコンテナを判定するためのヘルパー
template <typename T> using is_container_t =
typename std::enable_if_t<is_container<T>::value, std::nullptr_t>;
} // namespace kk2
#endif // KK2_TYPE_TRAITS_CONTAINER_TRAITS_HPP
namespace kk2 {
template <class T, class... Sizes> auto make_vector(int first, Sizes... sizes) {
if constexpr (sizeof...(sizes) == 0) {
return std::vector<T>(first);
} else {
return std::vector<decltype(make_vector<T>(sizes...))>(first, make_vector<T>(sizes...));
}
}
template <class T, class U> void fill_all(std::vector<T> &v, const U &x) {
if constexpr (is_vector<T>::value) {
for (auto &u : v) fill_all(u, x);
} else {
std::fill(v.begin(), v.end(), T(x));
}
}
template <class T, class U> int iota_all(std::vector<T> &v, U x, int offset = 0) {
if constexpr (is_vector<T>::value) {
for (auto &u : v) offset += iota_all(u, x + offset);
} else {
for (auto &u : v) u = x++, ++offset;
}
return offset;
}
template <class C> int mysize(const C &c) { return size(c); }
// T: commutative monoid, F: (U, T) -> U
template <class U, class T, class F>
U all_monoid_prod(const std::vector<T> &v, U unit, const F &f) {
U res = unit;
if constexpr (is_vector<T>::value) {
for (const auto &x : v) res = f(res, all_monoid_prod(x, unit, f));
} else {
for (const auto &x : v) res = f(res, x);
}
return res;
}
template <class U, class T> U all_sum(const std::vector<T> &v, U unit = U()) {
return all_monoid_prod<U, T>(v, unit, [](U a, U b) { return a + b; });
}
template <class U, class T> U all_prod(const std::vector<T> &v, U unit = U(1)) {
return all_monoid_prod<U, T>(v, unit, [](U a, U b) { return a * b; });
}
template <class U, class T> U all_xor(const std::vector<T> &v, U unit = U()) {
return all_monoid_prod<U, T>(v, unit, [](U a, U b) { return a ^ b; });
}
template <class U, class T> U all_and(const std::vector<T> &v, U unit = U(-1)) {
return all_monoid_prod<U, T>(v, unit, [](U a, U b) { return a & b; });
}
template <class U, class T> U all_or(const std::vector<T> &v, U unit = U()) {
return all_monoid_prod<U, T>(v, unit, [](U a, U b) { return a | b; });
}
template <class U, class T> U all_min(const std::vector<T> &v) {
return all_monoid_prod<monoid::Min<U>, T>(v, monoid::Min<U>::unit(), monoid::Min<U>::op);
}
template <class U, class T> U all_max(const std::vector<T> &v) {
return all_monoid_prod<monoid::Max<U>, T>(v, monoid::Max<U>::unit(), monoid::Max<U>::op);
}
template <class U, class T> U all_gcd(const std::vector<T> &v, U unit = U()) {
return all_monoid_prod<U, T>(v, unit, [](U a, U b) { return std::gcd(a, b); });
}
template <class U, class T> U all_lcm(const std::vector<T> &v, U unit = U(1)) {
return all_monoid_prod<U, T>(v, unit, [](U a, U b) { return std::lcm(a, b); });
}
} // namespace kk2
#endif // KK2_TEMPLATE_FUNCTION_UTIL_HPP
#ifndef KK2_TEMPLATE_IO_UTIL_HPP
#define KK2_TEMPLATE_IO_UTIL_HPP 1
// なんかoj verifyはプロトタイプ宣言が落ちる
namespace impl {
struct read {
template <class IStream, class T> inline static void all_read(IStream &is, T &x) { is >> x; }
template <class IStream, class T, class U>
inline static void all_read(IStream &is, std::pair<T, U> &p) {
all_read(is, p.first);
all_read(is, p.second);
}
template <class IStream, class T> inline static void all_read(IStream &is, std::vector<T> &v) {
for (T &x : v) all_read(is, x);
}
template <class IStream, class T, size_t F>
inline static void all_read(IStream &is, std::array<T, F> &a) {
for (T &x : a) all_read(is, x);
}
};
struct write {
template <class OStream, class T> inline static void all_write(OStream &os, const T &x) {
os << x;
}
template <class OStream, class T, class U>
inline static void all_write(OStream &os, const std::pair<T, U> &p) {
all_write(os, p.first);
all_write(os, ' ');
all_write(os, p.second);
}
template <class OStream, class T>
inline static void all_write(OStream &os, const std::vector<T> &v) {
for (int i = 0; i < (int)v.size(); ++i) {
if (i) all_write(os, ' ');
all_write(os, v[i]);
}
}
template <class OStream, class T, size_t F>
inline static void all_write(OStream &os, const std::array<T, F> &a) {
for (int i = 0; i < (int)F; ++i) {
if (i) all_write(os, ' ');
all_write(os, a[i]);
}
}
};
} // namespace impl
template <class IStream, class T, class U, kk2::is_istream_t<IStream> * = nullptr>
IStream &operator>>(IStream &is, std::pair<T, U> &p) {
impl::read::all_read(is, p);
return is;
}
template <class IStream, class T, kk2::is_istream_t<IStream> * = nullptr>
IStream &operator>>(IStream &is, std::vector<T> &v) {
impl::read::all_read(is, v);
return is;
}
template <class IStream, class T, size_t F, kk2::is_istream_t<IStream> * = nullptr>
IStream &operator>>(IStream &is, std::array<T, F> &a) {
impl::read::all_read(is, a);
return is;
}
template <class OStream, class T, class U, kk2::is_ostream_t<OStream> * = nullptr>
OStream &operator<<(OStream &os, const std::pair<T, U> &p) {
impl::write::all_write(os, p);
return os;
}
template <class OStream, class T, kk2::is_ostream_t<OStream> * = nullptr>
OStream &operator<<(OStream &os, const std::vector<T> &v) {
impl::write::all_write(os, v);
return os;
}
template <class OStream, class T, size_t F, kk2::is_ostream_t<OStream> * = nullptr>
OStream &operator<<(OStream &os, const std::array<T, F> &a) {
impl::write::all_write(os, a);
return os;
}
#endif // KK2_TEMPLATE_IO_UTIL_HPP
#ifndef KK2_TEMPLATE_MACROS_HPP
#define KK2_TEMPLATE_MACROS_HPP 1
#define rep1(a) for (long long _ = 0; _ < (long long)(a); ++_)
#define rep2(i, a) for (long long i = 0; i < (long long)(a); ++i)
#define rep3(i, a, b) for (long long i = (a); i < (long long)(b); ++i)
#define repi2(i, a) for (long long i = (a) - 1; i >= 0; --i)
#define repi3(i, a, b) for (long long i = (a) - 1; i >= (long long)(b); --i)
#define overload3(a, b, c, d, ...) d
#define rep(...) overload3(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)
#define repi(...) overload3(__VA_ARGS__, repi3, repi2, rep1)(__VA_ARGS__)
#define fi first
#define se second
#define all(p) begin(p), end(p)
#endif // KK2_TEMPLATE_MACROS_HPP
struct FastIOSetUp {
FastIOSetUp() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
}
} fast_io_set_up;
auto &kin = std::cin;
auto &kout = std::cout;
auto (*kendl)(std::ostream &) = std::endl<char, std::char_traits<char>>;
void Yes(bool b = 1) { kout << (b ? "Yes\n" : "No\n"); }
void No(bool b = 1) { kout << (b ? "No\n" : "Yes\n"); }
void YES(bool b = 1) { kout << (b ? "YES\n" : "NO\n"); }
void NO(bool b = 1) { kout << (b ? "NO\n" : "YES\n"); }
void yes(bool b = 1) { kout << (b ? "yes\n" : "no\n"); }
void no(bool b = 1) { kout << (b ? "no\n" : "yes\n"); }
template <class T, class S> inline bool chmax(T &a, const S &b) { return (a < b ? a = b, 1 : 0); }
template <class T, class S> inline bool chmin(T &a, const S &b) { return (a > b ? a = b, 1 : 0); }
std::istream &operator>>(std::istream &is, u128 &x) {
std::string s;
is >> s;
x = 0;
for (char c : s) {
assert('0' <= c && c <= '9');
x = x * 10 + c - '0';
}
return is;
}
std::istream &operator>>(std::istream &is, i128 &x) {
std::string s;
is >> s;
bool neg = s[0] == '-';
x = 0;
for (int i = neg; i < (int)s.size(); i++) {
assert('0' <= s[i] && s[i] <= '9');
x = x * 10 + s[i] - '0';
}
if (neg) x = -x;
return is;
}
std::ostream &operator<<(std::ostream &os, u128 x) {
if (x == 0) return os << '0';
std::string s;
while (x) {
s.push_back('0' + x % 10);
x /= 10;
}
std::reverse(s.begin(), s.end());
return os << s;
}
std::ostream &operator<<(std::ostream &os, i128 x) {
if (x == 0) return os << '0';
if (x < 0) {
os << '-';
x = -x;
}
std::string s;
while (x) {
s.push_back('0' + x % 10);
x /= 10;
}
std::reverse(s.begin(), s.end());
return os << s;
}
#endif // KK2_TEMPLATE_PROCON_HPP
#ifndef KK2_TEMPLATE_DEBUG_HPP
#define KK2_TEMPLATE_DEBUG_HPP 1
#ifndef KK2_TYPE_TRAITS_MEMBER_HPP
#define KK2_TYPE_TRAITS_MEMBER_HPP 1
namespace kk2 {
#define HAS_MEMBER_FUNC(member) \
template <typename T, typename... Ts> struct has_member_func_##member##_impl { \
template <typename U> \
static std::true_type check(decltype(std::declval<U>().member(std::declval<Ts>()...)) *); \
template <typename U> static std::false_type check(...); \
using type = decltype(check<T>(nullptr)); \
}; \
template <typename T, typename... Ts> struct has_member_func_##member \
: has_member_func_##member##_impl<T, Ts...>::type {}; \
template <typename T, typename... Ts> using has_member_func_##member##_t = \
std::enable_if_t<has_member_func_##member<T, Ts...>::value>; \
template <typename T, typename... Ts> using not_has_member_func_##member##_t = \
std::enable_if_t<!has_member_func_##member<T, Ts...>::value>;
#define HAS_MEMBER_VAR(member) \
template <typename T> struct has_member_var_##member##_impl { \
template <typename U> static std::true_type check(decltype(std::declval<U>().member) *); \
template <typename U> static std::false_type check(...); \
using type = decltype(check<T>(nullptr)); \
}; \
template <typename T> struct has_member_var_##member \
: has_member_var_##member##_impl<T>::type {}; \
template <typename T> using has_member_var_##member##_t = \
std::enable_if_t<has_member_var_##member<T>::value>; \
template <typename T> using not_has_member_var_##member##_t = \
std::enable_if_t<!has_member_var_##member<T>::value>;
HAS_MEMBER_FUNC(debug_output)
HAS_MEMBER_FUNC(val)
#undef HAS_MEMBER_FUNC
#undef HAS_MEMBER_VAR
} // namespace kk2
#endif // KK2_TYPE_TRAITS_MEMBER_HPP
namespace kk2 {
namespace debug {
#ifdef KK2
template <class OStream, is_ostream_t<OStream> *> void output(OStream &os);
template <class OStream, class T, is_ostream_t<OStream> *> void output(OStream &os, const T &t);
template <class OStream, class T, is_ostream_t<OStream> *>
void output(OStream &os, const std::vector<T> &v);
template <class OStream, class T, is_ostream_t<OStream> *>
void output(OStream &os, const std::vector<std::vector<T>> &d);
template <class OStream, class T, size_t F, is_ostream_t<OStream> *>
void output(OStream &os, const std::array<T, F> &a);
template <class OStream, class T, class U, is_ostream_t<OStream> *>
void output(OStream &os, const std::pair<T, U> &p);
template <class OStream, class T, is_ostream_t<OStream> *>
void output(OStream &os, const std::queue<T> &q);
template <class OStream, class T, class Container, class Compare, is_ostream_t<OStream> *>
void output(OStream &os, const std::priority_queue<T, Container, Compare> &q);
template <class OStream, class T, is_ostream_t<OStream> *>
void output(OStream &os, const std::deque<T> &d);
template <class OStream, class T, is_ostream_t<OStream> *>
void output(OStream &os, const std::stack<T> &s);
template <class OStream, class Key, class Compare, class Allocator, is_ostream_t<OStream> *>
void output(OStream &os, const std::set<Key, Compare, Allocator> &s);
template <class OStream, class Key, class Compare, class Allocator, is_ostream_t<OStream> *>
void output(OStream &os, const std::multiset<Key, Compare, Allocator> &s);
template <class OStream,
class Key,
class Hash,
class KeyEqual,
class Allocator,
is_ostream_t<OStream> *>
void output(OStream &os, const std::unordered_set<Key, Hash, KeyEqual, Allocator> &s);
template <class OStream,
class Key,
class Hash,
class KeyEqual,
class Allocator,
is_ostream_t<OStream> *>
void output(OStream &os, const std::unordered_multiset<Key, Hash, KeyEqual, Allocator> &s);
template <class OStream,
class Key,
class T,
class Compare,
class Allocator,
is_ostream_t<OStream> *>
void output(OStream &os, const std::map<Key, T, Compare, Allocator> &m);
template <class OStream,
class Key,
class T,
class Hash,
class KeyEqual,
class Allocator,
is_ostream_t<OStream> *>
void output(OStream &os, const std::unordered_map<Key, T, Hash, KeyEqual, Allocator> &m);
template <class OStream, is_ostream_t<OStream> * = nullptr> void output(OStream &) {}
template <class OStream, class T, is_ostream_t<OStream> * = nullptr>
void output(OStream &os, const T &t) {
if constexpr (has_member_func_debug_output<T, OStream &>::value) {
t.debug_output(os);
} else {
os << t;
}
}
template <class OStream, class T, is_ostream_t<OStream> * = nullptr>
void output(OStream &os, const std::vector<T> &v) {
os << "[";
for (int i = 0; i < (int)v.size(); i++) {
output(os, v[i]);
if (i + 1 != (int)v.size()) os << ", ";
}
os << "]";
}
template <class OStream, class T, is_ostream_t<OStream> * = nullptr>
void output(OStream &os, const std::vector<std::vector<T>> &d) {
os << "[\n";
for (int i = 0; i < (int)d.size(); i++) {
output(os, d[i]);
output(os, "\n");
}
os << "]";
}
template <class OStream, class T, size_t F, is_ostream_t<OStream> * = nullptr>
void output(OStream &os, const std::array<T, F> &a) {
os << "[";
for (int i = 0; i < (int)F; i++) {
output(os, a[i]);
if (i + 1 != (int)F) os << ", ";
}
os << "]";
}
template <class OStream, class T, class U, is_ostream_t<OStream> * = nullptr>
void output(OStream &os, const std::pair<T, U> &p) {
os << "(";
output(os, p.first);
os << ", ";
output(os, p.second);
os << ")";
}
template <class OStream, class T, is_ostream_t<OStream> * = nullptr>
void output(OStream &os, const std::queue<T> &q) {
os << "[";
std::queue<T> tmp = q;
while (!tmp.empty()) {
output(os, tmp.front());
tmp.pop();
if (!tmp.empty()) os << ", ";
}
os << "]";
}
template <class OStream, class T, class Container, class Compare, is_ostream_t<OStream> * = nullptr>
void output(OStream &os, const std::priority_queue<T, Container, Compare> &q) {
os << "[";
std::priority_queue<T, Container, Compare> tmp = q;
while (!tmp.empty()) {
output(os, tmp.top());
tmp.pop();
if (!tmp.empty()) os << ", ";
}
os << "]";
}
template <class OStream, class T, is_ostream_t<OStream> * = nullptr>
void output(OStream &os, const std::deque<T> &d) {
os << "[";
std::deque<T> tmp = d;
while (!tmp.empty()) {
output(os, tmp.front());
tmp.pop_front();
if (!tmp.empty()) os << ", ";
}
os << "]";
}
template <class OStream, class T, is_ostream_t<OStream> * = nullptr>
void output(OStream &os, const std::stack<T> &s) {
os << "[";
std::stack<T> tmp = s;
std::vector<T> v;
while (!tmp.empty()) {
v.push_back(tmp.top());
tmp.pop();
}
for (int i = (int)v.size() - 1; i >= 0; i--) {
output(os, v[i]);
if (i != 0) os << ", ";
}
os << "]";
}
template <class OStream,
class Key,
class Compare,
class Allocator,
is_ostream_t<OStream> * = nullptr>
void output(OStream &os, const std::set<Key, Compare, Allocator> &s) {
os << "{";
std::set<Key, Compare, Allocator> tmp = s;
for (auto it = tmp.begin(); it != tmp.end(); ++it) {
output(os, *it);
if (std::next(it) != tmp.end()) os << ", ";
}
os << "}";
}
template <class OStream,
class Key,
class Compare,
class Allocator,
is_ostream_t<OStream> * = nullptr>
void output(OStream &os, const std::multiset<Key, Compare, Allocator> &s) {
os << "{";
std::multiset<Key, Compare, Allocator> tmp = s;
for (auto it = tmp.begin(); it != tmp.end(); ++it) {
output(os, *it);
if (std::next(it) != tmp.end()) os << ", ";
}
os << "}";
}
template <class OStream,
class Key,
class Hash,
class KeyEqual,
class Allocator,
is_ostream_t<OStream> * = nullptr>
void output(OStream &os, const std::unordered_set<Key, Hash, KeyEqual, Allocator> &s) {
os << "{";
std::unordered_set<Key, Hash, KeyEqual, Allocator> tmp = s;
for (auto it = tmp.begin(); it != tmp.end(); ++it) {
output(os, *it);
if (std::next(it) != tmp.end()) os << ", ";
}
os << "}";
}
template <class OStream,
class Key,
class Hash,
class KeyEqual,
class Allocator,
is_ostream_t<OStream> * = nullptr>
void output(OStream &os, const std::unordered_multiset<Key, Hash, KeyEqual, Allocator> &s) {
os << "{";
std::unordered_multiset<Key, Hash, KeyEqual, Allocator> tmp = s;
for (auto it = tmp.begin(); it != tmp.end(); ++it) {
output(os, *it);
if (std::next(it) != tmp.end()) os << ", ";
}
os << "}";
}
template <class OStream,
class Key,
class T,
class Compare,
class Allocator,
is_ostream_t<OStream> * = nullptr>
void output(OStream &os, const std::map<Key, T, Compare, Allocator> &m) {
os << "{";
std::map<Key, T, Compare, Allocator> tmp = m;
for (auto it = tmp.begin(); it != tmp.end(); ++it) {
output(os, it->first);
os << ": ";
output(os, it->second);
if (std::next(it) != tmp.end()) os << ", ";
}
os << "}";
}
template <class OStream,
class Key,
class T,
class Hash,
class KeyEqual,
class Allocator,
is_ostream_t<OStream> * = nullptr>
void output(OStream &os, const std::unordered_map<Key, T, Hash, KeyEqual, Allocator> &m) {
os << "{";
std::unordered_map<Key, T, Hash, KeyEqual, Allocator> tmp = m;
for (auto it = tmp.begin(); it != tmp.end(); ++it) {
output(os, it->first);
os << ": ";
output(os, it->second);
if (std::next(it) != tmp.end()) os << ", ";
}
os << "}";
}
template <class OStream, class T, class... Args, is_ostream_t<OStream> * = nullptr>
void output(OStream &os, const T &t, const Args &...args) {
output(os, t);
os << ' ';
output(os, args...);
}
template <class OStream, is_ostream_t<OStream> * = nullptr> void outputln(OStream &os) {
os << '\n';
os.flush();
}
template <class OStream, class T, class... Args, is_ostream_t<OStream> * = nullptr>
void outputln(OStream &os, const T &t, const Args &...args) {
output(os, t, args...);
os << '\n';
os.flush();
}
std::vector<std::string> sep(const char *s) {
std::vector<std::string> res;
std::string now;
int dep = 0;
while (true) {
if (*s == '\0') {
res.emplace_back(now);
break;
}
if (*s == '(' or *s == '[' or *s == '{') dep++;
if (*s == ')' or *s == ']' or *s == '}') dep--;
if (dep == 0 and *s == ',') {
res.emplace_back(now);
now.clear();
} else if (!isspace(*s)) {
now += *s;
}
s++;
}
return res;
}
void show_vars(const std::vector<std::string> &, int) {}
template <class T, class... Args>
void show_vars(const std::vector<std::string> &name, int pos, const T &t, const Args &...args) {
// assert(pos < (int)name.size());
output(std::cerr, name[pos++] + ":", t);
if (sizeof...(args) > 0) output(std::cerr, ", ");
show_vars(name, pos, args...);
}
#define kdebug(...) \
std::cerr << "line:" << __LINE__ << ' '; \
kk2::debug::show_vars(kk2::debug::sep(#__VA_ARGS__), 0, __VA_ARGS__); \
std::cerr << std::endl;
#define kput(s) \
std::cerr << "line:" << __LINE__ << ' '; \
kk2::debug::outputln(std::cerr, s);
#else
template <class OStream, class... Args, is_ostream_t<OStream> * = nullptr>
void output(OStream &, const Args &...) {}
template <class OStream, class... Args, is_ostream_t<OStream> * = nullptr>
void outputln(OStream &, const Args &...) {}
template <class... Args> void fix_warn(const Args &...) {}
#define kdebug(...) kk2::debug::fix_warn(__VA_ARGS__);
#define kput(s) kk2::debug::fix_warn(s)
#endif // KK2
} // namespace debug
} // namespace kk2
#endif // KK2_TEMPLATE_DEBUG_HPP
#ifndef KK2_MODINT_MONT_HPP
#define KK2_MODINT_MONT_HPP 1
#ifndef KK2_TYPE_TRAITS_INTERGRAL_HPP
#define KK2_TYPE_TRAITS_INTERGRAL_HPP 1
namespace kk2 {
#ifndef _MSC_VER
template <typename T> using is_signed_int128 =
typename std::conditional<std::is_same<T, __int128_t>::value
or std::is_same<T, __int128>::value,
std::true_type,
std::false_type>::type;
template <typename T> using is_unsigned_int128 =
typename std::conditional<std::is_same<T, __uint128_t>::value
or std::is_same<T, unsigned __int128>::value,
std::true_type,
std::false_type>::type;
template <typename T> using is_integral =
typename std::conditional<std::is_integral<T>::value or is_signed_int128<T>::value
or is_unsigned_int128<T>::value,
std::true_type,
std::false_type>::type;
template <typename T> using is_signed =
typename std::conditional<std::is_signed<T>::value or is_signed_int128<T>::value,
std::true_type,
std::false_type>::type;
template <typename T> using is_unsigned =
typename std::conditional<std::is_unsigned<T>::value or is_unsigned_int128<T>::value,
std::true_type,
std::false_type>::type;
template <typename T> using make_unsigned_int128 =
typename std::conditional<std::is_same<T, __int128_t>::value, __uint128_t, unsigned __int128>;
template <typename T> using to_unsigned =
typename std::conditional<is_signed_int128<T>::value,
make_unsigned_int128<T>,
typename std::conditional<std::is_signed<T>::value,
std::make_unsigned<T>,
std::common_type<T>>::type>::type;
#else
template <typename T> using is_integral = std::enable_if_t<std::is_integral<T>::value>;
template <typename T> using is_signed = std::enable_if_t<std::is_signed<T>::value>;
template <typename T> using is_unsigned = std::enable_if_t<std::is_unsigned<T>::value>;
template <typename T> using to_unsigned = std::make_unsigned<T>;
#endif // _MSC_VER
template <typename T> using is_integral_t = std::enable_if_t<is_integral<T>::value>;
template <typename T> using is_signed_t = std::enable_if_t<is_signed<T>::value>;
template <typename T> using is_unsigned_t = std::enable_if_t<is_unsigned<T>::value>;
} // namespace kk2
#endif // KK2_TYPE_TRAITS_INTERGRAL_HPP
namespace kk2 {
template <int p> struct LazyMontgomeryModInt {
using mint = LazyMontgomeryModInt;
using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;
static constexpr u32 get_r() {
u32 ret = p;
for (int i = 0; i < 4; ++i) ret *= 2 - p * ret;
return ret;
}
static constexpr u32 r = get_r();
static constexpr u32 n2 = -u64(p) % p;
static_assert(r * p == 1, "invalid, r * p != 1");
static_assert(p < (1 << 30), "invalid, p >= 2 ^ 30");
static_assert((p & 1) == 1, "invalid, p % 2 == 0");
u32 _v;
constexpr LazyMontgomeryModInt() : _v(0) {}
template <typename T, is_integral_t<T> * = nullptr> constexpr LazyMontgomeryModInt(T b)
: _v(reduce(u64(b % p + p) * n2)) {}
static constexpr u32 reduce(const u64 &b) { return (b + u64(u32(b) * u32(-r)) * p) >> 32; }
constexpr mint &operator++() { return *this += 1; }
constexpr mint &operator--() { return *this -= 1; }
constexpr mint operator++(int) {
mint ret = *this;
*this += 1;
return ret;
}
constexpr mint operator--(int) {
mint ret = *this;
*this -= 1;
return ret;
}
constexpr mint &operator+=(const mint &b) {
if (i32(_v += b._v - 2 * p) < 0) _v += 2 * p;
return *this;
}
constexpr mint &operator-=(const mint &b) {
if (i32(_v -= b._v) < 0) _v += 2 * p;
return *this;
}
constexpr mint &operator*=(const mint &b) {
_v = reduce(u64(_v) * b._v);
return *this;
}
constexpr mint &operator/=(const mint &b) {
*this *= b.inv();
return *this;
}
constexpr bool operator==(const mint &b) const {
return (_v >= p ? _v - p : _v) == (b._v >= p ? b._v - p : b._v);
}
constexpr bool operator!=(const mint &b) const {
return (_v >= p ? _v - p : _v) != (b._v >= p ? b._v - p : b._v);
}
constexpr mint operator-() const { return mint() - mint(*this); }
constexpr mint operator+() const { return mint(*this); }
friend constexpr mint operator+(const mint &a, const mint &b) { return mint(a) += b; }
friend constexpr mint operator-(const mint &a, const mint &b) { return mint(a) -= b; }
friend constexpr mint operator*(const mint &a, const mint &b) { return mint(a) *= b; }
friend constexpr mint operator/(const mint &a, const mint &b) { return mint(a) /= b; }
template <class T> constexpr mint pow(T n) const {
mint ret(1), mul(*this);
while (n > 0) {
if (n & 1) ret *= mul;
if (n >>= 1) mul *= mul;
}
return ret;
}
constexpr mint inv() const {
assert(*this != mint(0));
return pow(p - 2);
}
template <class OStream, is_ostream_t<OStream> * = nullptr>
friend OStream &operator<<(OStream &os, const mint &x) {
return os << x.val();
}
template <class IStream, is_istream_t<IStream> * = nullptr>
friend IStream &operator>>(IStream &is, mint &x) {
i64 t;
is >> t;
x = mint(t);
return (is);
}
constexpr u32 val() const {
u32 ret = reduce(_v);
return ret >= p ? ret - p : ret;
}
static constexpr u32 getmod() { return p; }
};
template <int p> using Mont = LazyMontgomeryModInt<p>;
using mont998 = Mont<998244353>;
using mont107 = Mont<1000000007>;
} // namespace kk2
#endif // KK2_MODINT_MONT_HPP
#ifndef KK2_FPS_FPS_NTT_FRIENDLY_HPP
#define KK2_FPS_FPS_NTT_FRIENDLY_HPP 1
#ifndef KK2_CONVOLUTION_CONVOLUTION_HPP
#define KK2_CONVOLUTION_CONVOLUTION_HPP 1
#ifndef KK2_FPS_FPS_SPARSITY_DETECTOR_HPP
#define KK2_FPS_FPS_SPARSITY_DETECTOR_HPP 1
#ifndef KK2_BIT_BITCOUNT_HPP
#define KK2_BIT_BITCOUNT_HPP 1
namespace kk2 {
template <typename T> constexpr int ctz(T x) {
static_assert(is_integral<T>::value);
assert(x != T(0));
if constexpr (sizeof(T) <= 4) {
return __builtin_ctz(x);
} else if constexpr (sizeof(T) <= 8) {
return __builtin_ctzll(x);
} else {
if (x & 0xffffffffffffffff)
return __builtin_ctzll((unsigned long long)(x & 0xffffffffffffffff));
return 64 + __builtin_ctzll((unsigned long long)(x >> 64));
}
}
template <typename T> constexpr int lsb(T x) {
static_assert(is_integral<T>::value);
assert(x != T(0));
return ctz(x);
}
template <typename T> constexpr int clz(T x) {
static_assert(is_integral<T>::value);
assert(x != T(0));
if constexpr (sizeof(T) <= 4) {
return __builtin_clz(x);
} else if constexpr (sizeof(T) <= 8) {
return __builtin_clzll(x);
} else {
if (x >> 64) return __builtin_clzll((unsigned long long)(x >> 64));
return 64 + __builtin_clzll((unsigned long long)(x & 0xffffffffffffffff));
}
}
template <typename T> constexpr int msb(T x) {
static_assert(is_integral<T>::value);
assert(x != T(0));
return sizeof(T) * 8 - 1 - clz(x);
}
template <typename T> constexpr int popcount(T x) {
static_assert(is_integral<T>::value);
if constexpr (sizeof(T) <= 4) {
return __builtin_popcount(x);
} else if constexpr (sizeof(T) <= 8) {
return __builtin_popcountll(x);
} else {
return __builtin_popcountll((unsigned long long)(x >> 64))
+ __builtin_popcountll((unsigned long long)(x & 0xffffffffffffffff));
}
}
}; // namespace kk2
#endif // KK2_BIT_BITCOUNT_HPP
namespace kk2 {
enum class FPSOperation { CONVOLUTION, EXP };
template <class FPS, class mint = typename FPS::value_type> bool
is_sparse_operation(FPSOperation op, bool is_ntt_friendly, const FPS &a, const FPS &b = FPS()) {
int n = a.size(), m = b.size();
long long not_zero_a = 0, not_zero_b = 0;
bool same = a == b;
int lg = msb(n + m) + 1;
for (int i = 0; i < n; i++) not_zero_a += a[i] != mint(0);
for (int i = 0; i < m; i++) not_zero_b += b[i] != mint(0);
if (op == FPSOperation::CONVOLUTION) {
return (n + m) * lg * (is_ntt_friendly ? 3.42 : 20.0) * (same ? 0.5 : 1)
> double(not_zero_a) * not_zero_b;
}
if (op == FPSOperation::EXP) {
return n * lg * (is_ntt_friendly ? 8.2 : 60.0) > double(n) * not_zero_a;
}
return false;
}
} // namespace kk2
#endif // KK2_FPS_FPS_SPARSITY_DETECTOR_HPP
#ifndef KK2_MATH_MOD_BUTTERFLY_HPP
#define KK2_MATH_MOD_BUTTERFLY_HPP 1
#ifndef KK2_MATH_MOD_PRIMITIVE_ROOT_HPP
#define KK2_MATH_MOD_PRIMITIVE_ROOT_HPP 1
#ifndef KK2_MATH_MOD_POW_MOD_HPP
#define KK2_MATH_MOD_POW_MOD_HPP 1
namespace kk2 {
template <class S, class T, class U> constexpr S pow_mod(T x, U n, T m) {
assert(n >= 0);
if (m == 1) return S(0);
S _m = m, r = 1;
S y = x % _m;
if (y < 0) y += _m;
while (n) {
if (n & 1) r = (r * y) % _m;
if (n >>= 1) y = (y * y) % _m;
}
return r;
}
} // namespace kk2
#endif // KK2_MATH_MOD_POW_MOD_HPP
namespace kk2 {
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;
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 (pow_mod<long long>(g, (m - 1) / divs[i], m) == 1) {
ok = false;
break;
}
}
if (ok) return g;
}
}
template <int m> static constexpr int primitive_root = primitive_root_constexpr(m);
} // namespace kk2
#endif // KK2_MATH_MOD_PRIMITIVE_ROOT_HPP
namespace kk2 {
template <class FPS, class mint = typename FPS::value_type> void butterfly(FPS &a) {
static int g = primitive_root<mint::getmod()>;
int n = int(a.size());
int h = 0;
while ((1U << h) < (unsigned int)(n)) h++;
static bool first = true;
static mint sum_e2[30]; // sum_e[i] = ies[0] * ... * ies[i - 1] * es[i]
static mint sum_e3[30];
static mint es[30], ies[30]; // es[i]^(2^(2+i)) == 1
if (first) {
first = false;
int cnt2 = __builtin_ctz(mint::getmod() - 1);
mint e = mint(g).pow((mint::getmod() - 1) >> cnt2), ie = e.inv();
for (int i = cnt2; i >= 2; i--) {
// e^(2^i) == 1
es[i - 2] = e;
ies[i - 2] = ie;
e *= e;
ie *= ie;
}
mint now = 1;
for (int i = 0; i <= cnt2 - 2; i++) {
sum_e2[i] = es[i] * now;
now *= ies[i];
}
now = 1;
for (int i = 0; i <= cnt2 - 3; i++) {
sum_e3[i] = es[i + 1] * now;
now *= ies[i + 1];
}
}
int len = 0;
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 *= sum_e2[__builtin_ctz(~(unsigned int)(s))];
}
len++;
} else {
int p = 1 << (h - len - 2);
mint rot = 1, imag = es[0];
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 a0 = a[i + offset];
auto a1 = a[i + offset + p] * rot;
auto a2 = a[i + offset + p * 2] * rot2;
auto a3 = a[i + offset + p * 3] * rot3;
auto a1na3imag = (a1 - a3) * imag;
a[i + offset] = a0 + a2 + a1 + a3;
a[i + offset + p] = a0 + a2 - a1 - a3;
a[i + offset + p * 2] = a0 - a2 + a1na3imag;
a[i + offset + p * 3] = a0 - a2 - a1na3imag;
}
if (s + 1 != (1 << len)) rot *= sum_e3[__builtin_ctz(~(unsigned int)(s))];
}
len += 2;
}
}
}
template <class FPS, class mint = typename FPS::value_type> void butterfly_inv(FPS &a) {
static constexpr int g = primitive_root<mint::getmod()>;
int n = int(a.size());
int h = 0;
while ((1U << h) < (unsigned int)(n)) h++;
static bool first = true;
static mint sum_ie2[30]; // sum_ie[i] = es[0] * ... * es[i - 1] * ies[i]
static mint sum_ie3[30];
static mint es[30], ies[30]; // es[i]^(2^(2+i)) == 1
static mint invn[30];
if (first) {
first = false;
int cnt2 = __builtin_ctz(mint::getmod() - 1);
mint e = mint(g).pow((mint::getmod() - 1) >> cnt2), ie = e.inv();
for (int i = cnt2; i >= 2; i--) {
// e^(2^i) == 1
es[i - 2] = e;
ies[i - 2] = ie;
e *= e;
ie *= ie;
}
mint now = 1;
for (int i = 0; i <= cnt2 - 2; i++) {
sum_ie2[i] = ies[i] * now;
now *= es[i];
}
now = 1;
for (int i = 0; i <= cnt2 - 3; i++) {
sum_ie3[i] = ies[i + 1] * now;
now *= es[i + 1];
}
invn[0] = 1;
invn[1] = mint::getmod() / 2 + 1;
for (int i = 2; i < 30; i++) invn[i] = invn[i - 1] * invn[1];
}
int len = h;
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] = (l - r) * irot;
}
if (s + 1 != (1 << (len - 1))) irot *= sum_ie2[__builtin_ctz(~(unsigned int)(s))];
}
len--;
} else {
int p = 1 << (h - len);
mint irot = 1, iimag = ies[0];
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 = a[i + offset];
auto a1 = a[i + offset + p];
auto a2 = a[i + offset + p * 2];
auto a3 = a[i + offset + p * 3];
auto a2na3iimag = (a2 - a3) * iimag;
a[i + offset] = a0 + a1 + a2 + a3;
a[i + offset + p] = (a0 - a1 + a2na3iimag) * irot;
a[i + offset + p * 2] = (a0 + a1 - a2 - a3) * irot2;
a[i + offset + p * 3] = (a0 - a1 - a2na3iimag) * irot3;
}
if (s + 1 != (1 << (len - 2))) irot *= sum_ie3[__builtin_ctz(~(unsigned int)(s))];
}
len -= 2;
}
}
for (int i = 0; i < n; i++) a[i] *= invn[h];
}
template <class FPS, class mint = typename FPS::value_type> void doubling(FPS &a) {
int n = a.size();
auto b = a;
int z = 1;
butterfly_inv(b);
mint r = 1, zeta = mint(primitive_root<mint::getmod()>).pow((mint::getmod() - 1) / (n << 1));
for (int i = 0; i < n; i++) {
b[i] *= r;
r *= zeta;
}
butterfly(b);
std::copy(b.begin(), b.end(), std::back_inserter(a));
}
} // namespace kk2
#endif // KK2_MATH_MOD_BUTTERFLY_HPP
namespace kk2 {
template <class FPS, class mint = typename FPS::value_type> FPS convolution(FPS &a, const FPS &b) {
int n = int(a.size()), m = int(b.size());
if (!n || !m) {
a.clear();
return a;
}
if (is_sparse_operation(FPSOperation::CONVOLUTION, 1, a, b)) {
std::vector<int> nza(n), nzb(m);
int ai = 0, bi = 0;
for (int i = 0; i < n; i++)
if (a[i] != mint(0)) nza[ai++] = i;
for (int i = 0; i < m; i++)
if (b[i] != mint(0)) nzb[bi++] = i;
nza.resize(ai), nzb.resize(bi);
FPS res(n + m - 1);
for (int i : nza)
for (int j : nzb) res[i + j] += a[i] * b[j];
return a = res;
}
int z = 1;
while (z < n + m - 1) z <<= 1;
if (a == b) {
a.resize(z);
butterfly(a);
for (int i = 0; i < z; i++) a[i] *= a[i];
} else {
a.resize(z);
butterfly(a);
FPS t(b.begin(), b.end());
t.resize(z);
butterfly(t);
for (int i = 0; i < z; i++) a[i] *= t[i];
}
butterfly_inv(a);
a.resize(n + m - 1);
return a;
}
} // namespace kk2
#endif // KK2_CONVOLUTION_CONVOLUTION_HPP
#ifndef KK2_FPS_FPS_BASE_HPP
#define KK2_FPS_FPS_BASE_HPP 1
#ifndef KK2_MATH_MOD_INV_TABLE_HPP
#define KK2_MATH_MOD_INV_TABLE_HPP 1
namespace kk2 {
/**
* @brief `[1, n]`のmod逆元を列挙するテーブル
*
* @tparam mint
*/
template <class mint> struct InvTable {
static inline std::vector<mint> _invs{0, 1};
static inline auto _mod = mint::getmod();
InvTable() = delete;
static void set_upper(int m) {
if ((int)_invs.size() > m) return;
int start = _invs.size();
_invs.resize(m + 1);
// p = q * i + r
// - q / r = 1 / i (mod p)
for (int i = start; i <= m; ++i) _invs[i] = (-_invs[_mod % i]) * (_mod / i);
}
static inline mint inv(int n) {
bool neg = n < 0;
if (neg) n = -n;
if (n >= (int)_invs.size()) set_upper(n);
return neg ? -_invs[n] : _invs[n];
}
};
} // namespace kk2
#endif // KK2_MATH_MOD_INV_TABLE_HPP
namespace kk2 {
template <class Derived, class mint> struct FormalPowerSeriesBase : std::vector<mint> {
using std::vector<mint>::vector;
using FPS = Derived;
using ivta = InvTable<mint>;
// CRTPを使って派生クラスの参照を取得
Derived &derived() { return static_cast<Derived &>(*this); }
const Derived &derived() const { return static_cast<const Derived &>(*this); }
template <class OStream, is_ostream_t<OStream> * = nullptr>
void debug_output(OStream &os) const {
os << "[";
for (size_t i = 0; i < this->size(); i++) {
os << (*this)[i] << (i + 1 == this->size() ? "" : ", ");
}
os << "]";
}
template <class OStream, is_ostream_t<OStream> * = nullptr> void output(OStream &os) const {
for (size_t i = 0; i < this->size(); i++) {
os << (*this)[i] << (i + 1 == this->size() ? "\n" : " ");
}
}
template <class OStream, is_ostream_t<OStream> * = nullptr>
friend OStream &operator<<(OStream &os, const FPS &fps_) {
for (size_t i = 0; i < fps_.size(); i++) {
os << fps_[i] << (i + 1 == fps_.size() ? "" : " ");
}
return os;
}
template <class IStream, is_istream_t<IStream> * = nullptr> FPS &input(IStream &is) {
for (size_t i = 0; i < this->size(); i++) is >> (*this)[i];
return derived();
}
template <class IStream, is_istream_t<IStream> * = nullptr>
friend IStream &operator>>(IStream &is, FPS &fps_) {
for (auto &x : fps_) is >> x;
return is;
}
FPS &operator+=(const FPS &r) {
if (this->size() < r.size()) this->resize(r.size());
for (size_t i = 0; i < r.size(); i++) (*this)[i] += r[i];
return derived();
}
FPS &operator+=(const mint &r) {
if (this->empty()) this->resize(1);
(*this)[0] += r;
return derived();
}
FPS &operator-=(const FPS &r) {
if (this->size() < r.size()) this->resize(r.size());
for (size_t i = 0; i < r.size(); i++) (*this)[i] -= r[i];
return derived();
}
FPS &operator-=(const mint &r) {
if (this->empty()) this->resize(1);
(*this)[0] -= r;
return derived();
}
FPS &operator*=(const mint &r) {
for (size_t i = 0; i < this->size(); i++) { (*this)[i] *= r; }
return derived();
}
FPS &operator/=(const FPS &r) {
assert(!r.empty());
if (this->size() < r.size()) {
this->clear();
return derived();
}
int n = this->size() - r.size() + 1;
if (r.size() <= 64) {
FPS f(derived()), g(r);
g.shrink();
mint coeff = g.back().inv();
for (auto &x : g) x *= coeff;
int deg = (int)f.size() - (int)g.size() + 1;
int gs = g.size();
FPS quo(deg);
for (int i = deg - 1; i >= 0; i--) {
quo[i] = f[i + gs - 1];
for (int j = 0; j < gs; j++) f[i + j] -= quo[i] * g[j];
}
*this = quo * coeff;
this->resize(n, mint(0));
return derived();
}
return derived() = (derived().rev().pre(n) * r.rev().inv(n)).pre(n).rev();
}
FPS &operator%=(const FPS &r) {
derived() -= derived() / r * r;
shrink();
return derived();
}
FPS &operator>>=(int n) {
if (n >= (int)this->size()) {
this->clear();
} else {
this->erase(this->begin(), this->begin() + n);
}
return derived();
}
FPS &operator<<=(int n) {
this->insert(this->begin(), n, mint(0));
return derived();
}
// CRTPを使って派生クラスのメソッドを利用した演算子の自動実装
FPS operator+(const FPS &r) const { return FPS(derived()) += r; }
FPS operator+(const mint &r) const { return FPS(derived()) += r; }
FPS operator-(const FPS &r) const { return FPS(derived()) -= r; }
FPS operator-(const mint &r) const { return FPS(derived()) -= r; }
// 掛け算は派生クラスで定義される
FPS operator*(const FPS &r) const { return FPS(derived()) *= r; }
FPS operator*(const mint &r) const { return FPS(derived()) *= r; }
FPS operator/(const FPS &r) const { return FPS(derived()) /= r; }
FPS operator%(const FPS &r) const { return FPS(derived()) %= r; }
FPS operator>>(int n) const { return FPS(derived()) >>= n; }
FPS operator<<(int n) const { return FPS(derived()) <<= n; }
FPS operator-() const {
FPS ret(this->size());
for (size_t i = 0; i < this->size(); i++) ret[i] = -(*this)[i];
return ret;
}
FPS &shrink() {
while (this->size() && this->back() == mint(0)) this->pop_back();
return derived();
}
FPS &inplace_rev() {
std::reverse(this->begin(), this->end());
return derived();
}
FPS &inplace_dot(const FPS &r) {
this->resize(std::min(this->size(), r.size()));
for (size_t i = 0; i < this->size(); i++) (*this)[i] *= r[i];
return derived();
}
FPS &inplace_pre(int n) {
this->resize(n);
return derived();
}
FPS &inplace_diff() {
if (this->empty()) return derived();
this->erase(this->begin());
for (size_t i = 1; i <= this->size(); i++) (*this)[i - 1] *= mint(i);
return derived();
}
FPS &inplace_int() {
ivta::set_upper(this->size());
this->insert(this->begin(), mint(0));
for (size_t i = 1; i < this->size(); i++) (*this)[i] *= ivta::inv(i);
return derived();
}
// CRTPを使った便利関数の自動実装
FPS rev() const { return FPS(derived()).inplace_rev(); }
FPS dot(const FPS &r) const { return FPS(derived()).inplace_dot(r); }
FPS pre(int n) const { return FPS(derived()).inplace_pre(n); }
FPS diff() const { return FPS(derived()).inplace_diff(); }
FPS integral() const { return FPS(derived()).inplace_int(); }
mint eval(mint x) const {
mint r = 0, w = 1;
for (auto &v : *this) {
r += w * v;
w *= x;
}
return r;
}
FPS log(int deg = -1) const {
assert(!this->empty() && (*this)[0] == mint(1));
// sparsity check
return derived().dense_log(deg);
}
template <class T> FPS pow(T k, int deg = -1) const {
// sparsity check
return derived().dense_pow(k, deg);
}
FPS div(const FPS &r, int deg = -1) const {
// sparsity check
return (FPS(derived()).pre(deg) * r.inv(deg)).pre(deg);
}
FPS inv(int deg = -1) const {
assert(!this->empty() && (*this)[0] != mint(0));
// sparsity check
return derived().dense_inv(deg);
}
FPS exp(int deg = -1) const {
assert(this->empty() || (*this)[0] == mint(0));
// sparsity check
return derived().dense_exp(deg);
}
FPS dense_log(int deg = -1) const {
if (deg == -1) deg = this->size();
return (derived().diff() * derived().inv(deg)).pre(deg - 1).integral();
}
FPS sparse_log(int deg = -1) const {
if (deg == -1) deg = this->size();
std::vector<std::pair<int, mint>> fs;
for (int i = 1; i < int(this->size()); i++) {
if ((*this)[i] != mint(0)) fs.emplace_back(i, (*this)[i]);
}
ivta::set_upper(deg);
FPS g(deg);
for (int k = 0; k < deg - 1; k++) {
for (auto &[j, fj] : fs) {
if (k < j) break;
int i = k - j;
g[k + 1] -= g[i + 1] * fj * (i + 1);
}
g[k + 1] *= ivta::inv(k + 1);
if (k + 1 < int(this->size())) g[k + 1] += (*this)[k + 1];
}
return g;
}
template <class T> FPS dense_pow(T k, int deg = -1) const {
const int n = this->size();
if (deg == -1) deg = n;
if (k == 0) {
FPS ret(deg);
if (deg > 0) ret[0] = mint(1);
return ret;
}
for (int i = 0; i < n; i++) {
if ((*this)[i] != mint(0)) {
mint rev = mint(1) / (*this)[i];
FPS ret = ((derived() * rev) >> i).log(deg) * k;
ret = ret.exp(deg);
ret *= (*this)[i].pow(k);
ret = (ret << (i * k)).pre(deg);
if ((int)ret.size() < deg) ret.resize(deg, mint(0));
return ret;
}
if (__int128_t(i + 1) * k >= deg) return FPS(deg, mint(0));
}
return FPS(deg, mint(0));
}
template <class T> FPS sparse_pow(T k, int deg = -1) const {
if (deg == -1) deg = this->size();
if (k == 0) {
FPS ret(deg);
if (deg > 0) ret[0] = mint(1);
return ret;
}
int zero = 0;
while (zero != int(this->size()) && (*this)[zero] == mint(0)) zero++;
if (zero == int(this->size()) || __int128_t(zero) * k >= deg) { return FPS(deg, mint(0)); }
if (zero != 0) {
FPS suf(this->begin() + zero, this->end());
auto g = suf.sparse_pow(k, deg - zero * k);
FPS ret(zero * k, mint(0));
std::copy(std::begin(g), std::end(g), std::back_inserter(ret));
return ret;
}
int mod = mint::getmod();
static std::vector<mint> inv{1, 1};
while ((int)inv.size() <= deg) {
int i = inv.size();
inv.push_back(-inv[mod % i] * (mod / i));
}
std::vector<std::pair<int, mint>> fs;
for (int i = 1; i < int(this->size()); i++) {
if ((*this)[i] != mint(0)) fs.emplace_back(i, (*this)[i]);
}
FPS g(deg);
g[0] = (*this)[0].pow(k);
mint denom = (*this)[0].inv();
k %= mod;
for (int a = 1; a < deg; a++) {
for (auto &[i, f_i] : fs) {
if (a < i) break;
g[a] += g[a - i] * f_i * (mint(i) * (k + 1) - a);
}
g[a] *= denom * inv[a];
}
return g;
} // return this / r
FPS sparse_div(const FPS &r, int deg = -1) const {
assert(!r.empty() && r[0] != mint(0));
if (deg == -1) deg = this->size();
mint ir0 = r[0].inv();
FPS ret = derived() * ir0;
ret.resize(deg);
std::vector<std::pair<int, mint>> gs;
for (int i = 1; i < (int)r.size(); i++) {
if (r[i] != mint(0)) gs.emplace_back(i, r[i] * ir0);
}
for (int i = 0; i < deg; i++) {
for (auto &[j, g_j] : gs) {
if (i + j >= deg) break;
ret[i + j] -= ret[i] * g_j;
}
}
return ret;
}
FPS sparse_inv(int deg = -1) const {
if (deg == -1) deg = this->size();
std::vector<std::pair<int, mint>> fs;
for (int i = 1; i < int(this->size()); i++) {
if ((*this)[i] != mint(0)) fs.emplace_back(i, (*this)[i]);
}
FPS ret(deg);
mint if0 = (*this)[0].inv();
if (0 < deg) ret[0] = if0;
for (int k = 1; k < deg; k++) {
for (auto &[j, fj] : fs) {
if (k < j) break;
ret[k] += ret[k - j] * fj;
}
ret[k] *= -if0;
}
return ret;
}
FPS sparse_exp(int deg = -1) const {
if (deg == -1) deg = this->size();
std::vector<std::pair<int, mint>> fs;
for (int i = 1; i < int(this->size()); i++) {
if ((*this)[i] != mint(0)) fs.emplace_back(i, (*this)[i]);
}
int mod = mint::getmod();
static std::vector<mint> inv{1, 1};
int now = inv.size();
inv.resize(std::max(now, deg + 1));
for (int i = now; i <= deg; i++) inv[i] = -inv[mod % i] * (mod / i);
FPS g(deg);
if (deg) g[0] = 1;
for (int k = 0; k < deg - 1; k++) {
for (auto &[ip1, fip1] : fs) {
int i = ip1 - 1;
if (k < i) break;
g[k + 1] += g[k - i] * fip1 * (i + 1);
}
g[k + 1] *= inv[k + 1];
}
return g;
}
FPS &inplace_imos(int n) {
inplace_pre(n);
for (int i = 0; i < n - 1; i++) (*this)[i + 1] += (*this)[i];
return derived();
}
FPS &inplace_iimos(int n) {
inplace_pre(n);
for (int i = 0; i < n - 1; i++) (*this)[i + 1] -= (*this)[i];
return derived();
}
FPS imos(int n) const { return FPS(derived()).inplace_imos(n); }
FPS iimos(int n) const { return FPS(derived()).inplace_iimos(n); }
};
} // namespace kk2
#endif // KK2_FPS_FPS_BASE_HPP
namespace kk2 {
template <class mint> struct FormalPowerSeriesNTTFriendly
: FormalPowerSeriesBase<FormalPowerSeriesNTTFriendly<mint>, mint> {
using base = FormalPowerSeriesBase<FormalPowerSeriesNTTFriendly<mint>, mint>;
using FPS = FormalPowerSeriesNTTFriendly<mint>;
using base::FormalPowerSeriesBase;
using base::operator*=; // 基底クラスのoperator*=を継承
static constexpr bool is_ntt_friendly = true;
// CRTPを使った実装 - overrideは不要
FPS &operator*=(const FPS &r) {
convolution(*this, r);
return *this;
}
void but() { butterfly(*this); }
void ibut() { butterfly_inv(*this); }
void db() { doubling(*this); }
static int but_pr() { return primitive_root<mint::getmod()>; }
FPS dense_inv(int deg = -1) const {
if (deg == -1) deg = (int)this->size();
FPS res(deg);
res[0] = {mint(1) / (*this)[0]};
for (int d = 1; d < deg; d <<= 1) {
FPS f(2 * d), g(2 * d);
std::copy(std::begin(*this),
std::begin(*this) + std::min((int)this->size(), 2 * d),
std::begin(f));
std::copy(std::begin(res), std::begin(res) + d, std::begin(g));
f.but();
g.but();
f.inplace_dot(g);
f.ibut();
std::fill(std::begin(f), std::begin(f) + d, mint(0));
f.but();
f.inplace_dot(g);
f.ibut();
for (int j = d; j < std::min(2 * d, deg); j++) res[j] = -f[j];
}
return res.pre(deg);
}
FPS dense_exp(int deg = -1) const {
if (deg == -1) deg = (int)this->size();
FPS b{1, 1 < (int)this->size() ? (*this)[1] : mint(0)};
FPS c{1}, z1, z2{1, 1};
for (int m = 2; m < deg; m <<= 1) {
auto y = b;
y.resize(m << 1);
y.but();
z1 = z2;
FPS z(m);
z = y.dot(z1);
z.ibut();
std::fill(std::begin(z), std::begin(z) + (m >> 1), mint(0));
z.but();
z.inplace_dot(-z1);
z.ibut();
c.insert(std::end(c), std::begin(z) + (m >> 1), std::end(z));
z2 = c;
z2.resize(m << 1);
z2.but();
FPS x(this->begin(), this->begin() + std::min<int>(this->size(), m));
x.resize(m);
x.inplace_diff();
x.push_back(mint(0));
x.but();
x.inplace_dot(y);
x.ibut();
x -= b.diff();
x.resize(m << 1);
for (int i = 0; i < m - 1; i++) {
x[m + i] = x[i];
x[i] = mint(0);
}
x.but();
x.inplace_dot(z2);
x.ibut();
x.pop_back();
x.inplace_int();
for (int i = m; i < std::min<int>(this->size(), m << 1); i++) x[i] += (*this)[i];
std::fill(std::begin(x), std::begin(x) + m, mint(0));
x.but();
x.inplace_dot(y);
x.ibut();
b.insert(std::end(b), std::begin(x) + m, std::end(x));
}
return FPS(std::begin(b), std::begin(b) + deg);
}
};
template <class mint> using FPSNTT = FormalPowerSeriesNTTFriendly<mint>;
} // namespace kk2
#endif // KK2_FPS_FPS_NTT_FRIENDLY_HPP
using namespace std;
void solve() {
/*
dp[i, j] = 長さ2i, max depth jの個数
dp[i, j] = sum_k>0 dp[k, j-1]*dp[i-k-1, j]
f_i = sum_j x^j * dp[j, i]
f_i = f_{i-1}*f_ix + 1
f_i = f_i f_{i-1} f_{i-2}x^2 + f_i f_{i-1} x + f_i
*/
using mint = kk2::mont998;
using fps = kk2::FPSNTT<mint>;
int n, k;
kin >> n >> k;
fps p1{1}, q1{1}, p2, q2;
rep (i, 1, k + 1) {
p2 = p1;
q2 = q1;
q1 = q1 - (p1 << 1);
p1 = q2;
// kdebug(f);
// kdebug(g);
}
mint ng_k = (p1 * q1.inv(n + 1))[n];
mint ng_km1 = (p2 * q2.inv(n + 1))[n];
kout << ng_k - ng_km1 << kendl;
}
int main() {
#ifdef KK2
int t = 1;
#else
int t = 1;
#endif
// kin >> t;
rep (t) solve();
return 0;
}
// Author: kk2
// converted by https://github.com/kk2a/cpp-bundle
// 2025-07-25 23:19:23