結果
問題 | No.2166 Paint and Fill |
ユーザー | maspy |
提出日時 | 2022-12-18 11:05:48 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 61,601 bytes |
コンパイル時間 | 8,721 ms |
コンパイル使用メモリ | 349,912 KB |
実行使用メモリ | 20,332 KB |
最終ジャッジ日時 | 2024-11-17 22:07:04 |
合計ジャッジ時間 | 197,700 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
8,704 KB |
testcase_01 | AC | 411 ms
20,332 KB |
testcase_02 | TLE | - |
testcase_03 | AC | 263 ms
8,832 KB |
testcase_04 | AC | 257 ms
8,832 KB |
testcase_05 | AC | 239 ms
8,832 KB |
testcase_06 | AC | 259 ms
8,704 KB |
testcase_07 | AC | 265 ms
8,832 KB |
testcase_08 | AC | 3,322 ms
8,960 KB |
testcase_09 | AC | 3,319 ms
8,960 KB |
testcase_10 | AC | 3,273 ms
8,704 KB |
testcase_11 | AC | 3,317 ms
8,832 KB |
testcase_12 | AC | 3,261 ms
5,248 KB |
testcase_13 | TLE | - |
testcase_14 | TLE | - |
testcase_15 | TLE | - |
testcase_16 | TLE | - |
testcase_17 | TLE | - |
testcase_18 | TLE | - |
testcase_19 | TLE | - |
testcase_20 | TLE | - |
testcase_21 | TLE | - |
testcase_22 | TLE | - |
testcase_23 | TLE | - |
testcase_24 | TLE | - |
testcase_25 | AC | 2 ms
5,248 KB |
testcase_26 | AC | 1 ms
5,248 KB |
testcase_27 | AC | 1,464 ms
16,484 KB |
testcase_28 | AC | 1,925 ms
16,620 KB |
testcase_29 | AC | 1,740 ms
16,624 KB |
testcase_30 | AC | 1,951 ms
16,620 KB |
testcase_31 | AC | 1,974 ms
16,624 KB |
testcase_32 | AC | 1,990 ms
16,620 KB |
testcase_33 | AC | 1,964 ms
16,616 KB |
testcase_34 | AC | 1,959 ms
16,612 KB |
testcase_35 | AC | 2,006 ms
16,624 KB |
testcase_36 | AC | 2,024 ms
16,620 KB |
testcase_37 | AC | 2,019 ms
16,620 KB |
testcase_38 | AC | 2,047 ms
16,496 KB |
testcase_39 | AC | 2,026 ms
20,208 KB |
ソースコード
#line 1 "library/my_template.hpp" #if defined(LOCAL) #include <my_template_compiled.hpp> #else #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; using ll = long long; using pi = pair<ll, ll>; using vi = vector<ll>; using u32 = unsigned int; using u64 = unsigned long long; using i128 = __int128; template <class T> using vc = vector<T>; template <class T> using vvc = vector<vc<T>>; template <class T> using vvvc = vector<vvc<T>>; template <class T> using vvvvc = vector<vvvc<T>>; template <class T> using vvvvvc = vector<vvvvc<T>>; template <class T> using pq = priority_queue<T>; template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>; #define vec(type, name, ...) vector<type> name(__VA_ARGS__) #define vv(type, name, h, ...) \ vector<vector<type>> name(h, vector<type>(__VA_ARGS__)) #define vvv(type, name, h, w, ...) \ vector<vector<vector<type>>> name( \ h, vector<vector<type>>(w, vector<type>(__VA_ARGS__))) #define vvvv(type, name, a, b, c, ...) \ vector<vector<vector<vector<type>>>> name( \ a, vector<vector<vector<type>>>( \ b, vector<vector<type>>(c, vector<type>(__VA_ARGS__)))) // https://trap.jp/post/1224/ #define FOR1(a) for (ll _ = 0; _ < ll(a); ++_) #define FOR2(i, a) for (ll i = 0; i < ll(a); ++i) #define FOR3(i, a, b) for (ll i = a; i < ll(b); ++i) #define FOR4(i, a, b, c) for (ll i = a; i < ll(b); i += (c)) #define FOR1_R(a) for (ll i = (a)-1; i >= ll(0); --i) #define FOR2_R(i, a) for (ll i = (a)-1; i >= ll(0); --i) #define FOR3_R(i, a, b) for (ll i = (b)-1; i >= ll(a); --i) #define FOR4_R(i, a, b, c) for (ll i = (b)-1; i >= ll(a); i -= (c)) #define overload4(a, b, c, d, e, ...) e #define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__) #define FOR_R(...) \ overload4(__VA_ARGS__, FOR4_R, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__) #define FOR_subset(t, s) for (ll t = s; t >= 0; t = (t == 0 ? -1 : (t - 1) & s)) #define all(x) x.begin(), x.end() #define len(x) ll(x.size()) #define elif else if #define eb emplace_back #define mp make_pair #define mt make_tuple #define fi first #define se second #define stoi stoll template <typename T, typename U> T SUM(const vector<U> &A) { T sum = 0; for (auto &&a: A) sum += a; return sum; } #define MIN(v) *min_element(all(v)) #define MAX(v) *max_element(all(v)) #define LB(c, x) distance((c).begin(), lower_bound(all(c), (x))) #define UB(c, x) distance((c).begin(), upper_bound(all(c), (x))) #define UNIQUE(x) \ sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit() int popcnt(int x) { return __builtin_popcount(x); } int popcnt(u32 x) { return __builtin_popcount(x); } int popcnt(ll x) { return __builtin_popcountll(x); } int popcnt(u64 x) { return __builtin_popcountll(x); } // (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2) int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); } int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); } int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); } int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); } // (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2) int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); } int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); } int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); } int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); } template <typename T> T pick(deque<T> &que) { T a = que.front(); que.pop_front(); return a; } template <typename T> T pick(pq<T> &que) { T a = que.top(); que.pop(); return a; } template <typename T> T pick(pqg<T> &que) { assert(que.size()); T a = que.top(); que.pop(); return a; } template <typename T> T pick(vc<T> &que) { assert(que.size()); T a = que.back(); que.pop_back(); return a; } template <typename T, typename U> T ceil(T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); } template <typename T, typename U> T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); } template <typename T, typename U> pair<T, T> divmod(T x, U y) { T q = floor(x, y); return {q, x - q * y}; } template <typename F> ll binary_search(F check, ll ok, ll ng) { assert(check(ok)); while (abs(ok - ng) > 1) { auto x = (ng + ok) / 2; tie(ok, ng) = (check(x) ? mp(x, ng) : mp(ok, x)); } return ok; } template <typename F> double binary_search_real(F check, double ok, double ng, int iter = 100) { FOR(iter) { double x = (ok + ng) / 2; tie(ok, ng) = (check(x) ? mp(x, ng) : mp(ok, x)); } return (ok + ng) / 2; } 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); } vc<int> s_to_vi(const string &S, char first_char) { vc<int> A(S.size()); FOR(i, S.size()) { A[i] = S[i] - first_char; } return A; } template <typename T, typename U> vector<T> cumsum(vector<U> &A, int off = 1) { int N = A.size(); vector<T> B(N + 1); FOR(i, N) { B[i + 1] = B[i] + A[i]; } if (off == 0) B.erase(B.begin()); return B; } template <typename CNT, typename T> vc<CNT> bincount(const vc<T> &A, int size) { vc<CNT> C(size); for (auto &&x: A) { ++C[x]; } return C; } // stable template <typename T> vector<int> argsort(const vector<T> &A) { vector<int> ids(A.size()); iota(all(ids), 0); sort(all(ids), [&](int i, int j) { return A[i] < A[j] || (A[i] == A[j] && i < j); }); return ids; } // A[I[0]], A[I[1]], ... template <typename T> vc<T> rearrange(const vc<T> &A, const vc<int> &I) { int n = len(I); vc<T> B(n); FOR(i, n) B[i] = A[I[i]]; return B; } #endif #line 1 "library/other/io.hpp" // based on yosupo's fastio #include <unistd.h> namespace fastio { // クラスが read(), print() を持っているかを判定するメタ関数 struct has_write_impl { template <class T> static auto check(T &&x) -> decltype(x.write(), std::true_type{}); template <class T> static auto check(...) -> std::false_type; }; template <class T> class has_write : public decltype(has_write_impl::check<T>(std::declval<T>())) { }; struct has_read_impl { template <class T> static auto check(T &&x) -> decltype(x.read(), std::true_type{}); template <class T> static auto check(...) -> std::false_type; }; template <class T> class has_read : public decltype(has_read_impl::check<T>(std::declval<T>())) {}; struct Scanner { FILE *fp; char line[(1 << 15) + 1]; size_t st = 0, ed = 0; void reread() { memmove(line, line + st, ed - st); ed -= st; st = 0; ed += fread(line + ed, 1, (1 << 15) - ed, fp); line[ed] = '\0'; } bool succ() { while (true) { if (st == ed) { reread(); if (st == ed) return false; } while (st != ed && isspace(line[st])) st++; if (st != ed) break; } if (ed - st <= 50) { bool sep = false; for (size_t i = st; i < ed; i++) { if (isspace(line[i])) { sep = true; break; } } if (!sep) reread(); } return true; } template <class T, enable_if_t<is_same<T, string>::value, int> = 0> bool read_single(T &ref) { if (!succ()) return false; while (true) { size_t sz = 0; while (st + sz < ed && !isspace(line[st + sz])) sz++; ref.append(line + st, sz); st += sz; if (!sz || st != ed) break; reread(); } return true; } template <class T, enable_if_t<is_integral<T>::value, int> = 0> bool read_single(T &ref) { if (!succ()) return false; bool neg = false; if (line[st] == '-') { neg = true; st++; } ref = T(0); while (isdigit(line[st])) { ref = 10 * ref + (line[st++] & 0xf); } if (neg) ref = -ref; return true; } template <typename T, typename enable_if<has_read<T>::value>::type * = nullptr> inline bool read_single(T &x) { x.read(); return true; } bool read_single(double &ref) { string s; if (!read_single(s)) return false; ref = std::stod(s); return true; } bool read_single(char &ref) { string s; if (!read_single(s) || s.size() != 1) return false; ref = s[0]; return true; } template <class T> bool read_single(vector<T> &ref) { for (auto &d: ref) { if (!read_single(d)) return false; } return true; } template <class T, class U> bool read_single(pair<T, U> &p) { return (read_single(p.first) && read_single(p.second)); } template <size_t N = 0, typename T> void read_single_tuple(T &t) { if constexpr (N < std::tuple_size<T>::value) { auto &x = std::get<N>(t); read_single(x); read_single_tuple<N + 1>(t); } } template <class... T> bool read_single(tuple<T...> &tpl) { read_single_tuple(tpl); return true; } void read() {} template <class H, class... T> void read(H &h, T &... t) { bool f = read_single(h); assert(f); read(t...); } Scanner(FILE *fp) : fp(fp) {} }; struct Printer { Printer(FILE *_fp) : fp(_fp) {} ~Printer() { flush(); } static constexpr size_t SIZE = 1 << 15; FILE *fp; char line[SIZE], small[50]; size_t pos = 0; void flush() { fwrite(line, 1, pos, fp); pos = 0; } void write(const char val) { if (pos == SIZE) flush(); line[pos++] = val; } template <class T, enable_if_t<is_integral<T>::value, int> = 0> void write(T val) { if (pos > (1 << 15) - 50) flush(); if (val == 0) { write('0'); return; } if (val < 0) { write('-'); val = -val; // todo min } size_t len = 0; while (val) { small[len++] = char(0x30 | (val % 10)); val /= 10; } for (size_t i = 0; i < len; i++) { line[pos + i] = small[len - 1 - i]; } pos += len; } void write(const string s) { for (char c: s) write(c); } void write(const char *s) { size_t len = strlen(s); for (size_t i = 0; i < len; i++) write(s[i]); } void write(const double x) { ostringstream oss; oss << fixed << setprecision(15) << x; string s = oss.str(); write(s); } void write(const long double x) { ostringstream oss; oss << fixed << setprecision(15) << x; string s = oss.str(); write(s); } template <typename T, typename enable_if<has_write<T>::value>::type * = nullptr> inline void write(T x) { x.write(); } template <class T> void write(const vector<T> val) { auto n = val.size(); for (size_t i = 0; i < n; i++) { if (i) write(' '); write(val[i]); } } template <class T, class U> void write(const pair<T, U> val) { write(val.first); write(' '); write(val.second); } template <size_t N = 0, typename T> void write_tuple(const T t) { if constexpr (N < std::tuple_size<T>::value) { if constexpr (N > 0) { write(' '); } const auto x = std::get<N>(t); write(x); write_tuple<N + 1>(t); } } template <class... T> bool write(tuple<T...> tpl) { write_tuple(tpl); return true; } template <class T, size_t S> void write(const array<T, S> val) { auto n = val.size(); for (size_t i = 0; i < n; i++) { if (i) write(' '); write(val[i]); } } void write(i128 val) { string s; bool negative = 0; if (val < 0) { negative = 1; val = -val; } while (val) { s += '0' + int(val % 10); val /= 10; } if (negative) s += "-"; reverse(all(s)); if (len(s) == 0) s = "0"; write(s); } }; Scanner scanner = Scanner(stdin); Printer printer = Printer(stdout); void flush() { printer.flush(); } void print() { printer.write('\n'); } template <class Head, class... Tail> void print(Head &&head, Tail &&... tail) { printer.write(head); if (sizeof...(Tail)) printer.write(' '); print(forward<Tail>(tail)...); } void read() {} template <class Head, class... Tail> void read(Head &head, Tail &... tail) { scanner.read(head); read(tail...); } } // namespace fastio using fastio::print; using fastio::flush; using fastio::read; #define INT(...) \ int __VA_ARGS__; \ read(__VA_ARGS__) #define LL(...) \ ll __VA_ARGS__; \ read(__VA_ARGS__) #define STR(...) \ string __VA_ARGS__; \ read(__VA_ARGS__) #define CHAR(...) \ char __VA_ARGS__; \ read(__VA_ARGS__) #define DBL(...) \ double __VA_ARGS__; \ read(__VA_ARGS__) #define VEC(type, name, size) \ vector<type> name(size); \ read(name) #define VV(type, name, h, w) \ vector<vector<type>> name(h, vector<type>(w)); \ read(name) void YES(bool t = 1) { print(t ? "YES" : "NO"); } void NO(bool t = 1) { YES(!t); } void Yes(bool t = 1) { print(t ? "Yes" : "No"); } void No(bool t = 1) { Yes(!t); } void yes(bool t = 1) { print(t ? "yes" : "no"); } void no(bool t = 1) { yes(!t); } #line 2 "library/mod/modint.hpp" template <int mod> struct modint { int val; constexpr modint(ll x = 0) noexcept { if (0 <= x && x < mod) val = x; else { x %= mod; val = (x < 0 ? x + mod : x); } } bool operator<(const modint &other) const { return val < other.val; } // To use std::map modint &operator+=(const modint &p) { if ((val += p.val) >= mod) val -= mod; return *this; } modint &operator-=(const modint &p) { if ((val += mod - p.val) >= mod) val -= mod; return *this; } modint &operator*=(const modint &p) { val = (int)(1LL * val * p.val % mod); return *this; } modint &operator/=(const modint &p) { *this *= p.inverse(); return *this; } modint operator-() const { return modint(-val); } modint operator+(const modint &p) const { return modint(*this) += p; } modint operator-(const modint &p) const { return modint(*this) -= p; } modint operator*(const modint &p) const { return modint(*this) *= p; } modint operator/(const modint &p) const { return modint(*this) /= p; } bool operator==(const modint &p) const { return val == p.val; } bool operator!=(const modint &p) const { return val != p.val; } modint inverse() const { int a = val, b = mod, u = 1, v = 0, t; while (b > 0) { t = a / b; swap(a -= t * b, b), swap(u -= t * v, v); } return modint(u); } modint pow(int64_t n) const { modint ret(1), mul(val); while (n > 0) { if (n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } void write() { fastio::printer.write(val); } void read() { fastio::scanner.read(val); } static constexpr int get_mod() { return mod; } }; struct ArbitraryModInt { static constexpr bool is_modint = true; int val; ArbitraryModInt() : val(0) {} ArbitraryModInt(int64_t y) : val(y >= 0 ? y % get_mod() : (get_mod() - (-y) % get_mod()) % get_mod()) {} bool operator<(const ArbitraryModInt &other) const { return val < other.val; } // To use std::map<ArbitraryModInt, T> static int &get_mod() { static int mod = 0; return mod; } static void set_mod(int md) { get_mod() = md; } ArbitraryModInt &operator+=(const ArbitraryModInt &p) { if ((val += p.val) >= get_mod()) val -= get_mod(); return *this; } ArbitraryModInt &operator-=(const ArbitraryModInt &p) { if ((val += get_mod() - p.val) >= get_mod()) val -= get_mod(); return *this; } ArbitraryModInt &operator*=(const ArbitraryModInt &p) { long long a = (long long)val * p.val; int xh = (int)(a >> 32), xl = (int)a, d, m; asm("divl %4; \n\t" : "=a"(d), "=d"(m) : "d"(xh), "a"(xl), "r"(get_mod())); val = m; return *this; } ArbitraryModInt &operator/=(const ArbitraryModInt &p) { *this *= p.inverse(); return *this; } ArbitraryModInt operator-() const { return ArbitraryModInt(get_mod() - val); } ArbitraryModInt operator+(const ArbitraryModInt &p) const { return ArbitraryModInt(*this) += p; } ArbitraryModInt operator-(const ArbitraryModInt &p) const { return ArbitraryModInt(*this) -= p; } ArbitraryModInt operator*(const ArbitraryModInt &p) const { return ArbitraryModInt(*this) *= p; } ArbitraryModInt operator/(const ArbitraryModInt &p) const { return ArbitraryModInt(*this) /= p; } bool operator==(const ArbitraryModInt &p) const { return val == p.val; } bool operator!=(const ArbitraryModInt &p) const { return val != p.val; } ArbitraryModInt inverse() const { int a = val, b = get_mod(), u = 1, v = 0, t; while (b > 0) { t = a / b; swap(a -= t * b, b), swap(u -= t * v, v); } return ArbitraryModInt(u); } ArbitraryModInt pow(int64_t n) const { ArbitraryModInt ret(1), mul(val); while (n > 0) { if (n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } void write() { fastio::printer.write(val); } void read() { fastio::scanner.read(val); } }; template <typename mint> mint inv(int n) { static const int mod = mint::get_mod(); static vector<mint> dat = {0, 1}; assert(0 <= n); if (n >= mod) n %= mod; while (int(dat.size()) <= n) { int k = dat.size(); auto q = (mod + k - 1) / k; int r = k * q - mod; dat.emplace_back(dat[r] * mint(q)); } return dat[n]; } template <typename mint> mint fact(int n) { static const int mod = mint::get_mod(); static vector<mint> dat = {1, 1}; assert(0 <= n); if (n >= mod) return 0; while (int(dat.size()) <= n) { int k = dat.size(); dat.emplace_back(dat[k - 1] * mint(k)); } return dat[n]; } template <typename mint> mint fact_inv(int n) { static const int mod = mint::get_mod(); static vector<mint> dat = {1, 1}; assert(-1 <= n && n < mod); if (n == -1) return mint(0); while (int(dat.size()) <= n) { int k = dat.size(); dat.emplace_back(dat[k - 1] * inv<mint>(k)); } return dat[n]; } template <class mint, class... Ts> mint fact_invs(Ts... xs) { return (mint(1) * ... * fact_inv<mint>(xs)); } template <typename mint, class Head, class... Tail> mint multinomial(Head &&head, Tail &&... tail) { return fact<mint>(head) * fact_invs<mint>(std::forward<Tail>(tail)...); } template <typename mint> mint C_dense(int n, int k) { static vvc<mint> C; static int H = 0, W = 0; auto calc = [&](int i, int j) -> mint { if (i == 0) return (j == 0 ? mint(1) : mint(0)); return C[i - 1][j] + (j ? C[i - 1][j - 1] : 0); }; if (W <= k) { FOR(i, H) { C[i].resize(k + 1); FOR(j, W, k + 1) { C[i][j] = calc(i, j); } } W = k + 1; } if (H <= n) { C.resize(n + 1); FOR(i, H, n + 1) { C[i].resize(W); FOR(j, W) { C[i][j] = calc(i, j); } } H = n + 1; } return C[n][k]; } template <typename mint, bool large = false, bool dense = false> mint C(ll n, ll k) { assert(n >= 0); if (k < 0 || n < k) return 0; if (dense) return C_dense<mint>(n, k); if (!large) return fact<mint>(n) * fact_inv<mint>(k) * fact_inv<mint>(n - k); k = min(k, n - k); mint x(1); FOR(i, k) { x *= mint(n - i); } x *= fact_inv<mint>(k); return x; } template <typename mint, bool large = false> mint C_inv(ll n, ll k) { assert(n >= 0); assert(0 <= k && k <= n); if (!large) return fact_inv<mint>(n) * fact<mint>(k) * fact<mint>(n - k); return mint(1) / C<mint, 1>(n, k); } // [x^d] (1-x) ^ {-n} の計算 template <typename mint, bool large = false, bool dense = false> mint C_negative(ll n, ll d) { assert(n >= 0); if (d < 0) return mint(0); if (n == 0) { return (d == 0 ? mint(1) : mint(0)); } return C<mint, large, dense>(n + d - 1, d); } using modint107 = modint<1000000007>; using modint998 = modint<998244353>; using amint = ArbitraryModInt; #line 2 "library/poly/count_terms.hpp" template<typename mint> int count_terms(const vc<mint>& f){ int t = 0; FOR(i, len(f)) if(f[i] != mint(0)) ++t; return t; } #line 2 "library/mod/mod_inv.hpp" // long でも大丈夫 ll mod_inv(ll val, ll mod) { val %= mod; if (val < 0) val += mod; ll a = val, b = mod, u = 1, v = 0, t; while (b > 0) { t = a / b; swap(a -= t * b, b), swap(u -= t * v, v); } if (u < 0) u += mod; return u; } #line 1 "library/poly/convolution_naive.hpp" template <class T> vector<T> convolution_naive(const vector<T>& a, const vector<T>& b) { int n = int(a.size()), m = int(b.size()); vector<T> ans(n + m - 1); if (n < m) { FOR(j, m) FOR(i, n) ans[i + j] += a[i] * b[j]; } else { FOR(i, n) FOR(j, m) ans[i + j] += a[i] * b[j]; } return ans; } #line 2 "library/poly/ntt.hpp" template <class mint> struct ntt_info { static constexpr int bsf_constexpr(unsigned int n) { int x = 0; while (!(n & (1 << x))) x++; return x; } static constexpr int rank2 = bsf_constexpr(mint::get_mod() - 1); array<mint, rank2 + 1> root; array<mint, rank2 + 1> iroot; array<mint, max(0, rank2 - 1)> rate2; array<mint, max(0, rank2 - 1)> irate2; array<mint, max(0, rank2 - 2)> rate3; array<mint, max(0, rank2 - 2)> irate3; ntt_info() { int g = primitive_root(mint::get_mod()); root[rank2] = mint(g).pow((mint::get_mod() - 1) >> rank2); iroot[rank2] = mint(1) / root[rank2]; FOR_R(i, rank2) { 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]; } } } constexpr int primitive_root(int m) { if (m == 167772161) return 3; if (m == 469762049) return 3; if (m == 754974721) return 11; if (m == 880803841) return 26; if (m == 998244353) return 3; if (m == 924844053) return 5; return -1; } }; template <class mint> void ntt(vector<mint>& a, bool inverse) { int n = int(a.size()); int h = topbit(n); assert(n == 1 << h); static const ntt_info<mint> info; if (!inverse) { 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(s, 1 << len) { int offset = s << (h - len); FOR(i, p) { auto l = a[i + offset]; auto r = a[i + offset + p] * rot; a[i + offset] = l + r; a[i + offset + p] = l - r; } rot *= info.rate2[topbit(~s & -~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::get_mod() * mint::get_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); } rot *= info.rate3[topbit(~s & -~s)]; } len += 2; } } } else { mint coef = mint(1) / mint(len(a)); FOR(i, len(a)) a[i] *= coef; int len = h; while (len) { if (len == 1) { int p = 1 << (h - len); mint irot = 1; FOR(s, 1 << (len - 1)) { int offset = s << (h - len + 1); FOR(i, p) { auto l = a[i + offset]; auto r = a[i + offset + p]; a[i + offset] = l + r; a[i + offset + p] = (unsigned long long)(mint::get_mod() + l.val - r.val) * irot.val; ; } irot *= info.irate2[topbit(~s & -~s)]; } len--; } else { int p = 1 << (h - len); mint irot = 1, iimag = info.iroot[2]; FOR(s, (1 << (len - 2))) { 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::get_mod() + a2 - a3) * iimag.val).val; a[i + offset] = a0 + a1 + a2 + a3; a[i + offset + 1 * p] = (a0 + (mint::get_mod() - a1) + a2na3iimag) * irot.val; a[i + offset + 2 * p] = (a0 + a1 + (mint::get_mod() - a2) + (mint::get_mod() - a3)) * irot2.val; a[i + offset + 3 * p] = (a0 + (mint::get_mod() - a1) + (mint::get_mod() - a2na3iimag)) * irot3.val; } irot *= info.irate3[topbit(~s & -~s)]; } len -= 2; } } } } #line 1 "library/poly/fft.hpp" namespace CFFT { using real = double; struct C { real x, y; C() : x(0), y(0) {} C(real x, real y) : x(x), y(y) {} inline C operator+(const C& c) const { return C(x + c.x, y + c.y); } inline C operator-(const C& c) const { return C(x - c.x, y - c.y); } inline C operator*(const C& c) const { return C(x * c.x - y * c.y, x * c.y + y * c.x); } inline C conj() const { return C(x, -y); } }; const real PI = acosl(-1); int base = 1; vector<C> rts = {{0, 0}, {1, 0}}; vector<int> rev = {0, 1}; void ensure_base(int nbase) { if (nbase <= base) return; rev.resize(1 << nbase); rts.resize(1 << nbase); for (int i = 0; i < (1 << nbase); i++) { rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (nbase - 1)); } while (base < nbase) { real angle = PI * 2.0 / (1 << (base + 1)); for (int i = 1 << (base - 1); i < (1 << base); i++) { rts[i << 1] = rts[i]; real angle_i = angle * (2 * i + 1 - (1 << base)); rts[(i << 1) + 1] = C(cos(angle_i), sin(angle_i)); } ++base; } } void fft(vector<C>& a, int n) { assert((n & (n - 1)) == 0); int zeros = __builtin_ctz(n); ensure_base(zeros); int shift = base - zeros; for (int i = 0; i < n; i++) { if (i < (rev[i] >> shift)) { swap(a[i], a[rev[i] >> shift]); } } for (int k = 1; k < n; k <<= 1) { for (int i = 0; i < n; i += 2 * k) { for (int j = 0; j < k; j++) { C z = a[i + j + k] * rts[j + k]; a[i + j + k] = a[i + j] - z; a[i + j] = a[i + j] + z; } } } } } // namespace CFFT #line 7 "library/poly/convolution.hpp" template <class mint> vector<mint> convolution_ntt(vector<mint> a, vector<mint> b) { int n = int(a.size()), m = int(b.size()); int sz = 1; while (sz < n + m - 1) sz *= 2; // sz = 2^k のときの高速化。分割統治的なやつで損しまくるので。 if ((n + m - 3) <= sz / 2) { auto a_last = a.back(), b_last = b.back(); a.pop_back(), b.pop_back(); auto c = convolution(a, b); c.resize(n + m - 1); c[n + m - 2] = a_last * b_last; FOR(i, len(a)) c[i + len(b)] += a[i] * b_last; FOR(i, len(b)) c[i + len(a)] += b[i] * a_last; return c; } a.resize(sz), b.resize(sz); bool same = a == b; ntt(a, 0); if (same) { b = a; } else { ntt(b, 0); } FOR(i, sz) a[i] *= b[i]; ntt(a, 1); a.resize(n + m - 1); return a; } template <typename mint> vector<mint> convolution_garner(const vector<mint>& a, const vector<mint>& b) { int n = len(a), m = len(b); if (!n || !m) return {}; static const long long nttprimes[] = {754974721, 167772161, 469762049}; using mint0 = modint<754974721>; using mint1 = modint<167772161>; using mint2 = modint<469762049>; vc<mint0> a0(n), b0(m); vc<mint1> a1(n), b1(m); vc<mint2> a2(n), b2(m); FOR(i, n) a0[i] = a[i].val, a1[i] = a[i].val, a2[i] = a[i].val; FOR(i, m) b0[i] = b[i].val, b1[i] = b[i].val, b2[i] = b[i].val; auto c0 = convolution_ntt<mint0>(a0, b0); auto c1 = convolution_ntt<mint1>(a1, b1); auto c2 = convolution_ntt<mint2>(a2, b2); static const long long m01 = 1LL * nttprimes[0] * nttprimes[1]; static const long long m0_inv_m1 = mint1(nttprimes[0]).inverse().val; static const long long m01_inv_m2 = mint2(m01).inverse().val; static const int mod = mint::get_mod(); auto garner = [&](mint0 x0, mint1 x1, mint2 x2) -> mint { int r0 = x0.val, r1 = x1.val, r2 = x2.val; int v1 = (m0_inv_m1 * (r1 + nttprimes[1] - r0)) % nttprimes[1]; auto v2 = (mint2(r2) - r0 - mint2(nttprimes[0]) * v1) * mint2(m01_inv_m2); return mint(r0 + 1LL * nttprimes[0] * v1 + m01 % mod * v2.val); }; vc<mint> c(len(c0)); FOR(i, len(c)) c[i] = garner(c0[i], c1[i], c2[i]); return c; } template <typename R> vc<double> convolution_fft(const vc<R>& a, const vc<R>& b) { using C = CFFT::C; int need = (int)a.size() + (int)b.size() - 1; int nbase = 1; while ((1 << nbase) < need) nbase++; CFFT::ensure_base(nbase); int sz = 1 << nbase; vector<C> fa(sz); for (int i = 0; i < sz; i++) { int x = (i < (int)a.size() ? a[i] : 0); int y = (i < (int)b.size() ? b[i] : 0); fa[i] = C(x, y); } CFFT::fft(fa, sz); C r(0, -0.25 / (sz >> 1)), s(0, 1), t(0.5, 0); for (int i = 0; i <= (sz >> 1); i++) { int j = (sz - i) & (sz - 1); C z = (fa[j] * fa[j] - (fa[i] * fa[i]).conj()) * r; fa[j] = (fa[i] * fa[i] - (fa[j] * fa[j]).conj()) * r; fa[i] = z; } for (int i = 0; i < (sz >> 1); i++) { C A0 = (fa[i] + fa[i + (sz >> 1)]) * t; C A1 = (fa[i] - fa[i + (sz >> 1)]) * t * CFFT::rts[(sz >> 1) + i]; fa[i] = A0 + A1 * s; } CFFT::fft(fa, sz >> 1); vector<double> ret(need); for (int i = 0; i < need; i++) { ret[i] = (i & 1 ? fa[i >> 1].y : fa[i >> 1].x); } return ret; } vector<ll> convolution(const vector<ll>& a, const vector<ll>& b) { int n = len(a), m = len(b); if (!n || !m) return {}; if (min(n, m) <= 60) return convolution_naive(a, b); ll abs_sum_a = 0, abs_sum_b = 0; ll LIM = 1e15; FOR(i, n) abs_sum_a = min(LIM, abs_sum_a + abs(a[i])); FOR(i, n) abs_sum_b = min(LIM, abs_sum_b + abs(b[i])); if (i128(abs_sum_a) * abs_sum_b < 1e15) { vc<double> c = convolution_fft<ll>(a, b); vc<ll> res(len(c)); FOR(i, len(c)) res[i] = ll(floor(c[i] + .5)); return res; } static constexpr unsigned long long MOD1 = 754974721; // 2^24 static constexpr unsigned long long MOD2 = 167772161; // 2^25 static constexpr unsigned long long MOD3 = 469762049; // 2^26 static constexpr unsigned long long M2M3 = MOD2 * MOD3; static constexpr unsigned long long M1M3 = MOD1 * MOD3; static constexpr unsigned long long M1M2 = MOD1 * MOD2; static constexpr unsigned long long M1M2M3 = MOD1 * MOD2 * MOD3; static const unsigned long long i1 = mod_inv(MOD2 * MOD3, MOD1); static const unsigned long long i2 = mod_inv(MOD1 * MOD3, MOD2); static const unsigned long long i3 = mod_inv(MOD1 * MOD2, MOD3); using mint1 = modint<MOD1>; using mint2 = modint<MOD2>; using mint3 = modint<MOD3>; vc<mint1> a1(n), b1(m); vc<mint2> a2(n), b2(m); vc<mint3> a3(n), b3(m); FOR(i, n) a1[i] = a[i], a2[i] = a[i], a3[i] = a[i]; FOR(i, m) b1[i] = b[i], b2[i] = b[i], b3[i] = b[i]; auto c1 = convolution_ntt<mint1>(a1, b1); auto c2 = convolution_ntt<mint2>(a2, b2); auto c3 = convolution_ntt<mint3>(a3, b3); vc<ll> c(n + m - 1); FOR(i, n + m - 1) { u64 x = 0; x += (c1[i].val * i1) % MOD1 * M2M3; x += (c2[i].val * i2) % MOD2 * M1M3; x += (c3[i].val * i3) % MOD3 * M1M2; ll diff = c1[i].val - ((long long)(x) % (long long)(MOD1)); if (diff < 0) diff += MOD1; static constexpr unsigned long long offset[5] = {0, 0, M1M2M3, 2 * M1M2M3, 3 * M1M2M3}; x -= offset[diff % 5]; c[i] = x; } return c; } template <typename mint> enable_if_t<is_same<mint, modint998>::value, vc<mint>> convolution( const vc<mint>& a, const vc<mint>& b) { int n = len(a), m = len(b); if (!n || !m) return {}; if (min(n, m) <= 60) return convolution_naive(a, b); return convolution_ntt(a, b); } template <typename mint> enable_if_t<!is_same<mint, modint998>::value, vc<mint>> convolution( const vc<mint>& a, const vc<mint>& b) { int n = len(a), m = len(b); if (!n || !m) return {}; if (min(n, m) <= 60) return convolution_naive(a, b); return convolution_garner(a, b); } #line 2 "library/poly/integrate.hpp" template <typename mint> vc<mint> integrate(const vc<mint>& f) { vc<mint> g(len(f) + 1); FOR3(i, 1, len(g)) g[i] = f[i - 1] * inv<mint>(i); return g; } #line 2 "library/poly/differentiate.hpp" template <typename mint> vc<mint> differentiate(const vc<mint>& f) { if (len(f) <= 1) return {}; vc<mint> g(len(f) - 1); FOR(i, len(g)) g[i] = f[i + 1] * mint(i + 1); return g; } #line 6 "library/poly/fps_exp.hpp" template <typename mint> enable_if_t<is_same<mint, modint998>::value, vc<mint>> fps_exp(vc<mint>& f) { if (count_terms(f) <= 300) return fps_exp_sparse(f); return fps_exp_dense(f); } template <typename mint> enable_if_t<!is_same<mint, modint998>::value, vc<mint>> fps_exp(vc<mint>& f) { if (count_terms(f) <= 1000) return fps_exp_sparse(f); return fps_exp_dense(f); } template <typename mint> vc<mint> fps_exp_sparse(vc<mint>& f) { if (len(f) == 0) return {mint(1)}; assert(f[0] == 0); int N = len(f); // df を持たせる vc<pair<int, mint>> dat; FOR(i, 1, N) if (f[i] != mint(0)) dat.eb(i - 1, mint(i) * f[i]); vc<mint> F(N); F[0] = 1; FOR(n, 1, N) { mint rhs = 0; for (auto&& [k, fk]: dat) { if (k > n - 1) break; rhs += fk * F[n - 1 - k]; } F[n] = rhs * inv<mint>(n); } return F; } template <typename mint> enable_if_t<!is_same<mint, modint998>::value, vc<mint>> fps_exp_dense( vc<mint> h) { const int L = len(h); assert(L > 0 && h[0] == mint(0)); int LOG = 0; while (1 << LOG < L) ++LOG; h.resize(1 << LOG); auto dh = differentiate(h); vc<mint> f = {1}, g = {1}; int m = 1; vc<mint> p; FOR(LOG) { p = convolution(f, g); p.resize(m); p = convolution(p, g); p.resize(m); g.resize(m); FOR(i, m) g[i] += g[i] - p[i]; p = {dh.begin(), dh.begin() + m - 1}; p = convolution(f, p); p.resize(m + m - 1); FOR(i, m + m - 1) p[i] = -p[i]; FOR(i, m - 1) p[i] += mint(i + 1) * f[i + 1]; p = convolution(p, g); p.resize(m + m - 1); FOR(i, m - 1) p[i] += dh[i]; p = integrate(p); FOR(i, m + m) p[i] = h[i] - p[i]; p[0] += mint(1); f = convolution(f, p); f.resize(m + m); m += m; } f.resize(L); return f; } // ntt 素数専用実装。長さ n の FFT を利用して 2n の FFT // を行うなどの高速化をしている。 template <typename mint> enable_if_t<is_same<mint, modint998>::value, vc<mint>> fps_exp_dense( vc<mint>& f) { const int n = len(f); assert(n > 0 && f[0] == mint(0)); vc<mint> b = {1, (1 < n ? f[1] : 0)}; vc<mint> c = {1}, z1, z2 = {1, 1}; while (len(b) < n) { int m = len(b); auto y = b; y.resize(2 * m); ntt(y, 0); z1 = z2; vc<mint> z(m); FOR(i, m) z[i] = y[i] * z1[i]; ntt(z, 1); FOR(i, m / 2) z[i] = 0; ntt(z, 0); FOR(i, m) z[i] *= -z1[i]; ntt(z, 1); c.insert(c.end(), z.begin() + m / 2, z.end()); z2 = c; z2.resize(2 * m); ntt(z2, 0); vc<mint> x(f.begin(), f.begin() + m); FOR(i, len(x) - 1) x[i] = x[i + 1] * mint(i + 1); x.back() = 0; ntt(x, 0); FOR(i, m) x[i] *= y[i]; ntt(x, 1); FOR(i, m - 1) x[i] -= b[i + 1] * mint(i + 1); x.resize(m + m); FOR(i, m - 1) x[m + i] = x[i], x[i] = 0; ntt(x, 0); FOR(i, m + m) x[i] *= z2[i]; ntt(x, 1); FOR_R(i, len(x) - 1) x[i + 1] = x[i] * inv<mint>(i + 1); x[0] = 0; FOR3(i, m, min(n, m + m)) x[i] += f[i]; FOR(i, m) x[i] = 0; ntt(x, 0); FOR(i, m + m) x[i] *= y[i]; ntt(x, 1); b.insert(b.end(), x.begin() + m, x.end()); } b.resize(n); return b; } #line 2 "library/poly/fps_log.hpp" #line 4 "library/poly/fps_inv.hpp" template <typename mint> vc<mint> fps_inv_sparse(const vc<mint>& f) { assert(f[0] != mint(0)); int N = len(f); vc<pair<int, mint>> dat; FOR3(i, 1, N) if (f[i] != mint(0)) dat.eb(i, f[i]); vc<mint> g(N); mint g0 = mint(1) / f[0]; g[0] = g0; FOR3(n, 1, N) { mint rhs = 0; for (auto&& [k, fk]: dat) { if (k > n) break; rhs -= fk * g[n - k]; } g[n] = rhs * g0; } return g; } template <typename mint> enable_if_t<is_same<mint, modint998>::value, vc<mint>> fps_inv_dense( const vc<mint>& F) { assert(F[0] != mint(0)); vc<mint> G = {mint(1) / F[0]}; G.reserve(len(F)); ll N = len(F), n = 1; while (n < N) { vc<mint> f(2 * n), g(2 * n); FOR(i, min(N, 2 * n)) f[i] = F[i]; FOR(i, n) g[i] = G[i]; ntt(f, false); ntt(g, false); FOR(i, 2 * n) f[i] *= g[i]; ntt(f, true); FOR(i, n) f[i] = 0; ntt(f, false); FOR(i, 2 * n) f[i] *= g[i]; ntt(f, true); FOR3(i, n, 2 * n) G.eb(f[i] * mint(-1)); n *= 2; } G.resize(N); return G; } template <typename mint> enable_if_t<!is_same<mint, modint998>::value, vc<mint>> fps_inv_dense( const vc<mint>& F) { int N = len(F); assert(F[0] != mint(0)); vc<mint> R = {mint(1) / F[0]}; vc<mint> p; int m = 1; while (m < N) { p = convolution(R, R); p.resize(m + m); vc<mint> f = {F.begin(), F.begin() + min(m + m, N)}; p = convolution(p, f); R.resize(m + m); FOR(i, m + m) R[i] = R[i] + R[i] - p[i]; m += m; } R.resize(N); return R; } template <typename mint> enable_if_t<is_same<mint, modint998>::value, vc<mint>> fps_inv( const vc<mint>& f) { if (count_terms(f) <= 200) return fps_inv_sparse<mint>(f); return fps_inv_dense<mint>(f); } template <typename mint> enable_if_t<!is_same<mint, modint998>::value, vc<mint>> fps_inv( const vc<mint>& f) { if (count_terms(f) <= 700) return fps_inv_sparse<mint>(f); return fps_inv_dense<mint>(f); } #line 5 "library/poly/fps_log.hpp" template <typename mint> vc<mint> fps_log_dense(const vc<mint>& f) { assert(f[0] == mint(1)); ll N = len(f); vc<mint> df = f; FOR(i, N) df[i] *= mint(i); df.erase(df.begin()); auto f_inv = fps_inv(f); auto g = convolution(df, f_inv); g.resize(N - 1); g.insert(g.begin(), 0); FOR(i, N) g[i] *= inv<mint>(i); return g; } template<typename mint> vc<mint> fps_log_sparse(const vc<mint>& f){ int N = f.size(); vc<pair<int, mint>> dat; FOR(i, 1, N) if(f[i] != mint(0)) dat.eb(i, f[i]); vc<mint> F(N); vc<mint> g(N - 1); for (int n = 0; n < N - 1; ++n) { mint rhs = mint(n + 1) * f[n + 1]; for (auto &&[i, fi]: dat) { if (i > n) break; rhs -= fi * g[n - i]; } g[n] = rhs; F[n + 1] = rhs * inv<mint>(n + 1); } return F; } template<typename mint> vc<mint> fps_log(const vc<mint>& f){ assert(f[0] == mint(1)); if(count_terms(f) <= 200) return fps_log_sparse(f); return fps_log_dense(f); } #line 5 "library/poly/fps_pow.hpp" // fps の k 乗を求める。k >= 0 の前提である。 // 定数項が 1 で、k が mint の場合には、fps_pow_1 を使うこと。 // ・dense な場合: log, exp を使う O(NlogN) // ・sparse な場合: O(NK) template <typename mint> vc<mint> fps_pow(const vc<mint>& f, ll k) { assert(0 <= k); int n = len(f); if(k==0){ vc<mint> g(n); g[0] = mint(1); return g; } int d = n; FOR_R(i, n) if (f[i] != 0) d = i; // d * k >= n if(d >= ceil(n,k)){ vc<mint> g(n); return g; } ll off = d * k; mint c = f[d]; mint c_inv = mint(1) / mint(c); vc<mint> g(n - off); FOR(i, n - off) g[i] = f[d + i] * c_inv; g = fps_pow_1(g, mint(k)); vc<mint> h(n); c = c.pow(k); FOR(i, len(g)) h[off + i] = g[i] * c; return h; } template <typename mint> vc<mint> fps_pow_1_sparse(const vc<mint>& f, mint K) { int N = len(f); vc<pair<int, mint>> dat; FOR3(i, 1, N) if (f[i] != mint(0)) dat.eb(i, f[i]); vc<mint> g(N); g[0] = 1; FOR(n, N - 1) { mint& x = g[n + 1]; for (auto&& [d, cf]: dat) { if (d > n + 1) break; mint t = cf * g[n - d + 1]; x += t * (K * mint(d) - mint(n - d + 1)); } x *= inv<mint>(n + 1); } return g; } template <typename mint> vc<mint> fps_pow_1_dense(const vc<mint>& f, mint K) { assert(f[0] == mint(1)); auto log_f = fps_log(f); FOR(i, len(f)) log_f[i] *= K; return fps_exp(log_f); } template <typename mint> vc<mint> fps_pow_1(const vc<mint>& f, mint K) { if (count_terms(f) <= 100) return fps_pow_1_sparse(f, K); return fps_pow_1_dense(f, K); } #line 2 "library/poly/from_log_differentiation.hpp" #line 2 "library/linalg/mat_mul.hpp" struct has_mod_impl { template <class T> static auto check(T&& x) -> decltype(x.get_mod(), std::true_type{}); template <class T> static auto check(...) -> std::false_type; }; template <class T> class has_mod : public decltype(has_mod_impl::check<T>(std::declval<T>())) {}; template <class T, typename enable_if<has_mod<T>::value>::type* = nullptr> vc<vc<T>> mat_mul(const vc<vc<T>>& A, const vc<vc<T>>& B) { const int mod = T::get_mod(); auto N = len(A), M = len(B), K = len(B[0]); vv(int, b, K, M); FOR(i, M) FOR(j, K) b[j][i] = B[i][j].val; vv(T, C, N, K); FOR(i, N) { FOR(j, K) { i128 sm = 0; FOR(m, M) { sm += ll(A[i][m].val) * b[j][m]; } C[i][j] = sm % mod; } } return C; } template <class T, typename enable_if<!has_mod<T>::value>::type* = nullptr> vc<vc<T>> mat_mul(const vc<vc<T>>& A, const vc<vc<T>>& B) { auto N = len(A), M = len(B), K = len(B[0]); vv(T, C, N, K); FOR(n, N) FOR(m, M) FOR(k, K) C[n][k] += A[n][m] * B[m][k]; return C; } #line 2 "library/alg/monoid/mul.hpp" template <class T> struct Monoid_Mul { using value_type = T; using X = T; static constexpr X op(const X &x, const X &y) noexcept { return x * y; } static constexpr X inverse(const X &x) noexcept { return X(1) / x; } static constexpr X unit() { return X(1); } static constexpr bool commute = true; }; #line 1 "library/ds/sliding_window_aggregation.hpp" template <class Monoid> struct Sliding_Window_Aggregation { using X = typename Monoid::value_type; using value_type = X; int sz = 0; vc<X> dat; vc<X> cum_l; X cum_r; Sliding_Window_Aggregation() : cum_l({Monoid::unit()}), cum_r(Monoid::unit()) {} int size() { return sz; } void push(X x) { ++sz; cum_r = Monoid::op(cum_r, x); dat.eb(x); } void pop() { --sz; cum_l.pop_back(); if (len(cum_l) == 0) { cum_l = {Monoid::unit()}; cum_r = Monoid::unit(); while (len(dat) > 1) { cum_l.eb(Monoid::op(dat.back(), cum_l.back())); dat.pop_back(); } dat.pop_back(); } } X lprod() { return cum_l.back(); } X rprod() { return cum_r; } X prod() { return Monoid::op(cum_l.back(), cum_r); } void debug() { print("swag"); print("dat", dat); print("cum_l", cum_l); print("cum_r", cum_r); } }; // 定数倍は目に見えて遅くなるので、queue でよいときは使わない template <class Monoid> struct SWAG_deque { using X = typename Monoid::value_type; using value_type = X; int sz; vc<X> dat_l, dat_r; vc<X> cum_l, cum_r; SWAG_deque() : sz(0), cum_l({Monoid::unit()}), cum_r({Monoid::unit()}) {} int size() { return sz; } void push_back(X x) { ++sz; dat_r.eb(x); cum_r.eb(Monoid::op(cum_r.back(), x)); } void push_front(X x) { ++sz; dat_l.eb(x); cum_l.eb(Monoid::op(x, cum_l.back())); } void push(X x) { push_back(x); } void clear() { sz = 0; dat_l.clear(), dat_r.clear(); cum_l = {Monoid::unit()}, cum_r = {Monoid::unit()}; } void pop_front() { if (sz == 1) return clear(); if (dat_l.empty()) rebuild(); --sz; dat_l.pop_back(); cum_l.pop_back(); } void pop_back() { if (sz == 1) return clear(); if (dat_r.empty()) rebuild(); --sz; dat_r.pop_back(); cum_r.pop_back(); } void pop() { pop_front(); } X lprod() { return cum_l.back(); } X rprod() { return cum_r.back(); } X prod() { return Monoid::op(cum_l.back(), cum_r.back()); } X prod_all() { return prod(); } void debug() { print("swag"); print("dat_l", dat_l); print("dat_r", dat_r); print("cum_l", cum_l); print("cum_r", cum_r); } private: void rebuild() { vc<X> X; FOR_R(i, len(dat_l)) X.eb(dat_l[i]); X.insert(X.end(), all(dat_r)); clear(); int m = len(X) / 2; FOR_R(i, m) push_front(X[i]); FOR(i, m, len(X)) push_back(X[i]); assert(sz == len(X)); } }; #line 5 "library/poly/lagrange_interpolate_iota.hpp" // Input: f(0), ..., f(n-1) and c, m // Return: f(c), f(c+1), ..., f(c+m-1) // Complexity: M(n, n + m) template <typename mint> vc<mint> lagrange_interpolate_iota(vc<mint> &f, mint c, int m) { if (m <= 60) { vc<mint> ANS(m); FOR(i, m) ANS[i] = lagrange_interpolate_iota(f, c + mint(i)); return ANS; } ll n = len(f); auto a = f; FOR(i, n) { a[i] = a[i] * fact_inv<mint>(i) * fact_inv<mint>(n - 1 - i); if ((n - 1 - i) & 1) a[i] = -a[i]; } // x = c, c+1, ... に対して a0/x + a1/(x-1) + ... を求めておく vc<mint> b(n + m - 1); FOR(i, n + m - 1) b[i] = mint(1) / (c + mint(i - n + 1)); a = convolution(a, b); Sliding_Window_Aggregation<Monoid_Mul<mint>> swag; vc<mint> ANS(m); ll L = 0, R = 0; FOR(i, m) { while (L < i) { swag.pop(), ++L; } while (R - L < n) { swag.push(c + mint((R++) - n + 1)); } auto coef = swag.prod(); if (coef == 0) { ANS[i] = f[(c + i).val]; } else { ANS[i] = a[i + n - 1] * coef; } } return ANS; } // Input: f(0), ..., f(n-1) and c // Return: f(c) // Complexity: O(n) template <typename mint> mint lagrange_interpolate_iota(vc<mint> &f, mint c) { int n = len(f); if (int(c.val) < n) return f[c.val]; auto a = f; FOR(i, n) { a[i] = a[i] * fact_inv<mint>(i) * fact_inv<mint>(n - 1 - i); if ((n - 1 - i) & 1) a[i] = -a[i]; } vc<mint> lp(n + 1), rp(n + 1); lp[0] = rp[n] = 1; FOR(i, n) lp[i + 1] = lp[i] * (c - i); FOR_R(i, n) rp[i] = rp[i + 1] * (c - i); mint ANS = 0; FOR(i, n) ANS += a[i] * lp[i] * rp[i + 1]; return ANS; } #line 4 "library/poly/prefix_product_of_poly.hpp" // A[k-1]...A[0] を計算する // アルゴリズム参考:https://github.com/noshi91/n91lib_rs/blob/master/src/algorithm/polynomial_matrix_prod.rs // 実装参考:https://nyaannyaan.github.io/library/matrix/polynomial-matrix-prefix-prod.hpp template <typename T> vc<vc<T>> prefix_product_of_poly_matrix(vc<vc<vc<T>>>& A, ll k) { int n = len(A); using MAT = vc<vc<T>>; auto shift = [&](vc<MAT>& G, T x) -> vc<MAT> { int d = len(G); vvv(T, H, d, n, n); FOR(i, n) FOR(j, n) { vc<T> g(d); FOR(l, d) g[l] = G[l][i][j]; auto h = lagrange_interpolate_iota(g, x, d); FOR(l, d) H[l][i][j] = h[l]; } return H; }; auto evaluate = [&](vc<T>& f, T x) -> T { T res = 0; T p = 1; FOR(i, len(f)) { res += f[i] * p; p *= x; } return res; }; ll deg = 1; FOR(i, n) FOR(j, n) chmax(deg, len(A[i][j]) - 1); vc<MAT> G(deg + 1); ll v = 1; while (deg * v * v < k) v *= 2; T iv = T(1) / T(v); FOR(i, len(G)) { T x = T(v) * T(i); vv(T, mat, n, n); FOR(j, n) FOR(k, n) mat[j][k] = evaluate(A[j][k], x); G[i] = mat; } for (ll w = 1; w != v; w *= 2) { T W = w; auto G1 = shift(G, W * iv); auto G2 = shift(G, (W * T(deg) * T(v) + T(v)) * iv); auto G3 = shift(G, (W * T(deg) * T(v) + T(v) + W) * iv); FOR(i, w * deg + 1) { G[i] = mat_mul(G1[i], G[i]); G2[i] = mat_mul(G3[i], G2[i]); } copy(G2.begin(), G2.end() - 1, back_inserter(G)); } vv(T, res, n, n); FOR(i, n) res[i][i] = 1; ll i = 0; while (i + v <= k) res = mat_mul(G[i / v], res), i += v; while (i < k) { vv(T, mat, n, n); FOR(j, n) FOR(k, n) mat[j][k] = evaluate(A[j][k], i); res = mat_mul(mat, res); ++i; } return res; } // f[k-1]...f[0] を計算する template <typename T> T prefix_product_of_poly(vc<T>& f, ll k) { vc<vc<vc<T>>> A(1); A[0].resize(1); A[0][0] = f; auto res = prefix_product_of_poly_matrix(A, k); return res[0][0]; } #line 2 "library/seq/kth_term_of_p_recursive.hpp" // a0, ..., a_{r-1} および f_0, ..., f_r を与える // a_r f_0(0) + a_{r-1}f_1(0) + ... = 0 // a_{r+1} f_0(1) + a_{r}f_1(1) + ... = 0 template <typename T> T kth_term_of_p_recursive(vc<T> a, vc<vc<T>>& fs, ll k) { int r = len(a); assert(len(fs) == r + 1); if (k < r) return a[k]; vc<vc<vc<T>>> A; A.resize(r); FOR(i, r) A[i].resize(r); FOR(i, r) { // A[0][i] = -fs[i + 1]; for (auto&& x: fs[i + 1]) A[0][i].eb(-x); } FOR3(i, 1, r) A[i][i - 1] = fs[0]; vc<T> den = fs[0]; auto res = prefix_product_of_poly_matrix(A, k - r + 1); reverse(all(a)); T ANS = 0; FOR(j, r) ANS += res[0][j] * a[j]; ANS /= prefix_product_of_poly(den, k - r + 1); return ANS; } #line 4 "library/poly/from_log_differentiation.hpp" // 対数微分 F'/F = a(x)/b(x) から F を復元する。 // a, b が sparse であれば、O(N(K1+K2)) 時間でできる template <typename mint> vc<mint> from_log_differentiation(int N, const vc<mint>& a, const vc<mint>& b) { assert(b[0] == mint(1)); using P = pair<int, mint>; vc<P> dat_a, dat_b; FOR(i, len(a)) if (a[i] != mint(0)) dat_a.eb(i, a[i]); FOR(i, 1, len(b)) if (b[i] != mint(0)) dat_b.eb(i, b[i]); vc<mint> f(N + 1); vc<mint> df(N); f[0] = mint(1); FOR(n, N) { mint v = 0; for (auto&& [i, bi]: dat_b) { if (i > n) break; v -= bi * df[n - i]; } for (auto&& [i, ai]: dat_a) { if (i > n) break; v += ai * f[n - i]; } df[n] = v; f[n + 1] = df[n] * inv<mint>(n + 1); } return f; } // F'/F = a/b の解の、[x^K]F を求める。右辺は低次の有理式。 template <typename mint> mint from_log_differentiation_kth(int K, vc<mint>& a, vc<mint>& b) { assert(b[0] == mint(1)); int r = max(len(a), len(b) - 1); vvc<mint> c(r + 1); FOR(i, r + 1) { mint c0 = 0, c1 = 0; if (i < len(b)) c0 += mint(r - i) * b[i]; if (i < len(b)) c1 += b[i]; if (0 <= i - 1 && i - 1 < len(b)) c0 -= a[i - 1]; c[i] = {c0, c1}; } auto f = from_log_differentiation(r - 1, a, b); mint ANS = kth_term_of_p_recursive(f, c, K); return ANS; } #line 1 "library/mod/factorial998.hpp" // 1<<20 int factorial998table[1024] = {1,467742124,703158536,849331177,183632821,786787592,708945888,623860151,442444797,339076928,916211838,827641482,982515753,303461550,466748179,669060208,789885751,915736046,189957301,934038903,728735046,774755699,649374308,602288735,492352484,958678776,943233257,148504501,352124178,569334038,927469492,343841688,432351202,700916755,170721982,8283809,875807278,931632987,330722936,603566523,391470976,157944106,826756015,278928878,178606531,522053153,175494307,16217485,310769109,430912024,970167731,302127847,960178710,607169580,211863227,918097328,664502958,598427325,415194799,38321157,375608821,557298612,497769749,114695383,77784134,629192790,339438380,348348875,713806860,526342541,671850855,414726935,844082152,412454739,351143550,868784407,834684152,186057224,996072584,619190001,24770542,765280770,513490122,468949120,867194196,866447292,937135640,560788103,308335177,703539315,252044620,119916775,298069903,43651994,148641017,730387621,856452172,74265901,626807500,980602375,42825068,348086475,162321900,207340584,151258454,461547160,320321845,361026143,882876292,842563318,257705870,158156446,292795459,984763947,917068833,811332379,782439665,944504775,298167161,141501910,155584237,149720256,71954352,666430555,580966229,884747116,616367471,918981127,310328833,724405658,383796145,256700166,487819118,642491144,181867555,524937737,222137750,445244561,79921588,253457448,405659726,260707689,740044210,654653354,229885020,230551611,616689587,939003921,565960348,904184966,133298693,859220865,186139683,765071679,247651638,451157944,929341123,503724944,768266737,142218056,910573117,274579400,151387843,212671109,815271666,406331931,154251304,642676789,570372925,976277122,442985463,928799971,817581666,797627351,100113334,877639265,541537097,434482347,300960222,270085755,481153328,236088097,686884498,323505794,897572220,900787550,277507290,157634146,892066519,616420589,46056764,697140618,592483685,896871487,896388868,106444279,115102765,191484323,62322499,434613622,426026852,378184205,194359325,415197585,965735328,598860936,653751428,942602959,475099103,642401460,77868208,464952529,549976420,705774928,635299526,704085554,809044086,670938184,799176916,58985566,402328281,182103192,921913660,674272214,428301920,520916749,127424638,296779896,166780239,19634060,95873539,708947606,532272305,980167862,7015847,370183454,45567119,866949818,374428494,25583689,351370758,835388325,232690098,42002598,17055285,985022727,214528454,122907290,793349516,609331634,87133548,248246624,448572380,502875867,183097664,536117329,170926160,381772251,37038194,374439881,94285547,880631489,452052533,739811514,675382782,587926712,179133902,694266603,338843576,281485671,813341519,616512705,222785194,382494725,471654428,961907947,442140830,702296161,548575377,388901073,19119024,545916498,947169254,801677200,377657430,634980290,246239186,13175103,239754689,656729178,364003283,646568868,584909084,690387116,452007054,131381944,908149670,807287523,802277179,745423153,893994782,197548253,376096720,105840336,687751559,170787791,928507410,620382696,446955151,139665212,882526402,494793004,107171423,753993075,467588754,207595897,269813018,941027990,856873596,717085190,245280646,792026805,548741735,523767341,637697735,261200153,89666563,344573088,15832984,558492246,825051585,923222974,826620400,558080789,657328927,991078225,706029275,738905108,401212366,980043233,895405022,597894231,636951913,947342478,786075225,395095090,188433847,121279219,860403973,396099425,240442489,521535558,280382318,58023116,735594008,8696133,477645338,223630480,816606673,680021043,362424474,181667447,504295826,332167472,766361494,992840497,417671938,376941230,11880047,275790726,106186450,150546053,966438917,431896075,158021876,734833661,328332504,632143386,962477966,638741189,728804571,753715698,20536106,45105841,271673172,982138522,604809222,199722980,211807634,478008419,194715230,246865373,316443541,869035744,202922168,245262975,136244583,650969410,566222746,55188168,495968583,571946805,188658038,353720239,830419870,669127165,86710835,810103736,630008035,764354348,209246227,277861984,725469211,151404581,894191013,775554083,634671016,170299187,471849450,575347258,505276194,636730506,40086858,386228700,789875034,998219457,359035788,843760715,864829665,794240359,241486050,48334220,583177582,714653706,617669563,132782021,779225352,333301287,520569296,508276228,689073648,573645847,200419842,911561316,310562870,204959007,879280837,762843188,103128368,133300147,648946778,287218789,662474952,587555465,105622721,648151526,517033362,729251452,850555187,708613432,874408867,345608416,690718720,10813958,42384375,882264058,825490058,252850511,652942840,202604098,277615259,862885671,582470925,190843016,534488148,187675153,911660635,377262012,642854978,359397276,712333871,580131409,841639861,925383257,213683380,25291651,974815450,32032244,119030165,443676106,555727293,170519648,171131074,839941962,789829593,140975543,845347712,303299112,530420097,857005350,249174130,224087061,311280308,404814306,567648772,766512373,470895965,294358155,625218604,89534510,513216330,78173719,22818060,254922573,292417477,415060121,208989124,960117615,570018845,237661008,442774488,871349246,161574942,548661451,313471555,448096394,587422360,987939533,254478574,113844945,268886375,927289435,664834607,983476167,390569280,363763327,935767957,159015901,508613041,134148582,127417680,484767855,825835285,43847241,972918293,151969014,768480291,729490470,76727400,384998943,648970509,764966281,391326774,585299643,661473977,530021579,368308424,81083443,981417794,185781362,169555925,934957641,56005264,296483160,853982963,489694611,73207251,20297311,431253211,168162850,36271383,689526671,397669110,705876730,785504919,764896820,936514026,350141918,784778738,682324919,140913543,862125900,723248565,369074340,146936534,226913694,277886748,856792647,13654547,141461269,255233971,979535193,747662027,452683681,338311679,399620140,306913085,817524367,333578440,943193170,387930488,964713035,554372227,524201507,267870305,698863503,695139108,399857384,830659092,479624682,594238820,768224890,956955770,940576967,920740072,282055556,621677930,847367415,619094041,432519599,192780811,912052381,263304046,114280963,307107320,956809356,118706101,836710721,356893069,427113038,55360495,892694364,443807400,568616581,130165565,732273554,778059496,95936679,629634134,383940143,474733431,271200931,253893765,65679204,670721645,268831988,225698685,424701963,654858732,405695790,894299102,797306377,464723449,647679843,730366154,956550665,898568348,313188681,661403769,346715295,358990430,868898456,719464962,978551995,772931269,255694712,379904456,393101377,130818973,810783770,78951115,608848341,941552927,523163696,581658405,188869913,161971620,114600913,300038465,126906968,572973411,118017645,806069307,430432761,310699012,989119052,282768145,557792692,611036992,427168405,84497995,529589599,967936672,416953197,549641787,787274930,514952744,646568513,39329263,765390776,831388678,299074396,102522509,886062498,598990751,553048069,305737423,388746841,13007805,3445560,568306294,109543305,847740132,746222360,454654676,748993028,222910140,861308982,390243513,692742883,789475199,153430402,299806798,913070840,881332402,245792511,618823409,1817990,897836424,726794141,700802042,472214481,97004031,479899815,573979309,752576644,374801082,599964908,894966385,178103304,12240556,393873628,855241924,305678131,971858774,281586141,87362107,41844894,175133514,276243521,997376957,260427125,439339251,64661516,362212695,186181824,423316311,267640938,299252572,810040987,857956827,758991665,207700847,399398818,747579039,814755712,298373935,307448236,42074518,982127624,538863790,528558929,96501138,813255509,611769398,710541518,408153968,675346745,970094012,791931126,811516976,618049736,264048084,209805699,909045292,645349311,416989597,590393407,320547207,342653696,860169617,856611053,475149267,124801433,547187333,466598598,266454901,554467907,868909135,199244107,548833449,20952517,234169026,117025205,804238552,205574540,590283297,822322644,866010856,477388420,935768507,424373916,951967787,344871828,133969287,937034425,309380768,666909962,726492795,996576193,883938945,869749688,313581344,65216237,88860786,208895640,888760811,854567609,328142793,121852766,928690075,135269006,333105486,502240551,573712984,397698082,935117672,718828733,440474396,335628894,184935718,788258676,646732201,68099895,167036421,362572358,787671392,666366534,193503119,74429287,132805884,796935846,124574194,926012440,147265585,722608579,526866610,452261307,990444071,4595579,147427028,774597449,678783012,568563934,383628463,68242206,163493293,352851801,123192034,529859554,14733470,565063217,178575398,580871309,135817500,313966456,647215844,118781836,106243172,796669460,48496927,772979683,715961917,546863206,601711799,644312478,629259662,738295002,692301787,149995411,864799423,284186171,246177326,268779154,86400350,518698490,321709079,946212693,800553099,865864136,244789848,386206318,851633075,713794602,131117952,280474884,243820970,820033654,399700655,825581574,443639603,774376660,362476217,552383080,436759518,538430048,965968656,150434699,563163603,352073025,840124972,152029247,902082055,770264937,747653807,934664232,541451013,807031739,854866728,503502641,283479207,297947602,488469464,205196166,381583984,108455782,570592132,363674728,134077711,356931610,887112858,273780969,443297964,650953636,402662299,894089640,71844431,33030748,208583995,597099208,671156881,875032178,998244352,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; int factorial998(ll n) { constexpr int mod = 998244353; if (n >= mod) return 0; auto [q, r] = divmod(n, 1 << 20); ll x = factorial998table[q]; int s = q << 20; FOR(i, r) x = x * (s + i + 1) % mod; return x; } #line 7 "main.cpp" using mint = modint998; using poly = vc<mint>; const int mod = 998244353; mint naive(ll N, ll K) { poly f(3); f[0] = 1; f[1] = 2; f[2] = inv<mint>(2); f.resize(K + 1); f = fps_pow(f, N); return f[K] * fact<mint>(K); } mint solve_1(ll N, ll K) { return naive(N, K); } mint solve_2(ll N, ll K) { if (K >= mod) return 0; assert(K <= mod); poly f = {mint(2 * N), mint(N)}; poly g = {mint(1), mint(2), inv<mint>(2)}; mint ANS = mint(factorial998(K)) * from_log_differentiation_kth(K, f, g); return ANS; } void solve() { LL(N, K); print(solve_2(N, K)); } signed main() { INT(T); FOR(T) solve(); return 0; }