結果

問題 No.1629 Sorting Integers (SUM of M)
ユーザー simansiman
提出日時 2021-08-01 08:13:19
言語 Ruby
(3.3.0)
結果
AC  
実行時間 512 ms / 2,000 ms
コード長 857 bytes
コンパイル時間 374 ms
コンパイル使用メモリ 11,388 KB
実行使用メモリ 16,936 KB
最終ジャッジ日時 2023-10-14 18:41:37
合計ジャッジ時間 5,272 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
15,220 KB
testcase_01 AC 75 ms
15,036 KB
testcase_02 AC 75 ms
15,216 KB
testcase_03 AC 79 ms
15,124 KB
testcase_04 AC 210 ms
15,948 KB
testcase_05 AC 108 ms
15,556 KB
testcase_06 AC 117 ms
15,448 KB
testcase_07 AC 295 ms
16,224 KB
testcase_08 AC 244 ms
16,052 KB
testcase_09 AC 311 ms
16,056 KB
testcase_10 AC 237 ms
15,888 KB
testcase_11 AC 197 ms
15,628 KB
testcase_12 AC 302 ms
16,144 KB
testcase_13 AC 333 ms
16,184 KB
testcase_14 AC 237 ms
15,684 KB
testcase_15 AC 334 ms
16,088 KB
testcase_16 AC 294 ms
16,304 KB
testcase_17 AC 512 ms
16,936 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class Integer
  def mod_pow(n, mod)
    x = self
    res = 1

    while n > 0
      res = res * x % mod if n[0] == 1
      x = x * x % mod
      n >>= 1
    end

    res
  end

  def mod_inverse(mod)
    mod_pow(mod - 2, mod)
  end
end

N = gets.to_i
C = gets.split.map(&:to_i)
S = C.sum
MOD = 10 ** 9 + 7
cache = Array.new(S + 1, 1)
mod = Array.new(10, 0)

2.upto(S - 1) do |x|
  cache[x] = cache[x - 1] * x
  cache[x] %= MOD
end

1.upto(9) do |i|
  m = cache[S - 1]

  1.upto(9) do |j|
    c = C[j - 1]
    next if c == 0

    if i == j
      m *= cache[c - 1].mod_inverse(MOD) if c >= 2
    else
      m *= cache[c].mod_inverse(MOD)
    end

    m %= MOD
  end

  mod[i] = m
end

ans = 0
base = 1

N.times do
  C.each.with_index(1) do |c, i|
    next if c == 0

    ans += mod[i] * base * i
    ans %= MOD
  end

  base *= 10
  base %= MOD
end

puts ans
0