結果
| 問題 | No.2409 Strange Werewolves | 
| コンテスト | |
| ユーザー |  lam6er | 
| 提出日時 | 2025-03-20 20:28:12 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 58 ms / 2,000 ms | 
| コード長 | 1,028 bytes | 
| コンパイル時間 | 330 ms | 
| コンパイル使用メモリ | 82,848 KB | 
| 実行使用メモリ | 70,244 KB | 
| 最終ジャッジ日時 | 2025-03-20 20:29:27 | 
| 合計ジャッジ時間 | 1,915 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 16 | 
ソースコード
mod = 998244353
max_n = 600000  # Sufficient for the problem constraints
# Precompute factorials modulo mod
fact = [1] * (max_n + 1)
for i in range(1, max_n + 1):
    fact[i] = fact[i-1] * i % mod
# Precompute inverse factorials modulo mod using Fermat's little theorem
inv_fact = [1] * (max_n + 1)
inv_fact[max_n] = pow(fact[max_n], mod - 2, mod)  # Modular inverse of the last factorial
for i in range(max_n - 1, -1, -1):
    inv_fact[i] = inv_fact[i + 1] * (i + 1) % mod
def comb(n, k):
    if k < 0 or k > n:
        return 0
    return fact[n] * inv_fact[k] % mod * inv_fact[n - k] % mod
# Read input
X, Y, Z, W = map(int, input().split())
if W == 0:
    # Case 1: Wolves are all expelled, remaining humans Z (W=0)
    H = X - Z
    terms = H + Y - 1
    c = comb(X, H)
    ans = c * Y % mod
    ans = ans * fact[terms] % mod
else:
    # Case 2: Humans are all expelled, remaining wolves W (Z=0)
    K = Y - W
    terms = X + K - 1
    c = comb(Y, K)
    ans = c * X % mod
    ans = ans * fact[terms] % mod
print(ans)
            
            
            
        