結果

問題 No.2039 Copy and Avoid
ユーザー LyricalMaestro
提出日時 2024-08-17 20:00:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 220 ms / 2,000 ms
コード長 1,724 bytes
コンパイル時間 491 ms
コンパイル使用メモリ 82,688 KB
実行使用メモリ 74,112 KB
最終ジャッジ日時 2024-08-17 20:00:40
合計ジャッジ時間 3,876 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

## https://yukicoder.me/problems/no/2039

import math

MAX_INT = 10 ** 18

def main():
    N, M, A, B = map(int, input().split())
    C = list(map(int, input().split()))

    # 約数列挙
    sqrt_n = int(math.sqrt(N))
    divisors = []
    for p in range(1, sqrt_n + 1):
        if N % p == 0:
            q = N // p
            divisors.append(p)
            if q != p:
                divisors.append(q)
    divisors.sort()

    # 約数群と通過しては行けない数字の和集合を求める
    target_set = set(C)
    for d in divisors:
        target_set.add(d)
    target_map = {t: True for t in target_set}
    for c in C:
        target_map[c] = False
    target_array = [(key, value) for key, value in target_map.items()]
    target_array.sort(key=lambda x : x[0])

    a_dp = {1:0}
    b_dp = {}
    for d_index in range(len(divisors)):
        d = divisors[d_index]
        if d == 1:
            b_dp[d] = 0
        elif d == N:
            break
        else:
            if d in a_dp and a_dp[d] < MAX_INT:
                b_dp[d] = B + a_dp[d]
            else:
                continue
        
        # 操作aを使ってどれだけ行けるか?
        for t, available in target_array:
            if t <= d:
                continue

            if t % d == 0:
                if not available:
                    break

                x = t // d
                cost = b_dp[d] + (x - 1) * A
                if t not in a_dp:
                    a_dp[t] = MAX_INT
                a_dp[t] = min(a_dp[t], cost)
        
    if N in a_dp and a_dp[N] < MAX_INT:
        print(a_dp[N])
    else:
        print(-1)


                


    





        

if __name__ == "__main__":
    main()
0