結果

問題 No.1330 Multiply or Divide
ユーザー tktk_snsntktk_snsn
提出日時 2021-09-25 13:37:30
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 945 ms / 2,000 ms
コード長 1,009 bytes
コンパイル時間 329 ms
コンパイル使用メモリ 10,824 KB
実行使用メモリ 52,460 KB
最終ジャッジ日時 2023-09-18 23:01:04
合計ジャッジ時間 9,651 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,744 KB
testcase_01 AC 567 ms
49,508 KB
testcase_02 AC 19 ms
8,684 KB
testcase_03 AC 19 ms
8,564 KB
testcase_04 AC 19 ms
8,572 KB
testcase_05 AC 19 ms
8,732 KB
testcase_06 AC 51 ms
10,940 KB
testcase_07 AC 76 ms
24,044 KB
testcase_08 AC 901 ms
51,468 KB
testcase_09 AC 945 ms
52,460 KB
testcase_10 AC 919 ms
51,448 KB
testcase_11 AC 922 ms
50,524 KB
testcase_12 AC 931 ms
51,276 KB
testcase_13 AC 20 ms
8,752 KB
testcase_14 AC 19 ms
8,740 KB
testcase_15 AC 19 ms
8,620 KB
testcase_16 AC 19 ms
8,756 KB
testcase_17 AC 20 ms
8,684 KB
testcase_18 AC 140 ms
40,792 KB
testcase_19 AC 137 ms
41,852 KB
testcase_20 AC 136 ms
41,916 KB
testcase_21 AC 140 ms
40,676 KB
testcase_22 AC 137 ms
40,784 KB
testcase_23 AC 84 ms
24,548 KB
testcase_24 AC 20 ms
8,556 KB
testcase_25 AC 21 ms
8,572 KB
testcase_26 AC 20 ms
8,680 KB
testcase_27 AC 20 ms
8,700 KB
testcase_28 AC 20 ms
8,616 KB
testcase_29 AC 20 ms
8,684 KB
testcase_30 AC 20 ms
8,684 KB
testcase_31 AC 20 ms
8,556 KB
testcase_32 AC 19 ms
8,616 KB
testcase_33 AC 19 ms
8,564 KB
testcase_34 AC 97 ms
28,532 KB
testcase_35 AC 39 ms
14,936 KB
testcase_36 AC 89 ms
27,816 KB
testcase_37 AC 80 ms
26,524 KB
testcase_38 AC 53 ms
18,880 KB
testcase_39 AC 44 ms
16,076 KB
testcase_40 AC 50 ms
17,396 KB
testcase_41 AC 35 ms
14,056 KB
testcase_42 AC 78 ms
25,444 KB
testcase_43 AC 84 ms
27,240 KB
testcase_44 AC 52 ms
10,940 KB
testcase_45 AC 52 ms
10,944 KB
権限があれば一括ダウンロードができます

ソースコード

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