結果

問題 No.2039 Copy and Avoid
ユーザー tktk_snsntktk_snsn
提出日時 2022-08-13 12:54:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 353 ms / 2,000 ms
コード長 1,017 bytes
コンパイル時間 1,837 ms
コンパイル使用メモリ 81,648 KB
実行使用メモリ 90,488 KB
最終ジャッジ日時 2023-10-24 21:35:30
合計ジャッジ時間 4,737 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 345 ms
90,460 KB
testcase_01 AC 41 ms
53,396 KB
testcase_02 AC 55 ms
66,248 KB
testcase_03 AC 41 ms
53,396 KB
testcase_04 AC 41 ms
53,396 KB
testcase_05 AC 59 ms
66,276 KB
testcase_06 AC 41 ms
53,396 KB
testcase_07 AC 41 ms
53,396 KB
testcase_08 AC 41 ms
53,396 KB
testcase_09 AC 41 ms
53,396 KB
testcase_10 AC 41 ms
53,396 KB
testcase_11 AC 41 ms
53,396 KB
testcase_12 AC 44 ms
59,220 KB
testcase_13 AC 342 ms
90,488 KB
testcase_14 AC 353 ms
90,472 KB
testcase_15 AC 342 ms
90,440 KB
testcase_16 AC 324 ms
89,132 KB
testcase_17 AC 49 ms
62,128 KB
testcase_18 AC 57 ms
66,248 KB
testcase_19 AC 49 ms
62,140 KB
testcase_20 AC 77 ms
70,452 KB
testcase_21 AC 45 ms
59,512 KB
testcase_22 AC 41 ms
53,396 KB
testcase_23 AC 41 ms
53,396 KB
testcase_24 AC 40 ms
53,396 KB
testcase_25 AC 40 ms
53,396 KB
testcase_26 AC 40 ms
53,396 KB
testcase_27 AC 55 ms
64,088 KB
testcase_28 AC 41 ms
53,396 KB
testcase_29 AC 40 ms
53,396 KB
testcase_30 AC 40 ms
53,396 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import bisect
import heapq
inf = 2 * 10 ** 18


def gen_div(N):
    yield 1
    for i in range(2, N+1):
        if i * i > N:
            break
        if N % i == 0:
            yield i
            if i * i != N:
                yield N // i
    yield N


N, M, A, B = map(int, input().split())
C = sorted(map(int, input().split()))

ans = inf
divisors_of_N = sorted(gen_div(N))
K = len(divisors_of_N)
dtoi = {d: i for i, d in enumerate(divisors_of_N)}


dp = [[inf] * K for _ in range(K)]
dp[0][0] = 0
for i in range(K):
    for j in range(i):
        dp[i][i] = min(dp[i][i], dp[i][j] + B)

    di = divisors_of_N[i]
    NG = []
    for c in C:
        if c % di == 0:
            NG.append(c)

    for j in range(i+1, K):
        dj = divisors_of_N[j]
        if dj % di != 0:
            continue
        if bisect.bisect_right(NG, dj) - bisect.bisect_left(NG, di) > 0:
            break
        dp[j][i] = min(dp[j][i], dp[i][i] + (dj - di) // di * A)


ans = min(dp[K-1])
if ans >= inf:
    ans = -1
print(ans)
0