結果

問題 No.1330 Multiply or Divide
ユーザー siman
提出日時 2022-02-28 07:28:59
言語 Ruby
(3.4.1)
結果
AC  
実行時間 353 ms / 2,000 ms
コード長 643 bytes
コンパイル時間 85 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 26,240 KB
最終ジャッジ日時 2024-07-06 10:14:21
合計ジャッジ時間 8,776 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 46
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:50: warning: ambiguous first argument; put parentheses or a space even after `-' operator
Syntax OK

ソースコード

diff #
プレゼンテーションモードにする

N, M, P = gets.split.map(&:to_i)
A = gets.split.map(&:to_i)
AM = A.max
values = Array.new(Math.log2(M).ceil + 10, 0)
A.each do |a|
cost = 1
e = a
while e % P == 0
e /= P
cost += 1
end
next if e == 1
if values[cost] < e
values[cost] = e
end
end
L = Math.log2(M).ceil ** 2 + 10
dp = Array.new(L + 1, 0)
dp[0] = 1
values.each.with_index do |e, cost|
next if e == 0
0.upto(L) do |i|
next if dp[i] == 0
next if dp[i] > M
nv = i + cost
x = dp[i] * e
if dp[nv] < x
dp[nv] = x
end
end
end
0.upto(L) do |cost|
if dp[cost] * AM > M
puts cost + 1
exit
end
end
puts -1
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0