結果
問題 | No.2459 Stampaholic (Hard) |
ユーザー | SSRS |
提出日時 | 2023-09-01 22:35:15 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 2,631 ms / 4,000 ms |
コード長 | 3,502 bytes |
コンパイル時間 | 3,377 ms |
コンパイル使用メモリ | 248,308 KB |
実行使用メモリ | 202,636 KB |
最終ジャッジ日時 | 2024-06-11 04:33:02 |
合計ジャッジ時間 | 34,022 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | AC | 2,631 ms
202,400 KB |
testcase_02 | AC | 587 ms
47,384 KB |
testcase_03 | AC | 2 ms
6,940 KB |
testcase_04 | AC | 2 ms
6,944 KB |
testcase_05 | AC | 1 ms
6,940 KB |
testcase_06 | AC | 2 ms
6,940 KB |
testcase_07 | AC | 2 ms
6,940 KB |
testcase_08 | AC | 1,203 ms
89,904 KB |
testcase_09 | AC | 609 ms
49,264 KB |
testcase_10 | AC | 2,574 ms
192,244 KB |
testcase_11 | AC | 1,228 ms
96,828 KB |
testcase_12 | AC | 2,553 ms
201,332 KB |
testcase_13 | AC | 2,542 ms
197,656 KB |
testcase_14 | AC | 599 ms
51,292 KB |
testcase_15 | AC | 2,537 ms
202,372 KB |
testcase_16 | AC | 2,550 ms
202,388 KB |
testcase_17 | AC | 2,534 ms
202,636 KB |
testcase_18 | AC | 2,578 ms
202,424 KB |
testcase_19 | AC | 2,582 ms
202,536 KB |
testcase_20 | AC | 2 ms
6,944 KB |
testcase_21 | AC | 2,539 ms
184,920 KB |
ソースコード
#include <bits/stdc++.h> #include <atcoder/convolution> using namespace std; const long long MOD = 998244353; long long modpow(long long a, long long b){ long long ans = 1; while (b > 0){ if (b % 2 == 1){ ans *= a; ans %= MOD; } a *= a; a %= MOD; b /= 2; } return ans; } long long modinv(long long a){ return modpow(a, MOD - 2); } vector<long long> inverse(vector<long long> f){ int N = f.size(); vector<long long> ans(1); ans[0] = 1; for (int i = 1; i < N * 2; i *= 2){ vector<long long> ans2 = ans; ans2.resize(i * 4); int N2 = min(N, i * 2); vector<long long> f2(i * 4, 0); for (int j = 0; j < N2; j++){ f2[j] = f[j]; } ans2 = atcoder::convolution(ans2, ans2); ans2.resize(i * 2); ans2 = atcoder::convolution(ans2, f2); ans2.resize(i * 2); for (int j = 0; j < i; j++){ ans2[j] = MOD - ans2[j] + ans[j] * 2; ans2[j] %= MOD; } swap(ans, ans2); } ans.resize(N); return ans; } int main(){ int H, W, N, K; cin >> H >> W >> N >> K; vector<long long> inv(N + 2); inv[1] = 1; for (int i = 2; i <= N + 1; i++){ inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; } vector<long long> fact(N + 2); vector<long long> finv(N + 2); fact[0] = 1; finv[0] = 1; for (int i = 1; i <= N + 1; i++){ fact[i] = fact[i - 1] * i % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } vector<long long> binom(N + 1); for (int i = 0; i <= N; i++){ binom[i] = fact[N] * finv[i] % MOD * finv[N - i] % MOD; } int px = min(H - K + 1, K); int py = min(W - K + 1, K); int qx = H - 2 * (px - 1); int qy = W - 2 * (py - 1); long long C = MOD - modinv(H - K + 1) * modinv(W - K + 1) % MOD; vector<long long> POW_C(N + 1); vector<long long> POW_Cpx(N + 1); vector<long long> POW_Cpy(N + 1); POW_C[0] = 1; POW_Cpx[0] = 1; POW_Cpy[0] = 1; for (int i = 0; i < N; i++){ POW_C[i + 1] = POW_C[i] * C % MOD; POW_Cpx[i + 1] = POW_Cpx[i] * C % MOD * px % MOD; POW_Cpy[i + 1] = POW_Cpy[i] * C % MOD * py % MOD; } auto solve = [&](long long N, int K){ vector<long long> POW_N(K + 2); POW_N[0] = 1; for (int i = 0; i < K + 1; i++){ POW_N[i + 1] = POW_N[i] * N % MOD; } vector<long long> num(K + 1, 0); for (int i = 0; i <= K; i++){ num[i] = POW_N[i + 1] * finv[i + 1] % MOD; } vector<long long> den(K + 1, 0); for (int i = 0; i <= K; i++){ den[i] = finv[i + 1]; } vector<long long> den_inv = inverse(den); vector<long long> ans = atcoder::convolution(num, den_inv); for (int i = 0; i <= K; i++){ ans[i] *= fact[i]; ans[i] %= MOD; } ans[0]--; return ans; }; vector<long long> sum_px = solve(px, N); vector<long long> sum_py = solve(py, N); long long ans1 = 0; for (int i = 0; i <= N; i++){ ans1 += POW_C[i] * binom[i] % MOD * sum_px[i] % MOD * sum_py[i] % MOD; } ans1 %= MOD; ans1 *= 4; ans1 %= MOD; long long ans2 = 0; for (int i = 0; i <= N; i++){ ans2 += POW_Cpy[i] * binom[i] % MOD * sum_px[i] % MOD; } ans2 %= MOD; ans2 *= 2 * qy; ans2 %= MOD; long long ans3 = 0; for (int i = 0; i <= N; i++){ ans3 += POW_Cpx[i] * binom[i] % MOD * sum_py[i] % MOD; } ans3 %= MOD; ans3 *= 2 * qx; ans3 %= MOD; long long ans4 = modpow(MOD + 1 + C * px % MOD * py % MOD, N) * qx % MOD * qy % MOD; long long ans = (ans1 + ans2 + ans3 + ans4) % MOD; cout << ((long long) H * W % MOD - ans + MOD) % MOD << endl; }