結果

問題 No.1330 Multiply or Divide
ユーザー c-yan
提出日時 2021-01-10 00:52:52
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 560 ms / 2,000 ms
コード長 632 bytes
コンパイル時間 175 ms
コンパイル使用メモリ 12,288 KB
実行使用メモリ 32,260 KB
最終ジャッジ日時 2025-06-20 01:26:11
合計ジャッジ時間 6,503 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 51
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

INF = 10 ** 20

N, M, P, *A = map(int, open(0).read().split())

def f(x):
    c = 0
    while x % P == 0:
        c += 1
        x //= P
    return (x, c)

max_A = max(A)

if max_A > M:
    print(1)
    exit()

b = {}
for m, c in map(f, A):
    b.setdefault(c, 0)
    b[c] = max(b[c], m)
t = 1
for k in sorted(b):
    if b[k] <= t:
        del b[k]
    else:
        t = b[k]
cs = sorted(b)

if len(cs) == 0:
    print(-1)
    exit()

q = deque([(0, 1)])
while q:
    c, x = q.popleft()
    if x * max_A > M:
        print(c + 1)
        break
    for k in cs:
        q.append((c + 1 + k, x * b[k]))
0