結果
| 問題 |
No.1330 Multiply or Divide
|
| コンテスト | |
| ユーザー |
tktk_snsn
|
| 提出日時 | 2021-09-25 13:33:29 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 996 bytes |
| コンパイル時間 | 212 ms |
| コンパイル使用メモリ | 12,672 KB |
| 実行使用メモリ | 44,952 KB |
| 最終ジャッジ日時 | 2024-07-05 12:04:42 |
| 合計ジャッジ時間 | 4,948 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 4 WA * 1 RE * 2 TLE * 1 -- * 38 |
ソースコード
import bisect
from collections import defaultdict
inf = 10 ** 18
N, M, P = map(int, input().split())
A = list(map(int, input().split()))
if M == 1:
print(0)
exit()
last = max(A)
if last >= M:
print(1)
exit()
WV = defaultdict(lambda: inf)
for a in A:
cnt = 1
while a % P == 0:
a //= P
cnt += 1
if a == 1:
continue
x = a
cost = cnt
while x < M:
WV[x] = min(WV[x], cost)
cost += cnt
x *= a
WV = list(WV.items())
WV.sort()
item = []
for w, v in sorted(WV):
while item and item[-1][1] >= v:
item.pop()
item.append((w, v))
ans = inf
W, V = zip(*item)
for w, v in item:
ww = w * last
if ww >= M:
ans = min(ans, v + 1)
continue
target = (M + ww - 1) // ww
i = bisect.bisect_right(W, target)
for j in range(-1, 2):
if 0 <= i + j < len(item) and ww * W[i+j] >= M:
ans = min(ans, v + V[i+j] + 1)
if ans >= inf:
ans = -1
print(ans)
tktk_snsn