結果

問題 No.2459 Stampaholic (Hard)
ユーザー ForestedForested
提出日時 2023-09-01 23:37:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 681 ms / 4,000 ms
コード長 7,110 bytes
コンパイル時間 3,141 ms
コンパイル使用メモリ 176,152 KB
実行使用メモリ 29,372 KB
最終ジャッジ日時 2024-06-11 05:49:28
合計ジャッジ時間 11,567 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 670 ms
29,240 KB
testcase_02 AC 126 ms
8,852 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 254 ms
13,956 KB
testcase_09 AC 145 ms
9,336 KB
testcase_10 AC 588 ms
26,932 KB
testcase_11 AC 307 ms
15,876 KB
testcase_12 AC 657 ms
28,916 KB
testcase_13 AC 596 ms
26,684 KB
testcase_14 AC 155 ms
9,804 KB
testcase_15 AC 670 ms
29,248 KB
testcase_16 AC 662 ms
29,372 KB
testcase_17 AC 681 ms
29,248 KB
testcase_18 AC 666 ms
29,244 KB
testcase_19 AC 673 ms
29,372 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 532 ms
24,900 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifndef LOCAL
#define FAST_IO
#endif

// ============
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define OVERRIDE(a, b, c, d, ...) d
#define REP2(i, n) for (i32 i = 0; i < (i32)(n); ++i)
#define REP3(i, m, n) for (i32 i = (i32)(m); i < (i32)(n); ++i)
#define REP(...) OVERRIDE(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)
#define PER(i, n) for (i32 i = (i32)(n) - 1; i >= 0; --i)
#define ALL(x) begin(x), end(x)

using namespace std;

using u32 = unsigned int;
using u64 = unsigned long long;
using i32 = signed int;
using i64 = signed long long;
using f64 = double;
using f80 = long double;

template <typename T>
using Vec = vector<T>;

template <typename T>
bool chmin(T &x, const T &y) {
    if (x > y) {
        x = y;
        return true;
    }
    return false;
}
template <typename T>
bool chmax(T &x, const T &y) {
    if (x < y) {
        x = y;
        return true;
    }
    return false;
}

#ifdef INT128

using u128 = __uint128_t;
using i128 = __int128_t;

istream &operator>>(istream &is, i128 &x) {
    i64 v;
    is >> v;
    x = v;
    return is;
}
ostream &operator<<(ostream &os, i128 x) {
    os << (i64)x;
    return os;
}
istream &operator>>(istream &is, u128 &x) {
    u64 v;
    is >> v;
    x = v;
    return is;
}
ostream &operator<<(ostream &os, u128 x) {
    os << (u64)x;
    return os;
}

#endif

[[maybe_unused]] constexpr i32 INF = 1000000100;
[[maybe_unused]] constexpr i64 INF64 = 3000000000000000100;
struct SetUpIO {
    SetUpIO() {
#ifdef FAST_IO
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
#endif
        cout << fixed << setprecision(15);
    }
} set_up_io;
// ============

#ifdef DEBUGF
#else
#define DBG(x) (void)0
#endif

// ============

// ============

#include <algorithm>
#include <iostream>
#include <atcoder/convolution>

namespace poly {

using Mint = atcoder::modint998244353;
using Poly = std::vector<Mint>;

Poly add(Poly f, Poly g) {
    if (f.size() < g.size()) {
        std::swap(f, g);
    }
    for (int i = 0; i < (int)g.size(); ++i) {
        f[i] += g[i];
    }
    return f;
}

Poly sub(Poly f, Poly g) {
    if (f.size() < g.size()) {
        std::swap(f, g);
    }
    for (int i = 0; i < (int)g.size(); ++i) {
        f[i] -= g[i];
    }
    return f;
}

Poly mul(const Poly &f, const Poly &g) {
    return atcoder::convolution(f, g);
}

void dft(Poly &f) {
    atcoder::internal::butterfly(f);
}

void idft(Poly &f) {
    atcoder::internal::butterfly_inv(f);
    int ctz = __builtin_ctz((int)f.size());
    Mint inv = Mint::raw(Mint::mod() - (Mint::mod() >> ctz));
    for (Mint &cf : f) {
        cf *= inv;
    }
}

} // namespace poly
// ============

