結果

問題 No.1330 Multiply or Divide
ユーザー LyricalMaestro
提出日時 2025-07-27 17:46:06
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 1,069 bytes
コンパイル時間 200 ms
コンパイル使用メモリ 82,348 KB
実行使用メモリ 560,496 KB
最終ジャッジ日時 2025-07-27 17:46:12
合計ジャッジ時間 5,791 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()))

    candidates = []
    for a in A:
        a1 = a
        x = 0
        while a1 % P == 0:
            x += 1
            a1 //= P
        
        candidates.append((a, 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

        for a, b, x in candidates:
            if v * a > M:
                answer = min(answer, cost + 1)
                continue

            w = v * b
            if w in fix:
                continue

            new_cost = cost + x
            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