結果

問題 No.1388 Less than K
ユーザー vwxyzvwxyz
提出日時 2023-11-19 02:33:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 823 ms / 3,000 ms
コード長 2,563 bytes
コンパイル時間 793 ms
コンパイル使用メモリ 78,156 KB
実行使用メモリ 9,604 KB
最終ジャッジ日時 2023-11-19 02:34:14
合計ジャッジ時間 14,429 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 6 ms
6,676 KB
testcase_06 AC 4 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 10 ms
6,676 KB
testcase_09 AC 3 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 10 ms
6,676 KB
testcase_13 AC 16 ms
6,676 KB
testcase_14 AC 23 ms
6,676 KB
testcase_15 AC 60 ms
6,676 KB
testcase_16 AC 89 ms
6,676 KB
testcase_17 AC 140 ms
6,676 KB
testcase_18 AC 243 ms
6,676 KB
testcase_19 AC 378 ms
7,680 KB
testcase_20 AC 374 ms
7,808 KB
testcase_21 AC 23 ms
7,936 KB
testcase_22 AC 10 ms
8,064 KB
testcase_23 AC 7 ms
6,676 KB
testcase_24 AC 12 ms
6,676 KB
testcase_25 AC 15 ms
6,676 KB
testcase_26 AC 11 ms
6,676 KB
testcase_27 AC 8 ms
6,676 KB
testcase_28 AC 7 ms
6,676 KB
testcase_29 AC 6 ms
6,676 KB
testcase_30 AC 2 ms
6,676 KB
testcase_31 AC 3 ms
6,676 KB
testcase_32 AC 2 ms
6,676 KB
testcase_33 AC 11 ms
8,576 KB
testcase_34 AC 11 ms
8,320 KB
testcase_35 AC 10 ms
8,064 KB
testcase_36 AC 10 ms
7,296 KB
testcase_37 AC 12 ms
9,264 KB
testcase_38 AC 33 ms
7,936 KB
testcase_39 AC 16 ms
7,296 KB
testcase_40 AC 30 ms
6,912 KB
testcase_41 AC 75 ms
9,436 KB
testcase_42 AC 16 ms
7,936 KB
testcase_43 AC 324 ms
9,496 KB
testcase_44 AC 582 ms
9,432 KB
testcase_45 AC 431 ms
9,436 KB
testcase_46 AC 527 ms
9,508 KB
testcase_47 AC 773 ms
9,276 KB
testcase_48 AC 640 ms
9,436 KB
testcase_49 AC 823 ms
9,420 KB
testcase_50 AC 776 ms
9,408 KB
testcase_51 AC 426 ms
9,460 KB
testcase_52 AC 353 ms
9,544 KB
testcase_53 AC 78 ms
9,520 KB
testcase_54 AC 52 ms
9,492 KB
testcase_55 AC 18 ms
9,604 KB
testcase_56 AC 175 ms
9,604 KB
testcase_57 AC 432 ms
9,604 KB
testcase_58 AC 785 ms
9,604 KB
testcase_59 AC 443 ms
9,604 KB
testcase_60 AC 360 ms
9,604 KB
testcase_61 AC 15 ms
9,604 KB
testcase_62 AC 12 ms
9,604 KB
testcase_63 AC 21 ms
9,604 KB
testcase_64 AC 24 ms
9,604 KB
testcase_65 AC 54 ms
9,604 KB
testcase_66 AC 78 ms
9,604 KB
testcase_67 AC 108 ms
9,604 KB
testcase_68 AC 287 ms
9,604 KB
testcase_69 AC 353 ms
9,604 KB
testcase_70 AC 589 ms
9,604 KB
testcase_71 AC 513 ms
9,604 KB
testcase_72 AC 18 ms
9,604 KB
testcase_73 AC 14 ms
9,604 KB
testcase_74 AC 13 ms
9,604 KB
testcase_75 AC 13 ms
9,604 KB
testcase_76 AC 12 ms
9,604 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

using namespace std;

const long long int mod = 998244353;

long long int pow(long long int x,long long int n,long long int mod){
    if(n==0){
        return 1LL;
    }
    if(n%2==0){
        long long int p=pow(x,n/2,mod);
        return p*p%mod;
    }
    else{
        long long int p=pow(x,n/2,mod);
        p*=p;
        p%=mod;
        p*=x;
        return p%mod;
    }
}

int main() {
    int H, W, K;
    cin >> H >> W >> K;
    H--; W--;

    K /= 2;

    vector<long long> fact(H + W + 1, 1ll);
    for (int i = 1; i <= H + W; ++i) {
        fact[i] = (fact[i - 1] * i) % mod;
    }
    vector<long long> fact_inve(H + W + 1, 0ll);
    fact_inve[H + W] = pow(fact[H+W],mod-2,mod);
    for (int i = H + W - 1; i >= 0; --i) {
        fact_inve[i] = (fact_inve[i + 1] * (i+1ll)) % mod;
    }

    int N = min(H, W);
    long long ans = 0ll;

    if (K <= 300) {
        vector<long long> prev(2 * K + 1, 0ll);
        vector<long long> dp(2 * K + 1, 0ll);

        for (int h = 0; h <= N; ++h) {
            if (h) {
                prev = dp;
                dp = vector<long long>(2 * K + 1, 0ll);
            } else {
                dp = vector<long long>(2 * K + 1, 0ll);
                dp[K] = 1ll;
            }

            for (int w = max(h - K, 0); w <= min(h + K, N); ++w) {
                if (h && abs((h - 1) - w) <= K) {
                    dp[w - h + K] += prev[w - (h - 1) + K];
                }
                if (w && abs(h - (w - 1)) <= K) {
                    dp[w - h + K] += dp[(w - 1) - h + K];
                }
                dp[w - h + K] %= mod;
            }
            ans += fact[H + W] * fact_inve[H - h] % mod * fact_inve[W - h] % mod * fact_inve[2 * h] % mod * dp[K] % mod;
            ans %= mod;
        }
    } else {
        for (int cnt = 0; cnt <= N; ++cnt) {
            long long s = fact[2 * cnt] * fact_inve[cnt] % mod * fact_inve[cnt] % mod;
            for (int i = 1; i <= cnt / (K + 1); ++i) {
                if (i % 2) {
                    s -= 2 * fact[2 * cnt] * fact_inve[cnt - (K + 1) * i] % mod * fact_inve[cnt + (K + 1) * i] % mod;
                } else {
                    s += 2 * fact[2 * cnt] * fact_inve[cnt - (K + 1) * i] % mod * fact_inve[cnt + (K + 1) * i] % mod;
                }
                s = (s + mod) % mod;
            }
            ans += fact[H + W] * fact_inve[H - cnt] % mod * fact_inve[W - cnt] % mod * fact_inve[2 * cnt] % mod * s % mod;
            ans %= mod;

        }
    }

    cout << ans << endl;

    return 0;
}
0