結果
問題 | No.2409 Strange Werewolves |
ユーザー |
![]() |
提出日時 | 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 = 998244353max_n = 600000 # Sufficient for the problem constraints# Precompute factorials modulo modfact = [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 theoreminv_fact = [1] * (max_n + 1)inv_fact[max_n] = pow(fact[max_n], mod - 2, mod) # Modular inverse of the last factorialfor i in range(max_n - 1, -1, -1):inv_fact[i] = inv_fact[i + 1] * (i + 1) % moddef comb(n, k):if k < 0 or k > n:return 0return fact[n] * inv_fact[k] % mod * inv_fact[n - k] % mod# Read inputX, Y, Z, W = map(int, input().split())if W == 0:# Case 1: Wolves are all expelled, remaining humans Z (W=0)H = X - Zterms = H + Y - 1c = comb(X, H)ans = c * Y % modans = ans * fact[terms] % modelse:# Case 2: Humans are all expelled, remaining wolves W (Z=0)K = Y - Wterms = X + K - 1c = comb(Y, K)ans = c * X % modans = ans * fact[terms] % modprint(ans)