#ifndef HIDDEN_IN_VS // 折りたたみ用 // 警告の抑制 #define _CRT_SECURE_NO_WARNINGS // ライブラリの読み込み #include using namespace std; // 型名の短縮 using ll = long long; // -2^63 ~ 2^63 = 9 * 10^18(int は -2^31 ~ 2^31 = 2 * 10^9) using pii = pair; using pll = pair; using pil = pair; using pli = pair; using vi = vector; using vvi = vector; using vvvi = vector; using vvvvi = vector; using vl = vector; using vvl = vector; using vvvl = vector; using vvvvl = vector; using vb = vector; using vvb = vector; using vvvb = vector; using vc = vector; using vvc = vector; using vvvc = vector; using vd = vector; using vvd = vector; using vvvd = vector; template using priority_queue_rev = priority_queue, greater>; using Graph = vvi; // 定数の定義 const double PI = acos(-1); const vi DX = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左) const vi DY = { 0, 1, 0, -1 }; int INF = 1001001001; ll INFL = 4004004003104004004LL; // (int)INFL = 1010931620; double EPS = 1e-15; // 入出力高速化 struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp; // 汎用マクロの定義 #define all(a) (a).begin(), (a).end() #define sz(x) ((int)(x).size()) #define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), x)) #define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), x)) #define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");} #define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順 #define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順 #define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順 #define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能) #define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能) #define repb(set, d) for(int set = 0; set < (1 << int(d)); ++set) // d ビット全探索(昇順) #define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順) #define smod(n, m) ((((n) % (m)) + (m)) % (m)) // 非負mod #define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去 #define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了 #define inQ(x, y, u, l, d, r) ((u) <= (x) && (l) <= (y) && (x) < (d) && (y) < (r)) // 半開矩形内判定 // 汎用関数の定義 template inline ll pow(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; } template inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す) template inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す) template inline T get(T set, int i) { return (set >> i) & T(1); } // 演算子オーバーロード template inline istream& operator>>(istream& is, pair& p) { is >> p.first >> p.second; return is; } template inline istream& operator>>(istream& is, vector& v) { repea(x, v) is >> x; return is; } template inline vector& operator--(vector& v) { repea(x, v) --x; return v; } template inline vector& operator++(vector& v) { repea(x, v) ++x; return v; } #endif // 折りたたみ用 #if __has_include() #include using namespace atcoder; #ifdef _MSC_VER #include "localACL.hpp" #endif //using mint = modint1000000007; using mint = modint998244353; //using mint = modint; // mint::set_mod(mod); //using mint = static_modint<924844033>; namespace atcoder { inline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; } inline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; } } using vm = vector; using vvm = vector; using vvvm = vector; using vvvvm = vector; #endif #ifdef _MSC_VER // 手元環境(Visual Studio) #include "local.hpp" #else // 提出用(gcc) inline int popcount(int n) { return __builtin_popcount(n); } inline int popcount(ll n) { return __builtin_popcountll(n); } inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : -1; } inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : -1; } inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; } inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; } #define gcd __gcd #define dump(...) #define dumpel(v) #define dump_list(v) #define dump_mat(v) #define input_from_file(f) #define output_to_file(f) #define Assert(b) { if (!(b)) while (1) cout << "OLE"; } #endif mint easy(ll h, ll w, int n, ll k) { if (h > w) swap(h, w); mint dnm = (mint(h - k + 1) * (w - k + 1)).inv(); // dump(dnm); mint res = 0; if (h >= 2 * k) { if (w >= 2 * k) { ll len = (h - 2 * k) * 2 + (w - 2 * k) * 2; repi(i, 1, k) { res += (1 - (1 - i * k * dnm).pow(n)) * len; } res += (1 - (1 - k * k * dnm).pow(n)) * (h - 2 * k) * (w - 2 * k); h = 2 * k; w = 2 * k; } } else { if (w >= 2 * k) { rep(i, h) { ll u = max(0, i - k + 1); ll d = min(i, h - k); res += (1 - (1 - (d - u + 1) * k * dnm).pow(n)) * (w - 2 * k); } w = 2 * k; } } // dump(h, w, res); rep(i, h) rep(j, w) { ll u = max(0, i - k + 1); ll d = min(i, h - k); ll l = max(0, j - k + 1); ll r = min(j, w - k); // dump(i, j, ":", u, d, l, r); res += (1 - (1 - (d - u + 1) * (r - l + 1) * dnm).pow(n)); } return res; } //【形式的冪級数】 /* * MFPS() : O(1) * 零多項式 f = 0 で初期化する. * * MFPS(mint c0) : O(1) * 定数多項式 f = c0 で初期化する. * * MFPS(mint c0, int n) : O(n) * n 次未満の項をもつ定数多項式 f = c0 で初期化する. * * MFPS(vm c) : O(n) * f(z) = c[0] + c[1] z + ... + c[n - 1] z^(n-1) で初期化する. * * set_conv(vm(*CONV)(const vm&, const vm&)) : O(1) * 畳込み用の関数を CONV に設定する. * * c + f, f + c : O(1) f + g : O(n) * f - c : O(1) c - f, f - g, -f : O(n) * c * f, f * c : O(n) f * g : O(n log n) f * g_sp : O(n k)(k : g の項数) * f / c : O(n) f / g : O(n log n) f / g_sp : O(n k)(k : g の項数) * 形式的冪級数としての和,差,積,商の結果を返す. * g_sp はスパース多項式であり,{次数, 係数} の次数昇順の組の vector で表す. * 制約 : 商では g(0) != 0 * * MFPS f.inv(int d) : O(n log n) * 1 / f mod z^d を返す. * 制約 : f(0) != 0 * * MFPS f.quotient(MFPS g) : O(n log n) * MFPS f.reminder(MFPS g) : O(n log n) * pair f.quotient_remainder(MFPS g) : O(n log n) * 多項式としての f を g で割った商,余り,商と余りの組を返す. * 制約 : g の最高次の係数は 0 でない * * int f.deg(), int f.size() : O(1) * 多項式 f の次数[項数]を返す. * * MFPS::monomial(int d, mint c = 1) : O(d) * 単項式 c z^d を返す. * * mint f.assign(mint c) : O(n) * 多項式 f の不定元 z に c を代入した値を返す. * * f.resize(int d) : O(1) * mod z^d をとる. * * f.resize() : O(n) * 不要な高次の項を削る. * * f >> d, f << d : O(n) * 係数列を d だけ右[左]シフトした多項式を返す. * (右シフトは z^d の乗算,左シフトは z^d で割った商と等価) */ struct MFPS { using SMFPS = vector>; int n; // 係数の個数(次数 + 1) vm c; // 係数列 inline static vm(*CONV)(const vm&, const vm&) = convolution; // 畳込み用の関数 // コンストラクタ(0,定数,係数列で初期化) MFPS() : n(0) {} MFPS(mint c0) : n(1), c({ c0 }) {} MFPS(int c0) : n(1), c({ mint(c0) }) {} MFPS(mint c0, int d) : n(d), c(n) { c[0] = c0; } MFPS(int c0, int d) : n(d), c(n) { c[0] = c0; } MFPS(const vm& c_) : n(sz(c_)), c(c_) {} MFPS(const vi& c_) : n(sz(c_)), c(n) { rep(i, n) c[i] = c_[i]; } // 代入 MFPS(const MFPS& f) = default; MFPS& operator=(const MFPS& f) = default; MFPS& operator=(const mint& c0) { n = 1; c = { c0 }; return *this; } // 比較 bool operator==(const MFPS& g) const { return c == g.c; } bool operator!=(const MFPS& g) const { return c != g.c; } // アクセス inline mint const& operator[](int i) const { return c[i]; } inline mint& operator[](int i) { return c[i]; } // 次数 int deg() const { return n - 1; } int size() const { return n; } static void set_conv(vm(*CONV_)(const vm&, const vm&)) { // verify : https://atcoder.jp/contests/tdpc/tasks/tdpc_fibonacci CONV = CONV_; } // 加算 MFPS& operator+=(const MFPS& g) { if (n >= g.n) rep(i, g.n) c[i] += g.c[i]; else { rep(i, n) c[i] += g.c[i]; repi(i, n, g.n - 1) c.push_back(g.c[i]); n = g.n; } return *this; } MFPS operator+(const MFPS& g) const { return MFPS(*this) += g; } // 定数加算 MFPS& operator+=(const mint& sc) { if (n == 0) { n = 1; c = { sc }; } else { c[0] += sc; } return *this; } MFPS operator+(const mint& sc) const { return MFPS(*this) += sc; } friend MFPS operator+(const mint& sc, const MFPS& f) { return f + sc; } MFPS& operator+=(const int& sc) { *this += mint(sc); return *this; } MFPS operator+(const int& sc) const { return MFPS(*this) += sc; } friend MFPS operator+(const int& sc, const MFPS& f) { return f + sc; } // 減算 MFPS& operator-=(const MFPS& g) { if (n >= g.n) rep(i, g.n) c[i] -= g.c[i]; else { rep(i, n) c[i] -= g.c[i]; repi(i, n, g.n - 1) c.push_back(-g.c[i]); n = g.n; } return *this; } MFPS operator-(const MFPS& g) const { return MFPS(*this) -= g; } // 定数減算 MFPS& operator-=(const mint& sc) { *this += -sc; return *this; } MFPS operator-(const mint& sc) const { return MFPS(*this) -= sc; } friend MFPS operator-(const mint& sc, const MFPS& f) { return -(f - sc); } MFPS& operator-=(const int& sc) { *this += -sc; return *this; } MFPS operator-(const int& sc) const { return MFPS(*this) -= sc; } friend MFPS operator-(const int& sc, const MFPS& f) { return -(f - sc); } // 加法逆元 MFPS operator-() const { return MFPS(*this) *= -1; } // 定数倍 MFPS& operator*=(const mint& sc) { rep(i, n) c[i] *= sc; return *this; } MFPS operator*(const mint& sc) const { return MFPS(*this) *= sc; } friend MFPS operator*(const mint& sc, const MFPS& f) { return f * sc; } MFPS& operator*=(const int& sc) { *this *= mint(sc); return *this; } MFPS operator*(const int& sc) const { return MFPS(*this) *= sc; } friend MFPS operator*(const int& sc, const MFPS& f) { return f * sc; } // 右からの定数除算 MFPS& operator/=(const mint& sc) { *this *= sc.inv(); return *this; } MFPS operator/(const mint& sc) const { return MFPS(*this) /= sc; } MFPS& operator/=(const int& sc) { *this /= mint(sc); return *this; } MFPS operator/(const int& sc) const { return MFPS(*this) /= sc; } // 積 MFPS& operator*=(const MFPS& g) { c = CONV(c, g.c); n = sz(c); return *this; } MFPS operator*(const MFPS& g) const { return MFPS(*this) *= g; } // 除算 MFPS inv(int d) const { // 参考:https://nyaannyaan.github.io/library/fps/formal-power-series.hpp // verify : https://judge.yosupo.jp/problem/inv_of_formal_power_series //【方法】 // 1 / f mod z^d を求めることは, // f g = 1 (mod z^d) // なる g を求めることである. // この d の部分を 1, 2, 4, ..., 2^i と倍々にして求めていく. // // d = 1 のときについては // g = 1 / f[0] (mod z^1) // である. // // 次に, // g = h (mod z^k) // が求まっているとして // g mod z^(2 k) // を求める.最初の式を変形していくことで // g - h = 0 (mod z^k) // ⇒ (g - h)^2 = 0 (mod z^(2 k)) // ⇔ g^2 - 2 g h + h^2 = 0 (mod z^(2 k)) // ⇒ f g^2 - 2 f g h + f h^2 = 0 (mod z^(2 k)) // ⇔ g - 2 h + f h^2 = 0 (mod z^(2 k))  (f g = 1 (mod z^d) より) // ⇔ g = (2 - f h) h (mod z^(2 k)) // を得る. // // この手順を d ≦ 2^i となる i まで繰り返し,d 次以上の項を削除すればよい. Assert(!c.empty()); Assert(c[0] != 0); MFPS g(c[0].inv()); for (int k = 1; k < d; k *= 2) { int len = max(min(2 * k, d), 1); MFPS tmp(0, len); rep(i, min(len, n)) tmp[i] = -c[i]; // -f tmp *= g; // -f h tmp.resize(len); tmp[0] += 2; // 2 - f h g *= tmp; // (2 - f h) h g.resize(len); } return g; } MFPS& operator/=(const MFPS& g) { return *this *= g.inv(max(n, g.n)); } MFPS operator/(const MFPS& g) const { return MFPS(*this) /= g; } // 余り付き除算 MFPS quotient(const MFPS& g) const { // 参考 : https://nyaannyaan.github.io/library/fps/formal-power-series.hpp // verify : https://judge.yosupo.jp/problem/division_of_polynomials //【方法】 // f(x) = g(x) q(x) + r(x) となる q(x) を求める. // f の次数は n - 1, g の次数は m - 1 とする.(n >= m) // 従って q の次数は n - m,r の次数は m - 2 となる. // // f^R で f の係数列を逆順にした多項式を表す.すなわち // f^R(x) := f(1/x) x^(n-1) // である.他の多項式も同様とする. // // 最初の式で x → 1/x と置き換えると, // f(1/x) = g(1/x) q(1/x) + r(1/x) // ⇔ f(1/x) x^(n-1) = g(1/x) q(1/x) x^(n-1) + r(1/x) x^(n-1) // ⇔ f(1/x) x^(n-1) = g(1/x) x^(m-1) q(1/x) x^(n-m) + r(1/x) x^(m-2) x^(n-m+1) // ⇔ f^R(x) = g^R(x) q^R(x) + r^R(x) x^(n-m+1) // ⇒ f^R(x) = g^R(x) q^R(x) (mod x^(n-m+1)) // ⇒ q^R(x) = f^R(x) / g^R(x) (mod x^(n-m+1)) // を得る. // // これで q を mod x^(n-m+1) で正しく求めることができることになるが, // q の次数は n - m であったから,q 自身を正しく求めることができた. if (n < g.n) return MFPS(); return ((this->rev() / g.rev()).resize(n - g.n + 1)).rev(); } MFPS reminder(const MFPS& g) const { // verify : https://judge.yosupo.jp/problem/division_of_polynomials return (*this - this->quotient(g) * g).resize(g.n - 1); } pair quotient_remainder(const MFPS& g) const { // verify : https://judge.yosupo.jp/problem/division_of_polynomials pair res; res.first = this->quotient(g); res.second = (*this - res.first * g).resize(g.n - 1); return res; } // スパース積 MFPS& operator*=(const SMFPS& g) { // g の定数項だけ例外処理 auto it0 = g.begin(); mint g0 = 0; if (it0->first == 0) { g0 = it0->second; it0++; } // 後ろからインライン配る DP repir(i, n - 1, 0) { // 上位項に係数倍して配っていく. for (auto it = it0; it != g.end(); it++) { auto [j, gj] = *it; if (i + j >= n) break; c[i + j] += c[i] * gj; } // 定数項は最後に配るか消去しないといけない. c[i] *= g0; } return *this; } MFPS operator*(const SMFPS& g) const { return MFPS(*this) *= g; } // スパース商 MFPS& operator/=(const SMFPS& g) { // g の定数項だけ例外処理 auto it0 = g.begin(); Assert(it0->first == 0 && it0->second != 0); mint g0_inv = it0->second.inv(); it0++; // 前からインライン配る DP(後ろに累積効果あり) rep(i, n) { // 定数項は最初に配らないといけない. c[i] *= g0_inv; // 上位項に係数倍して配っていく. for (auto it = it0; it != g.end(); it++) { auto [j, gj] = *it; if (i + j >= n) break; c[i + j] -= c[i] * gj; } } return *this; } MFPS operator/(const SMFPS& g) const { return MFPS(*this) /= g; } // 係数反転 MFPS rev() const { MFPS h = *this; reverse(all(h.c)); return h; } // 単項式 static MFPS monomial(int d, mint coef = 1) { MFPS mono(0, d + 1); mono[d] = coef; return mono; } // 不要な高次項の除去 MFPS& resize() { // 最高次の係数が非 0 になるまで削る. while (n > 0 && c[n - 1] == 0) { c.pop_back(); n--; } return *this; } // x^d 以上の項を除去する. MFPS& resize(int d) { n = d; c.resize(d); return *this; } // 不定元への代入 mint assign(const mint& x) const { mint val = 0; repir(i, n - 1, 0) val = val * x + c[i]; return val; } // 係数のシフト MFPS& operator>>=(int d) { n += d; c.insert(c.begin(), d, 0); return *this; } MFPS& operator<<=(int d) { n -= d; if (n <= 0) { c.clear(); n = 0; } else c.erase(c.begin(), c.begin() + d); return *this; } MFPS operator>>(int d) const { return MFPS(*this) >>= d; } MFPS operator<<(int d) const { return MFPS(*this) <<= d; } #ifdef _MSC_VER friend ostream& operator<<(ostream& os, const MFPS& f) { if (f.n == 0) os << 0; else { rep(i, f.n) { os << f[i].val() << "z^" << i; if (i < f.n - 1) os << " + "; } } return os; } #endif }; //【階乗など(法が大きな素数)】 /* * Factorial_mint(int N) : O(n) * N まで計算可能として初期化する. * * mint fact(int n) : O(1) * n! を返す. * * mint fact_inv(int n) : O(1) * 1/n! を返す(n が負なら 0 を返す) * * mint inv(int n) : O(1) * 1/n を返す. * * mint perm(int n, int r) : O(1) * 順列の数 nPr を返す. * * mint bin(int n, int r) : O(1) * 二項係数 nCr を返す. * * mint mul(vi rs) : O(|rs|) * 多項係数 nC[rs] を返す.(n = Σrs) */ class Factorial_mint { int n_max; // 階乗と階乗の逆数の値を保持するテーブル vm fac, fac_inv; public: // n! までの階乗とその逆数を前計算しておく.O(n) Factorial_mint(int n) : n_max(n), fac(n + 1), fac_inv(n + 1) { // verify : https://atcoder.jp/contests/dwacon6th-prelims/tasks/dwacon6th_prelims_b fac[0] = 1; repi(i, 1, n) fac[i] = fac[i - 1] * i; fac_inv[n] = fac[n].inv(); repir(i, n - 1, 0) fac_inv[i] = fac_inv[i + 1] * (i + 1); } Factorial_mint() : n_max(0) {} // ダミー // n! を返す. mint fact(int n) const { // verify : https://atcoder.jp/contests/dwacon6th-prelims/tasks/dwacon6th_prelims_b Assert(0 <= n && n <= n_max); return fac[n]; } // 1/n! を返す(n が負なら 0 を返す) mint fact_inv(int n) const { // verify : https://atcoder.jp/contests/abc289/tasks/abc289_h Assert(n <= n_max); if (n < 0) return 0; return fac_inv[n]; } // 1/n を返す. mint inv(int n) const { // verify : https://atcoder.jp/contests/exawizards2019/tasks/exawizards2019_d Assert(0 < n && n <= n_max); return fac[n - 1] * fac_inv[n]; } // 順列の数 nPr を返す. mint perm(int n, int r) const { // verify : https://atcoder.jp/contests/abc172/tasks/abc172_e Assert(n <= n_max); if (r < 0 || n - r < 0) return 0; return fac[n] * fac_inv[n - r]; } // 二項係数 nCr を返す. mint bin(int n, int r) const { // verify : https://atcoder.jp/contests/abc034/tasks/abc034_c Assert(n <= n_max); if (r < 0 || n - r < 0) return 0; return fac[n] * fac_inv[r] * fac_inv[n - r]; } // 多項係数 nC[rs] を返す. mint mul(const vi& rs) const { // verify : https://yukicoder.me/problems/no/2141 if (*min_element(all(rs)) < 0) return 0; int n = accumulate(all(rs), 0); Assert(n <= n_max); mint res = fac[n]; repe(r, rs) res *= fac_inv[r]; return res; } }; //【平行移動】O(n log n) /* * f(z + c) を返す. * * 制約 : fm は deg(f) までの階乗計算が可能であること. */ MFPS taylor_shift(const MFPS& f, mint c, const Factorial_mint& fm) { // 参考 : https://nyaannyaan.github.io/library/fps/taylor-shift.hpp.html // verify : https://judge.yosupo.jp/problem/polynomial_taylor_shift //【方法】 // f(x) = Σn=[0..N] f[n] x^n // と表されるとすると, // f(x + c) // = Σn=[0..N] f[n] (x + c)^n // = Σn=[0..N] f[n] Σr=[0..n] nCr c^(n-r) x^r (二項定理) // = Σn=[0..N] Σr=[0..n] f[n] n! / ((n-r)! r!) c^(n-r) x^r // = Σr=[0..N] Σn=[r..N] f[n] n! / ((n-r)! r!) c^(n-r) x^r (和の順序交換) // = Σr=[0..N] x^r / r! Σn=[r..N] (c^(n-r) / (n-r)!) n! f[n] // = Σr=[0..N] x^r / r! Σm=[0..N-r] (c^(N-m-r) / (N-m-r)!) (N-m)! f[N-m] (m = N - n) // = Σj=[0..N] x^(N-j) / (N-j)! Σm=[0..j] (c^(j-m) / (j-m)!) (N-m)! f[N-m] (j = N - r) // と書き直せる. // // よって // g(x) = Σn=[0..N] (c^n / n!) x^n // h(x) = Σn=[0..N] (N-n)! f[N-n] x^n // とおくと, // f(x + c) // = Σj=[0..N] x^(N-j) / (N-j)! (g*h)[j] // = Σj=[0..N] x^j / j! (g*h)[N-j] // と表される. int n = f.deg() + 1; MFPS g(1); g.resize(n); repi(i, 1, n - 1) g[i] = g[i - 1] * c * fm.inv(i); MFPS h(f); rep(i, n) h[i] *= fm.fact(i); h = h.rev(); MFPS fs = (g * h).resize(n); fs = fs.rev(); rep(i, n) fs[i] *= fm.fact_inv(i); return fs; } // 低次数の項の無視とテイラーシフトとの噛み合いが悪い. // (z-c)^n みたいなのを掛ければ調整はできるが,その n が大きくて困っているので本末転倒. vm powered_sum_WA(ll n, int m, const Factorial_mint& fm) { MFPS f(vm({ 1, 0 })), g(vm({ -1, 1 })), resf(0), resg(1); m++; while (n > 0) { if (n & 1) { resf = taylor_shift(resf, -g.deg(), fm) * g + taylor_shift(resg, -f.deg(), fm) * f; resg = taylor_shift(resg, -g.deg(), fm) * g; if (sz(resf) > m) { resf <<= sz(resf) - m; resg <<= sz(resg) - m; } } f = taylor_shift(f, -g.deg(), fm) * g + taylor_shift(g, -f.deg(), fm) * f; g = taylor_shift(g, -g.deg(), fm) * g; if (sz(f) > m) { f <<= sz(f) - m; g <<= sz(g) - m; } n /= 2; } dump(resf); dump(resg); resf = resf.rev(); resg = resg.rev(); resf.resize(m); resg.resize(m); auto res = (resf / resg) << 1; res.resize(m - 1); return res.c; } //【累乗(mint 利用)】 /* * Pow_mint(T B, int n) : O(n) * 底を B とし,B^(-n) から B^n まで計算可能として初期化する. * 制約 : B は mint の法と互いに素 * * mint [](int i) : O(1) * B^i を返す. */ template class Pow_mint { int n; vm powB, powB_inv; public: Pow_mint(T B, int n) : n(n) { // verify : https://atcoder.jp/contests/arc116/tasks/arc116_b // B の累乗を計算する. powB.resize(n + 1); powB[0] = 1; rep(i, n) powB[i + 1] = powB[i] * B; // B の逆元の累乗を計算する. mint invB = mint(1) / B; powB_inv.resize(n + 1); powB_inv[0] = 1; rep(i, n) powB_inv[i + 1] = powB_inv[i] * invB; }; Pow_mint() : n(0) {} // B^i を返す. mint const& operator[](int i) const { // verify : https://atcoder.jp/contests/arc116/tasks/arc116_b Assert(abs(i) <= n); return i >= 0 ? powB[i] : powB_inv[-i]; } #ifdef _MSC_VER friend ostream& operator<<(ostream& os, const Pow_mint& pw) { os << pw.powB << endl; os << pw.powB_inv << endl; return os; } #endif }; //【mint → 有理数】(実験用) /* * x を分母と分子の絶対値が v_max 以下の有理数表示に変換する(不可能ならそのまま) */ string mint_to_frac(mint x, int v_max = 31595) { // verify : https://www.codechef.com/problems/SUMOVERALL repi(dnm, 1, v_max) { int num = (x * dnm).val(); if (num == 0) { return "0"; } if (num <= v_max) { if (dnm == 1) return to_string(num); return to_string(num) + "/" + to_string(dnm); } if (mint::mod() - num <= v_max) { if (dnm == 1) return "-" + to_string(mint::mod() - num); return "-" + to_string(mint::mod() - num) + "/" + to_string(dnm); } } return to_string(x.val()); } //【累乗和(連続整数,次数ごと)】O(m log m) /* * 各 j∈[0..m) について,Σi∈[0..n) i^j を格納したリストを返す(0^0 = 1 とする) * * 制約:fm は m! まで計算可能 */ vm powered_sum(ll n, int m, const Factorial_mint& fm) { // 参考:https://maspypy.com/%e5%a4%9a%e9%a0%85%e5%bc%8f%e3%83%bb%e5%bd%a2%e5%bc%8f%e7%9a%84%e3%81%b9%e3%81%8d%e7%b4%9a%e6%95%b0-%e9%ab%98%e9%80%9f%e3%81%ab%e8%a8%88%e7%ae%97%e3%81%a7%e3%81%8d%e3%82%8b%e3%82%82%e3%81%ae#toc34 //【方法】 // 指数関数のテイラー展開の式より // Σi∈[0..n) exp(i z) // = Σi∈[0..n) Σ_j (i^j / j!) z^j // = Σ_j ((Σi∈[0..n) i^j) / j!) z^j // となるので,この係数から求める累乗和が得られる.等比数列の和の公式より // Σi∈[0..n) exp(i z) // = Σi∈[0..n) exp(z)^i // = (1 - exp(n z)) / (1 - exp(z)) // として計算すれば高速に係数が得られる. vm num(m), dnm(m); mint pow_n = n; rep(j, m) { num[j] = -fm.fact_inv(j + 1) * pow_n; dnm[j] = -fm.fact_inv(j + 1); pow_n *= n; } auto f = MFPS(num) / MFPS(dnm); vm res(m); rep(j, m) res[j] = f[j] * fm.fact(j); return res; } mint solve(ll h, ll w, int n, ll k) { ll h1 = min(h - k, k - 1); ll h0 = h - 2 * h1; ll w1 = min(w - k, k - 1); ll w0 = w - 2 * w1; dump(h1, h0, w1, w0); Factorial_mint fm(2 * n); auto ph = powered_sum(h1 + 1, n + 1, fm); auto pw = powered_sum(w1 + 1, n + 1, fm); ph[0]--; pw[0]--; dump(ph); dump(pw); mint dnm_inv = mint((h - k + 1) * (w - k + 1)).inv(); dump(mint_to_frac(dnm_inv)); Pow_mint nD(-dnm_inv, n); Pow_mint nhD(-(h1 + 1) * dnm_inv, n); Pow_mint nwD(-(w1 + 1) * dnm_inv, n); Pow_mint nhwD(-(w1 + 1) * (h1 + 1) * dnm_inv, n); mint res = 0; repi(k, 0, n) res += fm.bin(n, k) * nD[k] * ph[k] * pw[k] * 4; dump(mint_to_frac(res)); repi(k, 0, n) res += fm.bin(n, k) * nwD[k] * ph[k] * w0 * 2; dump(mint_to_frac(res)); repi(k, 0, n) res += fm.bin(n, k) * nhD[k] * pw[k] * h0 * 2; dump(mint_to_frac(res)); repi(k, 0, n) res += fm.bin(n, k) * nhwD[k] * h0 * w0; dump(mint_to_frac(res)); res = h * w - res; return res; } void bug_find() { #ifdef _MSC_VER // 合わない入力例を見つける. mt19937_64 mt; mt.seed((int)time(NULL)); uniform_int_distribution rnd(0LL, 1LL << 60); mute_dump = true; rep(hoge, 1000) { ll h = rnd(mt) % 20 + 1; ll w = rnd(mt) % 7 + 1; int n = rnd(mt) % 10 + 1; ll k = rnd(mt) % min(h, w) + 1; auto res_naive = easy(h, w, n, k); auto res_solve = solve(h, w, n, k); if (res_naive != res_solve) { cout << "----------error!----------" << endl; cout << "input:" << endl; cout << h << " " << w << " " << n << " " << k << endl; cout << "results:" << endl; cout << res_naive << endl; cout << res_solve << endl; cout << "--------------------------" << endl; } } mute_dump = false; exit(0); #endif } int main() { input_from_file("input.txt"); // output_to_file("output.txt"); bug_find(); ll h, w, k; int n; cin >> h >> w >> n >> k; dump(h, w, n, k); dump("----"); dump(easy(h, w, n, k)); dump("-----"); auto res = solve(h, w, n, k); cout << res << endl; }