結果

問題 No.1330 Multiply or Divide
ユーザー LyricalMaestro
提出日時 2025-07-27 17:54:11
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 1,225 bytes
コンパイル時間 856 ms
コンパイル使用メモリ 82,364 KB
実行使用メモリ 678,352 KB
最終ジャッジ日時 2025-07-27 17:54:17
合計ジャッジ時間 4,967 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 1 MLE * 1 -- * 49
権限があれば一括ダウンロードができます

ソースコード

diff #

## https://yukicoder.me/problems/no/1443

import heapq


def main():
    N, M, P = map(int, input().split())
    A = list(map(int, input().split()))

    max_a = max(A)
    max_m = M // max_a + (1 if M % max_a > 0 else 0)

    candidates = {}
    for a in A:
        a1 = a
        x = 0
        while a1 % P == 0:
            x += 1
            a1 //= P
        
        if a1 not in candidates:
            candidates[a1] = float("inf")
        candidates[a1] = min(candidates[a1], 1 + x)

    seen = {1: 0}
    fix = {}
    queue = []
    heapq.heappush(queue, (0, 1))
    answer = float("inf")
    while len(queue) > 0:
        cost, v = heapq.heappop(queue)
        if v in fix:
            continue
        fix[v] = cost

        if v >= max_m:
            answer = min(answer, cost + 1)
            continue

        for b, cos in candidates.items():
            w = v * b
            if w in fix:
                continue

            new_cost = cost + cos
            if w not in seen or seen[w] > new_cost:
                seen[w] = new_cost
                heapq.heappush(queue, (new_cost, w))
    if answer == float("inf"):
        print(-1)
    else:
        print(answer)


if __name__ == "__main__":
    main()
0