結果

問題 No.2409 Strange Werewolves
ユーザー simansiman
提出日時 2023-08-25 14:30:55
言語 Ruby
(3.3.0)
結果
AC  
実行時間 358 ms / 2,000 ms
コード長 1,163 bytes
コンパイル時間 441 ms
コンパイル使用メモリ 11,396 KB
実行使用メモリ 29,456 KB
最終ジャッジ日時 2023-08-25 14:31:04
合計ジャッジ時間 6,455 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 264 ms
29,148 KB
testcase_01 AC 265 ms
29,208 KB
testcase_02 AC 275 ms
29,376 KB
testcase_03 AC 358 ms
29,172 KB
testcase_04 AC 302 ms
29,292 KB
testcase_05 AC 296 ms
29,136 KB
testcase_06 AC 310 ms
29,448 KB
testcase_07 AC 331 ms
29,248 KB
testcase_08 AC 291 ms
29,456 KB
testcase_09 AC 275 ms
29,200 KB
testcase_10 AC 265 ms
29,380 KB
testcase_11 AC 286 ms
29,176 KB
testcase_12 AC 270 ms
29,388 KB
testcase_13 AC 305 ms
29,172 KB
testcase_14 AC 308 ms
29,440 KB
testcase_15 AC 300 ms
29,240 KB
testcase_16 AC 266 ms
29,208 KB
testcase_17 AC 275 ms
29,136 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class ModInteger
  attr_reader :fac, :inv, :finv, :mod

  def initialize(n, mod: MOD)
    @mod = mod
    @fac = [1, 1]
    @inv = [1, 1]
    @finv = [1, 1]

    (2..n).each do |i|
      @fac[i] = fac[i - 1] * i % mod
      @inv[i] = mod - inv[mod % i] * (mod / i) % mod
      @finv[i] = finv[i - 1] * inv[i] % mod
    end
  end

  def combination(n, k)
    return 0 if n < k
    return 0 if n < 0 || k < 0

    fac[n] * (finv[k] * finv[n - k] % mod) % mod
  end

  def permutation(n, k = n)
    return 0 if n < k
    return 0 if n < 0 || k < 0

    fac[n] * (finv[n - k] % mod) % mod
  end

  def repeated_combination(n, k)
    combination(n + k - 1, k)
  end
end

def mod_permutation(n, mod)
  (1..n).inject(1) { |e, x| e = e * x % mod }
end

MOD = 998_244_353
X, Y, Z, W = gets.split.map(&:to_i)
mi = ModInteger.new(600_010, mod: MOD)
all = X + Y - Z - W

if Z == 0
  v1 = mi.combination(all - 1, X - 1) * mod_permutation(X, MOD)
  v2 = mi.combination(Y, W) * mod_permutation(Y - W, MOD)
  puts v1 * v2 % MOD
else
  v1 = mi.combination(all - 1, Y - 1) * mod_permutation(Y, MOD)
  v2 = mi.combination(X, Z) * mod_permutation(X - Z, MOD)
  puts v1 * v2 % MOD
end
0