結果
| 問題 |
No.1629 Sorting Integers (SUM of M)
|
| コンテスト | |
| ユーザー |
siman
|
| 提出日時 | 2021-08-01 08:13:19 |
| 言語 | Ruby (3.4.1) |
| 結果 |
AC
|
| 実行時間 | 501 ms / 2,000 ms |
| コード長 | 857 bytes |
| コンパイル時間 | 302 ms |
| コンパイル使用メモリ | 7,296 KB |
| 実行使用メモリ | 13,952 KB |
| 最終ジャッジ日時 | 2024-09-16 12:43:42 |
| 合計ジャッジ時間 | 5,467 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 14 |
コンパイルメッセージ
Syntax OK
ソースコード
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
siman