結果

問題 No.2459 Stampaholic (Hard)
ユーザー 👑 NachiaNachia
提出日時 2023-08-25 04:32:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 780 ms / 4,000 ms
コード長 2,860 bytes
コンパイル時間 2,388 ms
コンパイル使用メモリ 120,372 KB
実行使用メモリ 33,980 KB
最終ジャッジ日時 2023-08-25 04:32:33
合計ジャッジ時間 12,466 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 749 ms
33,904 KB
testcase_02 AC 172 ms
9,956 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 357 ms
16,564 KB
testcase_09 AC 176 ms
10,468 KB
testcase_10 AC 739 ms
32,380 KB
testcase_11 AC 368 ms
18,240 KB
testcase_12 AC 780 ms
33,680 KB
testcase_13 AC 742 ms
31,776 KB
testcase_14 AC 178 ms
10,864 KB
testcase_15 AC 769 ms
33,784 KB
testcase_16 AC 755 ms
33,852 KB
testcase_17 AC 754 ms
33,840 KB
testcase_18 AC 779 ms
33,844 KB
testcase_19 AC 760 ms
33,980 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 753 ms
30,732 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <atcoder/modint>
#include <atcoder/convolution>
using namespace std;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
#define rep(i,n) for(int i=0; i<(int)(n); i++)
const i64 INF = 1001001001001001001;

using Modint = atcoder::static_modint<998244353>;

vector<Modint> invfps(vector<Modint> x, int n){
    Modint ix0 = x[0].inv();
    for(auto& a : x) a *= ix0;
    vector<Modint> res;
    res.push_back(1);
    int z = 1;
    while(z < n){
        if((int)x.size() < z*2) x.resize(z*2);
        auto xx = vector(x.begin(), x.begin() + z*2);
        auto tmp = atcoder::convolution(res, xx);
        tmp.resize(z*2);
        tmp = atcoder::convolution(tmp, res);
        rep(i,z) res.push_back(-tmp[z+i]);
        z *= 2;
    }
    for(auto& a : res) a *= ix0;
    return vector(res.begin(), res.begin() + z);
}

vector<Modint> factorial;
vector<Modint> invFactorial;
void extend(int n){
    factorial.resize(1+n);
    factorial[0] = 1;
    for(int i=1; i<=n; i++) factorial[i] = factorial[i-1] * i;
    invFactorial.resize(1+n);
    invFactorial[n] = factorial[n].inv();
    for(int i=n; i>=1; i--) invFactorial[i-1] = invFactorial[i] * i;
}
Modint comb(int n, int k){ return factorial[n] * invFactorial[k] * invFactorial[n-k]; }

vector<Modint> powsum(Modint q, int n){
    vector<Modint> a(n+1), b(n+1);
    a[0] = q; rep(i,n) a[i+1] = a[i] * q;
    rep(i,n+1) a[i] *= invFactorial[i+1];
    rep(i,n+1) b[i] = invFactorial[i+1];
    auto invb = invfps(move(b), n+1);
    a = atcoder::convolution(a, invb);
    rep(i,n+1) a[i] *= factorial[i];
    return vector(a.begin(), a.begin() + (n+1));
}

int main(){
    i64 H, W, N, K; cin >> H >> W >> N >> K;
    extend(N+2);
    i64 f = (H-K+1) * (W-K+1);
    Modint p = (Modint(f)).inv().pow(N);
    Modint ans = 0;
    i64 xcent = min(K, W-K+1);
    i64 xcent_width = W - xcent * 2 + 2;
    i64 ycent = min(K, H-K+1);
    i64 ycent_width = H - ycent * 2 + 2;
    auto xpowsum = powsum(xcent, N); xpowsum[0] -= 1;
    auto ypowsum = powsum(ycent, N); ypowsum[0] -= 1;
    vector<Modint> powf(N+1);
    powf[0] = Modint((N%2==0) ? 1 : -1);
    rep(i,N) powf[i+1] = powf[i] * (-f);
    vector<Modint> pow_xcent(N+1);
    pow_xcent[0] = 1;
    rep(i,N) pow_xcent[i+1] = pow_xcent[i] * xcent;
    vector<Modint> pow_ycent(N+1);
    pow_ycent[0] = 1;
    rep(i,N) pow_ycent[i+1] = pow_ycent[i] * ycent;
    rep(k,N+1) ans += comb(N,k) * powf[N-k] * (xpowsum[k] * 2 + pow_xcent[k] * xcent_width) * (ypowsum[k] * 2 + pow_ycent[k] * ycent_width);
    ans *= p;
    ans = Modint(H*W) - ans;
    cout << ans.val() << endl;
    return 0;
}


struct ios_do_not_sync{
    ios_do_not_sync(){
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
    }
} ios_do_not_sync_instance;

0