結果

問題 No.2459 Stampaholic (Hard)
ユーザー KudeKude
提出日時 2023-09-02 00:06:57
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 655 ms / 4,000 ms
コード長 3,821 bytes
コンパイル時間 3,575 ms
コンパイル使用メモリ 283,500 KB
実行使用メモリ 35,068 KB
最終ジャッジ日時 2023-09-02 00:07:11
合計ジャッジ時間 11,860 ms
ジャッジサーバーID
(参考情報)
judge16 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
11,192 KB
testcase_01 AC 622 ms
35,068 KB
testcase_02 AC 137 ms
16,172 KB
testcase_03 AC 15 ms
11,304 KB
testcase_04 AC 14 ms
11,244 KB
testcase_05 AC 15 ms
11,300 KB
testcase_06 AC 15 ms
11,264 KB
testcase_07 AC 15 ms
11,324 KB
testcase_08 AC 264 ms
21,140 KB
testcase_09 AC 147 ms
16,568 KB
testcase_10 AC 571 ms
32,848 KB
testcase_11 AC 300 ms
22,520 KB
testcase_12 AC 638 ms
34,624 KB
testcase_13 AC 568 ms
32,868 KB
testcase_14 AC 157 ms
17,052 KB
testcase_15 AC 655 ms
34,988 KB
testcase_16 AC 621 ms
34,984 KB
testcase_17 AC 637 ms
34,936 KB
testcase_18 AC 621 ms
34,928 KB
testcase_19 AC 644 ms
34,924 KB
testcase_20 AC 14 ms
11,196 KB
testcase_21 AC 532 ms
31,504 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:49:6: 警告: ‘{anonymous}::mint {anonymous}::perm(int, int)’ defined but not used [-Wunused-function]
   49 | mint perm(int n, int k) {
      |      ^~~~
main.cpp:48:6: 警告: ‘{anonymous}::mint {anonymous}::fact(int)’ defined but not used [-Wunused-function]
   48 | mint fact(int n) {return Fact[n];}
      |      ^~~~
main.cpp:44:6: 警告: ‘{anonymous}::mint {anonymous}::icomb(int, int)’ defined but not used [-Wunused-function]
   44 | mint icomb(int n, int k) {
      |      ^~~~~

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using mint = modint998244353;

constexpr int FACT_SIZE = 1000000;
mint Fact[FACT_SIZE + 1];
mint iFact[FACT_SIZE + 1];
const auto fact_init = [] {
    Fact[0] = mint::raw(1);
    for(int i = 1; i <= FACT_SIZE; ++i) {
        Fact[i] = Fact[i-1] * i;
    }
    iFact[FACT_SIZE] = Fact[FACT_SIZE].inv();
    for(int i = FACT_SIZE; i; --i) {
        iFact[i-1] = iFact[i] * i;
    }
    return false;
}();

mint comb(int n, int k) {
    if (k == 0) return mint::raw(1);
    assert(n >= 0 && k >= 0);
    if (k > n) return mint::raw(0);
    return Fact[n] * iFact[n - k] * iFact[k];
}

mint icomb(int n, int k) {
    return iFact[n] * Fact[n - k] * Fact[k];
}

mint fact(int n) {return Fact[n];}
mint perm(int n, int k) {
    assert(0 <= n);
    return Fact[n] * iFact[n - k];
}


vector<mint> fps_inverse(vector<mint> f, int precision) {
  int n = f.size();
  assert(n >= 1 && f[0] != 0);
  int len = 1;
  const int z = (int)std::bit_ceil((unsigned int)(precision));
  vector<mint> g{f[0].inv()};
  const mint inv4 = mint(4).inv();
  mint inv4k = 1;
  while (len < z) {
    int nlen = 2 * len;
    vector<mint> ft(f.begin(), f.begin() + min(n, nlen));
    ft.resize(nlen);
    internal::butterfly(ft);
    vector<mint> gt = g;
    gt.resize(nlen);
    internal::butterfly(gt);
    for (int i = 0; i < nlen; i++) ft[i] *= gt[i];
    internal::butterfly_inv(ft);
    ft.erase(ft.begin(), ft.begin() + len);
    ft.resize(nlen);
    internal::butterfly(ft);
    for (int i = 0; i < nlen; i++) ft[i] *= gt[i];
    internal::butterfly_inv(ft);
    inv4k *= inv4;
    mint c = -inv4k;
    for (int i = 0; i < len; i++) g.emplace_back(c * ft[i]);
    len = nlen;
  }
  g.resize(precision);
  return g;
}


vector<mint> kth_power_sums(mint n, int m) {
  // e^0+e^x+e^2x+...+e^(n-1)x
  // = ((e^nx-1)/x) / ((e^x-1)/x)
  // =: f / g
  // init_fact(m + 1);
  vector<mint> f(m), g(m);
  mint pow_n = n;
  for (int i = 0; i < m; i++) {
    f[i] = pow_n * iFact[i + 1];
    g[i] = iFact[i + 1];
    pow_n *= n;
  }
  vector<mint> res = convolution(f, fps_inverse(g, m));
  res.resize(m);
  for (int i = 0; i < m; i++) res[i] *= Fact[i];
  return res;
}


} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w, n, k;
  cin >> h >> w >> n >> k;
  int ubh = -1, ubw = -1;
  mint mxh = -1, mxw = -1;
  mint lh = -1, lw = -1;
  rep(_, 2) {
    if (h >= 2 * k) {
      ubh = k;
      mxh = k;
      lh = h - 2 * k + 2;
    } else {
      ubh = h - k + 1;
      mxh = h - k + 1;
      lh = 2 * k - h;
    }
    swap(h, w);
    swap(ubh, ubw);
    swap(mxh, mxw);
    swap(lh, lw);
  }

  mint tot = mint(h - k + 1) * (w - k + 1);
  auto ph = kth_power_sums(ubh, n + 1);
  auto pw = kth_power_sums(ubw, n + 1);
  ph[0]--;
  pw[0]--;
  mint ans;
  rep(i, n + 1) {
    mint coeff = comb(n, i) * tot.pow(n - i);
    if (i % 2) coeff *= -1;
    mint v;
    v += (mxh * mxw).pow(i) * lh * lw;
    v += mxh.pow(i) * lh * pw[i] * 2;
    v += ph[i] * mxw.pow(i) * lw * 2;
    v += ph[i] * pw[i] * 4;
    ans += coeff * v;
  }
  ans /= tot.pow(n);
  ans = mint(h) * w - ans;
  cout << ans.val() << '\n';
}
0