結果

問題 No.1330 Multiply or Divide
ユーザー tktk_snsn
提出日時 2021-09-25 13:37:30
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 839 ms / 2,000 ms
コード長 1,009 bytes
コンパイル時間 344 ms
コンパイル使用メモリ 12,288 KB
実行使用メモリ 54,260 KB
最終ジャッジ日時 2025-06-20 01:38:33
合計ジャッジ時間 8,760 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 51
権限があれば一括ダウンロードができます

ソースコード

diff #

import bisect
from collections import defaultdict
inf = 10 ** 18


N, M, P = map(int, input().split())
A = set(map(int, input().split()))
A = list(A)


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))

if not item:
    print(-1)
    exit()

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)
0