結果

問題 No.1927 AB-CD
ユーザー simansiman
提出日時 2022-05-07 13:49:32
言語 Ruby
(3.3.0)
結果
AC  
実行時間 195 ms / 2,000 ms
コード長 809 bytes
コンパイル時間 311 ms
コンパイル使用メモリ 11,196 KB
実行使用メモリ 22,968 KB
最終ジャッジ日時 2023-09-20 23:23:17
合計ジャッジ時間 6,930 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
15,128 KB
testcase_01 AC 84 ms
15,140 KB
testcase_02 AC 83 ms
15,088 KB
testcase_03 AC 82 ms
15,148 KB
testcase_04 AC 167 ms
21,412 KB
testcase_05 AC 161 ms
21,012 KB
testcase_06 AC 162 ms
21,068 KB
testcase_07 AC 156 ms
20,072 KB
testcase_08 AC 92 ms
16,100 KB
testcase_09 AC 168 ms
21,232 KB
testcase_10 AC 146 ms
19,792 KB
testcase_11 AC 158 ms
20,312 KB
testcase_12 AC 120 ms
17,756 KB
testcase_13 AC 125 ms
18,324 KB
testcase_14 AC 88 ms
15,824 KB
testcase_15 AC 100 ms
16,820 KB
testcase_16 AC 161 ms
20,824 KB
testcase_17 AC 138 ms
19,196 KB
testcase_18 AC 144 ms
19,476 KB
testcase_19 AC 150 ms
19,832 KB
testcase_20 AC 178 ms
21,792 KB
testcase_21 AC 150 ms
20,056 KB
testcase_22 AC 110 ms
17,376 KB
testcase_23 AC 180 ms
22,132 KB
testcase_24 AC 193 ms
22,968 KB
testcase_25 AC 195 ms
22,944 KB
testcase_26 AC 195 ms
22,808 KB
testcase_27 AC 89 ms
15,160 KB
testcase_28 AC 83 ms
15,032 KB
testcase_29 AC 84 ms
15,300 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

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

  MOD = 998_244_353

  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

N = gets.to_i
S = gets.chomp

mi = ModInteger.new(N + 1)

a = S.count('A')
b = S.count('B')

puts mi.combination(N, a + b)
0