結果

問題 No.1330 Multiply or Divide
ユーザー qibqib
提出日時 2023-04-24 01:28:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 123 ms / 2,000 ms
コード長 918 bytes
コンパイル時間 441 ms
コンパイル使用メモリ 82,484 KB
実行使用メモリ 115,496 KB
最終ジャッジ日時 2024-04-25 18:45:05
合計ジャッジ時間 6,125 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
53,332 KB
testcase_01 AC 102 ms
109,072 KB
testcase_02 AC 37 ms
54,336 KB
testcase_03 AC 36 ms
53,316 KB
testcase_04 AC 36 ms
53,860 KB
testcase_05 AC 36 ms
54,612 KB
testcase_06 AC 97 ms
115,344 KB
testcase_07 AC 114 ms
108,952 KB
testcase_08 AC 120 ms
114,844 KB
testcase_09 AC 120 ms
114,596 KB
testcase_10 AC 117 ms
114,588 KB
testcase_11 AC 123 ms
114,560 KB
testcase_12 AC 122 ms
114,596 KB
testcase_13 AC 83 ms
76,648 KB
testcase_14 AC 61 ms
69,112 KB
testcase_15 AC 37 ms
53,136 KB
testcase_16 AC 60 ms
69,624 KB
testcase_17 AC 50 ms
65,088 KB
testcase_18 AC 93 ms
102,312 KB
testcase_19 AC 94 ms
102,032 KB
testcase_20 AC 90 ms
102,200 KB
testcase_21 AC 91 ms
102,208 KB
testcase_22 AC 93 ms
102,372 KB
testcase_23 AC 114 ms
114,528 KB
testcase_24 AC 37 ms
54,284 KB
testcase_25 AC 36 ms
53,008 KB
testcase_26 AC 38 ms
54,268 KB
testcase_27 AC 37 ms
54,468 KB
testcase_28 AC 35 ms
53,168 KB
testcase_29 AC 34 ms
53,444 KB
testcase_30 AC 34 ms
52,928 KB
testcase_31 AC 35 ms
53,748 KB
testcase_32 AC 34 ms
53,696 KB
testcase_33 AC 34 ms
53,632 KB
testcase_34 AC 76 ms
100,596 KB
testcase_35 AC 47 ms
72,444 KB
testcase_36 AC 69 ms
95,052 KB
testcase_37 AC 63 ms
90,752 KB
testcase_38 AC 56 ms
79,172 KB
testcase_39 AC 48 ms
72,728 KB
testcase_40 AC 55 ms
76,032 KB
testcase_41 AC 46 ms
68,324 KB
testcase_42 AC 66 ms
89,016 KB
testcase_43 AC 70 ms
93,912 KB
testcase_44 AC 95 ms
115,276 KB
testcase_45 AC 95 ms
115,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

INF = 1 << 60

n, m, p = map(int, input().split())
a = list(map(int, input().split()))

if any([x > m for x in a]):
  print("1")
  exit()

idx = {}
q = []
for i in range(n):
  cur = a[i]
  cnt = 1
  while cur % p == 0:
    cnt += 1
    cur //= p

  q.append((a[i], cur))

  if idx.get(cnt, None) is None or a[idx[cnt]] < a[i]:
    idx[cnt] = i

dist = {}
dist[1] = 0
hp = [(0, 1)]
ans = INF
while len(hp) > 0:
  cd, cur = heapq.heappop(hp)
  if cd > dist[cur]:
    continue

  for cost, i in idx.items():
    ai, d = q[i]
    nxt = cur * ai
    if dist.get(nxt, INF) > dist[cur] + 1:
      if nxt > m:
        ans = min(ans, dist[cur] + 1)
    nxt = cur * d
    if dist.get(nxt, INF) > dist[cur] + cost:
      if nxt > m:
        ans = min(ans, dist[cur] + cost)
      else:
        dist[nxt] = dist[cur] + cost
        heapq.heappush(hp, (dist[nxt], nxt))

if ans < INF:
  print(ans)
else:
  print("-1")
0