結果

問題 No.2409 Strange Werewolves
ユーザー InTheBloomInTheBloom
提出日時 2023-08-11 22:30:45
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 197 ms / 2,000 ms
コード長 1,941 bytes
コンパイル時間 4,086 ms
コンパイル使用メモリ 171,904 KB
実行使用メモリ 7,424 KB
最終ジャッジ日時 2024-04-29 13:32:29
合計ジャッジ時間 6,194 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 197 ms
7,296 KB
testcase_04 AC 195 ms
7,296 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 194 ms
7,424 KB
testcase_07 AC 190 ms
7,424 KB
testcase_08 AC 87 ms
5,376 KB
testcase_09 AC 135 ms
5,888 KB
testcase_10 AC 41 ms
5,376 KB
testcase_11 AC 22 ms
5,376 KB
testcase_12 AC 64 ms
5,376 KB
testcase_13 AC 58 ms
5,376 KB
testcase_14 AC 93 ms
5,376 KB
testcase_15 AC 175 ms
6,528 KB
testcase_16 AC 119 ms
5,504 KB
testcase_17 AC 24 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std;

void main () {
    int X, Y, Z, W; readln.read(X, Y, Z, W);
    solve(X, Y, Z, W);
}

void solve (int X, int Y, int Z, int W) {
    // 片方が追放されるとして、これは片方の順列にもう片方の元を好きなように差し込む通り数になる?
    const int MOD = 998244353;
    long ans = 1;

    // 常に人間側が残るように正規化
    if (Z == 0) {
        swap(X, Y);
        swap(Z, W);
    }

    for (int i = 1; i <= Y; i++) {
        ans *= i;
        ans %= MOD;
    }

    long[] fac = new long[](X+1);
    long[] fac_inv = new long[](X+1);
    fac[0] = fac_inv[0] = 1;
    foreach (i; 1..X+1) {
        fac[i] = i*fac[i-1];
        fac[i] %= MOD;
        fac_inv[i] = modPow(fac[i], MOD-2, MOD);
    }
    long nCk (long n, long k) {
        long res = fac[n];
        res *= fac_inv[k];
        res %= MOD;
        res *= fac_inv[n-k];
        return res % MOD;
    }

    // X-Z人を選ぶところからなので、X C X-Z を計算しないといけない
    ans *= nCk(X, X-Z);
    ans %= MOD;

    for (int i = 0; i < X-Z; i++) {
        // i人目はY+i-1人の(右端を除く)隙間に入る感じ
        ans *= Y+i;
        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;
}
0