結果

問題 No.2527 H and W
ユーザー InTheBloomInTheBloom
提出日時 2023-11-03 22:17:37
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 655 ms / 2,000 ms
コード長 2,461 bytes
コンパイル時間 4,119 ms
コンパイル使用メモリ 193,524 KB
実行使用メモリ 19,992 KB
最終ジャッジ日時 2023-11-03 22:18:01
合計ジャッジ時間 22,354 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 649 ms
19,992 KB
testcase_01 AC 625 ms
19,992 KB
testcase_02 AC 637 ms
19,992 KB
testcase_03 AC 647 ms
19,992 KB
testcase_04 AC 633 ms
19,992 KB
testcase_05 AC 651 ms
19,992 KB
testcase_06 AC 630 ms
19,992 KB
testcase_07 AC 642 ms
19,992 KB
testcase_08 AC 629 ms
19,992 KB
testcase_09 AC 652 ms
19,992 KB
testcase_10 AC 631 ms
19,992 KB
testcase_11 AC 652 ms
19,992 KB
testcase_12 AC 649 ms
19,992 KB
testcase_13 AC 649 ms
19,992 KB
testcase_14 AC 628 ms
19,992 KB
testcase_15 AC 649 ms
19,992 KB
testcase_16 AC 638 ms
19,992 KB
testcase_17 AC 631 ms
19,992 KB
testcase_18 AC 653 ms
19,992 KB
testcase_19 AC 629 ms
19,992 KB
testcase_20 AC 655 ms
19,992 KB
testcase_21 AC 629 ms
19,992 KB
testcase_22 AC 642 ms
19,992 KB
testcase_23 AC 622 ms
19,992 KB
testcase_24 AC 646 ms
19,992 KB
testcase_25 AC 631 ms
19,992 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std;

void main () {
    long H, W, K; readln.read(H, W, K);
    solve(H, W, K);
}

void solve (long H, long W, long K) {
    const int MOD = 998244353;
    /* combination前計算をすればよい */
    const int MAX = 10^^6+1;
    long[] fact = new long[](MAX);
    long[] factInv = new long[](MAX);
    fact[0] = factInv[0] = 1;
    for (int i = 1; i < MAX; i++) {
        (fact[i] = i*fact[i-1]) %= MOD;
        factInv[i] = modInv(fact[i], MOD);
    }

    long comb (long n_, long k_, const int MOD) {
        int n = cast(int) n_, k = cast(int) k_;
        if (n < k) return 0;
        long res = fact[n];
        res *= factInv[k]; res %= MOD;
        res *= factInv[n-k]; res %= MOD;
        return res;
    }

    /* 列をn個選んだとすると、選ぶべき行は(大抵の場合)一意に定まり、m = (K-HW-nH)/(W-n)
    /* 盤面を全部塗りつぶすときがコーナーケースなので、気を付けて! */

    if (K == 0) {
        /* 片方を全部選ぶ -> もう片方を好き勝手にする(2^x通り)を2回やって、両方全部塗るときのかぶりとして1引く */
        long ans = (modPow(2, H, MOD) + modPow(2, W, MOD))%MOD - 1;
        if (ans < 0) ans += MOD;
        writeln(ans);
        return;
    }

    long ans = 0;
    for (int i = 0; i < W; i++) {
        if ((H*W - K - i*H) % (W - i) == 0) {
            long choice = (H*W - K - i*H) / (W-i);
            if (0 <= choice) ans += (comb(W, i, MOD) * comb(H, choice, MOD)) % MOD; ans %= MOD;
        }
    }

    writeln(ans);
}

void read(T...)(string S, ref T args) {
    auto buf = S.split;
    foreach (i, ref arg; args) {
        arg = buf[i].to!(typeof(arg));
    }
}

long modPow (long a, long x, const int MOD) {
    // assertion
    assert(0 <= x);
    assert(1 <= MOD);

    // normalize
    a %= MOD; a += MOD; a %= MOD;

    // simple case
    if (MOD == 1) {
        return 0L;
    }

    if (x == 0) {
        return 1L;
    }

    if (x == 1) {
        return a;
    }

    // calculate
    long res = 1L;
    long base = a % MOD;
    while (x != 0) {
        if ((x&1) != 0) {
            res *= base;
            res %= MOD;
        }
        base = base*base; base %= MOD;
        x >>= 1;
    }

    return res;
}

long modInv (long x, const int MOD) {
    import std.exception;
    enforce(1 <= MOD, format("Line : %s, MOD must be greater than 1. Your input = %s", __LINE__, MOD));
    return modPow(x, MOD-2, MOD);
}
0