結果

問題 No.391 CODING WAR
ユーザー simansiman
提出日時 2022-07-20 16:05:04
言語 Ruby
(3.3.0)
結果
AC  
実行時間 224 ms / 2,000 ms
コード長 998 bytes
コンパイル時間 411 ms
コンパイル使用メモリ 11,176 KB
実行使用メモリ 18,020 KB
最終ジャッジ日時 2023-09-15 09:53:26
合計ジャッジ時間 4,127 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
15,020 KB
testcase_01 AC 82 ms
15,052 KB
testcase_02 AC 83 ms
15,108 KB
testcase_03 AC 82 ms
15,164 KB
testcase_04 AC 82 ms
15,252 KB
testcase_05 AC 83 ms
15,132 KB
testcase_06 AC 82 ms
15,136 KB
testcase_07 AC 80 ms
15,296 KB
testcase_08 AC 81 ms
15,048 KB
testcase_09 AC 224 ms
17,624 KB
testcase_10 AC 222 ms
18,020 KB
testcase_11 AC 83 ms
15,060 KB
testcase_12 AC 83 ms
15,328 KB
testcase_13 AC 218 ms
17,760 KB
testcase_14 AC 190 ms
17,416 KB
testcase_15 AC 203 ms
17,400 KB
testcase_16 AC 153 ms
16,536 KB
testcase_17 AC 168 ms
16,856 KB
testcase_18 AC 141 ms
16,744 KB
testcase_19 AC 143 ms
16,748 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class Integer
  def mod_inverse(mod)
    self.pow(mod - 2, mod)
  end
end

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)

if N < M
  puts 0
  exit
end

MOD = 10 ** 9 + 7
mi = ModInteger.new(M + 10)
a = 1
ans = 0

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

  a *= -1
end

puts ans
0