結果

問題 No.1035 Color Box
ユーザー simansiman
提出日時 2021-01-26 07:55:35
言語 Ruby
(3.3.0)
結果
AC  
実行時間 191 ms / 2,000 ms
コード長 904 bytes
コンパイル時間 268 ms
コンパイル使用メモリ 11,560 KB
実行使用メモリ 17,868 KB
最終ジャッジ日時 2023-09-05 11:24:46
合計ジャッジ時間 6,005 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 188 ms
17,868 KB
testcase_01 AC 86 ms
15,148 KB
testcase_02 AC 82 ms
15,272 KB
testcase_03 AC 93 ms
15,360 KB
testcase_04 AC 91 ms
15,376 KB
testcase_05 AC 85 ms
15,140 KB
testcase_06 AC 84 ms
15,108 KB
testcase_07 AC 84 ms
15,140 KB
testcase_08 AC 175 ms
17,676 KB
testcase_09 AC 120 ms
16,844 KB
testcase_10 AC 82 ms
15,144 KB
testcase_11 AC 83 ms
15,188 KB
testcase_12 AC 83 ms
15,184 KB
testcase_13 AC 156 ms
17,032 KB
testcase_14 AC 170 ms
17,360 KB
testcase_15 AC 191 ms
17,656 KB
testcase_16 AC 83 ms
15,108 KB
testcase_17 AC 82 ms
15,104 KB
testcase_18 AC 83 ms
15,176 KB
testcase_19 AC 188 ms
17,684 KB
testcase_20 AC 83 ms
15,156 KB
testcase_21 AC 84 ms
15,196 KB
testcase_22 AC 83 ms
15,052 KB
testcase_23 AC 82 ms
15,156 KB
testcase_24 AC 80 ms
15,156 KB
testcase_25 AC 83 ms
15,100 KB
testcase_26 AC 83 ms
15,104 KB
testcase_27 AC 82 ms
15,252 KB
testcase_28 AC 81 ms
15,176 KB
testcase_29 AC 82 ms
15,040 KB
testcase_30 AC 83 ms
15,192 KB
testcase_31 AC 85 ms
15,148 KB
testcase_32 AC 85 ms
15,324 KB
testcase_33 AC 85 ms
15,088 KB
testcase_34 AC 84 ms
15,144 KB
testcase_35 AC 84 ms
15,104 KB
testcase_36 AC 84 ms
15,156 KB
testcase_37 AC 85 ms
15,012 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)

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

puts ans
0