結果

問題 No.2039 Copy and Avoid
ユーザー sotanishysotanishy
提出日時 2022-08-12 23:15:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 278 ms / 2,000 ms
コード長 1,009 bytes
コンパイル時間 391 ms
コンパイル使用メモリ 81,708 KB
実行使用メモリ 79,328 KB
最終ジャッジ日時 2023-10-24 11:35:49
合計ジャッジ時間 3,633 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 253 ms
79,044 KB
testcase_01 AC 36 ms
53,476 KB
testcase_02 AC 46 ms
62,076 KB
testcase_03 AC 35 ms
53,476 KB
testcase_04 AC 34 ms
53,476 KB
testcase_05 AC 43 ms
62,076 KB
testcase_06 AC 36 ms
53,476 KB
testcase_07 AC 34 ms
53,476 KB
testcase_08 AC 36 ms
53,476 KB
testcase_09 AC 35 ms
53,476 KB
testcase_10 AC 36 ms
53,476 KB
testcase_11 AC 34 ms
53,476 KB
testcase_12 AC 38 ms
59,296 KB
testcase_13 AC 267 ms
79,000 KB
testcase_14 AC 278 ms
79,328 KB
testcase_15 AC 259 ms
79,212 KB
testcase_16 AC 248 ms
78,516 KB
testcase_17 AC 43 ms
62,076 KB
testcase_18 AC 45 ms
62,076 KB
testcase_19 AC 42 ms
62,076 KB
testcase_20 AC 60 ms
66,784 KB
testcase_21 AC 38 ms
53,476 KB
testcase_22 AC 36 ms
53,476 KB
testcase_23 AC 37 ms
53,476 KB
testcase_24 AC 37 ms
53,476 KB
testcase_25 AC 37 ms
53,476 KB
testcase_26 AC 36 ms
53,476 KB
testcase_27 AC 87 ms
76,396 KB
testcase_28 AC 37 ms
53,476 KB
testcase_29 AC 36 ms
53,476 KB
testcase_30 AC 37 ms
53,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappush, heappop
import sys
input = sys.stdin.readline

def divisor(n):
    divisors = []
    i = 1
    while i * i < n:
        if n % i == 0:
            divisors.append(i)
            divisors.append(n // i)
        i += 1
    if i * i == n:
        divisors.append(i)
    divisors.sort()
    return divisors

N, M, A, B = map(int, input().split())
C = list(map(int, input().split()))
C.sort()
div = divisor(N)
leastmultiple = {}
for d in div:
    for c in C:
        if c % d == 0:
            leastmultiple[d] = c
            break
dist = {}
heap = [(0, N)]
dist[N] = 0
while heap:
    d, n = heappop(heap)
    if n == 1:
        print(d-B)
        exit()
    if dist[n] < d:
        continue
    for m in div:
        if m >= n:
            break
        if n % m > 0 or m in leastmultiple and leastmultiple[m] < n:
            continue
        nd = d + (n//m-1) * A + B
        if m not in dist or nd < dist[m]:
            dist[m] = nd
            heappush(heap, (nd, m))
print(-1)
0