結果

問題 No.1330 Multiply or Divide
ユーザー simansiman
提出日時 2022-02-28 07:28:59
言語 Ruby
(3.3.0)
結果
AC  
実行時間 370 ms / 2,000 ms
コード長 643 bytes
コンパイル時間 273 ms
コンパイル使用メモリ 11,512 KB
実行使用メモリ 28,696 KB
最終ジャッジ日時 2023-09-20 15:05:52
合計ジャッジ時間 8,787 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
15,340 KB
testcase_01 AC 177 ms
28,072 KB
testcase_02 AC 77 ms
15,056 KB
testcase_03 AC 77 ms
15,184 KB
testcase_04 AC 78 ms
15,308 KB
testcase_05 AC 80 ms
15,340 KB
testcase_06 AC 174 ms
26,952 KB
testcase_07 AC 370 ms
28,040 KB
testcase_08 AC 186 ms
28,604 KB
testcase_09 AC 196 ms
28,608 KB
testcase_10 AC 187 ms
28,536 KB
testcase_11 AC 183 ms
28,624 KB
testcase_12 AC 191 ms
28,500 KB
testcase_13 AC 81 ms
15,180 KB
testcase_14 AC 81 ms
15,180 KB
testcase_15 AC 81 ms
15,392 KB
testcase_16 AC 81 ms
15,416 KB
testcase_17 AC 80 ms
15,368 KB
testcase_18 AC 178 ms
28,556 KB
testcase_19 AC 178 ms
28,532 KB
testcase_20 AC 180 ms
28,620 KB
testcase_21 AC 178 ms
28,472 KB
testcase_22 AC 178 ms
28,612 KB
testcase_23 AC 180 ms
28,696 KB
testcase_24 AC 81 ms
15,344 KB
testcase_25 AC 80 ms
15,312 KB
testcase_26 AC 80 ms
15,172 KB
testcase_27 AC 80 ms
15,300 KB
testcase_28 AC 80 ms
15,324 KB
testcase_29 AC 79 ms
15,320 KB
testcase_30 AC 79 ms
15,372 KB
testcase_31 AC 79 ms
15,412 KB
testcase_32 AC 80 ms
15,168 KB
testcase_33 AC 81 ms
15,384 KB
testcase_34 AC 156 ms
25,268 KB
testcase_35 AC 102 ms
17,680 KB
testcase_36 AC 149 ms
24,144 KB
testcase_37 AC 143 ms
23,312 KB
testcase_38 AC 115 ms
20,168 KB
testcase_39 AC 109 ms
18,856 KB
testcase_40 AC 111 ms
19,420 KB
testcase_41 AC 99 ms
17,084 KB
testcase_42 AC 138 ms
22,604 KB
testcase_43 AC 149 ms
23,688 KB
testcase_44 AC 178 ms
27,056 KB
testcase_45 AC 181 ms
27,104 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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
0