namespace poly {

Poly fps_inv(const Poly &f, int sz = -1) {
    assert(!f.empty() && f[0] != Mint());
    if (sz == -1) {
        sz = (int)f.size();
    }
    assert(sz >= 0);
    Poly g(1, f[0].inv());
    while ((int)g.size() < sz) {
        Poly fg;
        if (2 * g.size() <= f.size()) {
            fg = Poly(f.begin(), f.begin() + 2 * g.size());
        } else {
            fg = f;
            fg.resize(2 * g.size());
        }
        dft(fg);
        Poly dft_g = g;
        dft_g.resize(2 * g.size());
        dft(dft_g);
        for (int i = 0; i < (int)dft_g.size(); ++i) {
            fg[i] *= dft_g[i];
        }
        idft(fg);
        std::fill(fg.begin(), fg.begin() + g.size(), Mint());
        dft(fg);
        for (int i = 0; i < (int)dft_g.size(); ++i) {
            fg[i] *= dft_g[i];
        }
        idft(fg);
        g.resize(2 * g.size());
        for (int i = (int)g.size() / 2; i < (int)g.size(); ++i) {
            g[i] = -fg[i];
        }
    }
    g.resize(sz);
    return g;
}

} // namespace poly
// ============
// ============

// ============
// ============

namespace poly {

class Factorial {
    std::vector<Mint> fac;
    std::vector<Mint> ifac;
    
public:
    Factorial() : fac(1, Mint::raw(1)), ifac(1, Mint::raw(1)) {}
    
    void reserve(int n) {
        int m = (int)fac.size() - 1;
        if (n <= m) {
            return;
        }
        fac.resize(n + 1);
        for (int i = m + 1; i <= n; ++i) {
            fac[i] = fac[i - 1] * Mint::raw(i);
        }
        ifac.resize(n + 1);
        ifac[n] = fac[n].inv();
        for (int i = n - 1; i >= m; --i) {
            ifac[i] = ifac[i + 1] * Mint::raw(i + 1);
        }
    }
    
    Mint fact(int n) const {
        assert(n < (int)fac.size());
        return fac[n];
    }
    
    Mint inv_fact(int n) const {
        assert(n < (int)fac.size());
        return ifac[n];
    }
    
    Mint inv_n(int n) const {
        assert(n < (int)fac.size());
        return ifac[n] * fac[n - 1];
    }
    
    Mint binom(int n, int r) const {
        assert(n < (int)fac.size());
        return fac[n] * ifac[r] * ifac[n - r];
    }
} factorial;

} // namespace poly
// ============

using namespace poly;

Vec<Mint> enumerate_pow_sum(Mint x, i32 deg_max) {
    x += Mint(1);
    Vec<Mint> f(deg_max + 1), g(deg_max + 1);
    Mint p = x;
    REP(i, deg_max + 1) {
        f[i] = p * factorial.inv_fact(i + 1);
        g[i] = factorial.inv_fact(i + 1);
        p *= x;
    }
    Vec<Mint> inv_g = fps_inv(g);
    Vec<Mint> f_div_g = mul(f, inv_g);
    f_div_g.resize(deg_max + 1);
    REP(i, deg_max + 1) {
        f_div_g[i] *= factorial.fact(i);
    }
    return f_div_g;
}

int main() {
    i32 h, w, n, k;
    cin >> h >> w >> n >> k;
    
    factorial.reserve(n + 10);
    
    Mint sum;
    
    i32 h_max = min({(h + 1) / 2, h - k + 1, k});
    i32 w_max = min({(w + 1) / 2, w - k + 1, k});
    i32 h_max_cnt = h - 2 * (h_max - 1);
    i32 w_max_cnt = w - 2 * (w_max - 1);
    
    Mint tot = Mint(h - k + 1) * Mint(w - k + 1);
    Vec<Mint> h_side = enumerate_pow_sum(Mint(h_max - 1), n);
    Vec<Mint> w_side = enumerate_pow_sum(Mint(w_max - 1), n);
    h_side[0] -= Mint(1);
    w_side[0] -= Mint(1);
    DBG(h_side);
    DBG(w_side);
    {
        REP(i, n + 1) {
            sum += factorial.binom(n, i) * tot.pow(i) * h_side[n - i] * w_side[n - i] * Mint(-1).pow(n - i) * Mint(4);
        }
        DBG(sum);
    }
    {
        REP(i, n + 1) {
            sum += factorial.binom(n, i) * tot.pow(i) * h_side[n - i] * Mint(w_max).pow(n - i) * Mint(w_max_cnt) * Mint(-1).pow(n - i) * Mint(2);
        }
        DBG(sum);
    }
    {
        REP(i, n + 1) {
            sum += factorial.binom(n, i) * tot.pow(i) * w_side[n - i] * Mint(h_max).pow(n - i) * Mint(h_max_cnt) * Mint(-1).pow(n - i) * Mint(2);
        }
        DBG(sum);
    }
    {
        Mint si = tot - Mint(h_max) * Mint(w_max);
        sum += si.pow(n) * Mint(h_max_cnt) * Mint(w_max_cnt);
        DBG(sum);
    }
    
    sum /= (Mint(h - k + 1) * Mint(w - k + 1)).pow(n);
    
    Mint ans = Mint(h) * Mint(w) - sum;
    
    cout << ans.val() << '\n';
}
0