結果

問題 No.1035 Color Box
ユーザー simansiman
提出日時 2021-01-26 07:49:31
言語 Ruby
(3.2.2)
結果
WA  
実行時間 -
コード長 890 bytes
コンパイル時間 372 ms
コンパイル使用メモリ 11,244 KB
実行使用メモリ 17,852 KB
最終ジャッジ日時 2023-09-05 11:21:21
合計ジャッジ時間 6,145 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 187 ms
17,660 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 90 ms
15,424 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 172 ms
17,316 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 83 ms
15,188 KB
testcase_12 WA -
testcase_13 AC 152 ms
17,064 KB
testcase_14 AC 162 ms
17,248 KB
testcase_15 AC 185 ms
17,852 KB
testcase_16 AC 82 ms
15,192 KB
testcase_17 AC 83 ms
15,240 KB
testcase_18 AC 84 ms
15,108 KB
testcase_19 AC 187 ms
17,756 KB
testcase_20 AC 84 ms
15,244 KB
testcase_21 AC 83 ms
15,176 KB
testcase_22 AC 82 ms
15,104 KB
testcase_23 AC 83 ms
15,240 KB
testcase_24 AC 83 ms
15,140 KB
testcase_25 AC 83 ms
15,052 KB
testcase_26 WA -
testcase_27 AC 82 ms
15,148 KB
testcase_28 AC 81 ms
15,052 KB
testcase_29 AC 82 ms
15,076 KB
testcase_30 WA -
testcase_31 AC 82 ms
15,108 KB
testcase_32 AC 81 ms
15,244 KB
testcase_33 AC 81 ms
15,196 KB
testcase_34 AC 82 ms
15,120 KB
testcase_35 WA -
testcase_36 WA -
testcase_37 AC 82 ms
15,280 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

N, M = gets.split.map(&:to_i)
MOD = 10 ** 9 + 7
ans = M.pow(N, MOD)
mi = ModInteger.new(M + 1)

1.upto(M - 1) do |x|
  ans += ((-1) ** x) * mi.combination(M, x) * x.pow(N, MOD)
  ans %= MOD
end

puts ans
0