結果

問題 No.2023 Tiling is Fun
ユーザー simansiman
提出日時 2022-09-05 18:13:11
言語 Ruby
(3.3.0)
結果
AC  
実行時間 175 ms / 2,000 ms
コード長 810 bytes
コンパイル時間 109 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 20,864 KB
最終ジャッジ日時 2024-04-30 21:15:50
合計ジャッジ時間 3,623 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 102 ms
12,160 KB
testcase_01 AC 141 ms
16,640 KB
testcase_02 AC 114 ms
14,336 KB
testcase_03 AC 119 ms
14,080 KB
testcase_04 AC 96 ms
12,544 KB
testcase_05 AC 119 ms
14,336 KB
testcase_06 AC 117 ms
14,592 KB
testcase_07 AC 152 ms
17,152 KB
testcase_08 AC 129 ms
15,488 KB
testcase_09 AC 146 ms
16,768 KB
testcase_10 AC 142 ms
16,768 KB
testcase_11 AC 96 ms
12,416 KB
testcase_12 AC 92 ms
12,416 KB
testcase_13 AC 91 ms
12,416 KB
testcase_14 AC 129 ms
15,616 KB
testcase_15 AC 130 ms
15,616 KB
testcase_16 AC 131 ms
15,488 KB
testcase_17 AC 175 ms
20,736 KB
testcase_18 AC 173 ms
20,864 KB
testcase_19 AC 147 ms
17,152 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

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

  MOD = 10 ** 9 + 7

  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

A, B = gets.split.map(&:to_i)
MOD = 998_244_353
mi = ModInteger.new(A + B, mod: MOD)

puts mi.combination(A + B - 2, A - 1)
0