結果

問題 No.2039 Copy and Avoid
ユーザー tentententen
提出日時 2023-02-07 19:49:29
言語 Java21
(openjdk 21)
結果
AC  
実行時間 474 ms / 2,000 ms
コード長 3,011 bytes
コンパイル時間 8,288 ms
コンパイル使用メモリ 81,316 KB
実行使用メモリ 63,500 KB
最終ジャッジ日時 2023-09-19 02:05:26
合計ジャッジ時間 15,935 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 474 ms
63,500 KB
testcase_01 AC 46 ms
49,972 KB
testcase_02 AC 90 ms
52,584 KB
testcase_03 AC 47 ms
49,588 KB
testcase_04 AC 47 ms
49,596 KB
testcase_05 AC 98 ms
52,456 KB
testcase_06 AC 47 ms
49,540 KB
testcase_07 AC 46 ms
49,468 KB
testcase_08 AC 45 ms
47,748 KB
testcase_09 AC 46 ms
49,596 KB
testcase_10 AC 47 ms
49,672 KB
testcase_11 AC 47 ms
49,668 KB
testcase_12 AC 48 ms
49,492 KB
testcase_13 AC 340 ms
59,812 KB
testcase_14 AC 358 ms
57,944 KB
testcase_15 AC 353 ms
59,876 KB
testcase_16 AC 320 ms
60,264 KB
testcase_17 AC 93 ms
52,228 KB
testcase_18 AC 98 ms
52,724 KB
testcase_19 AC 83 ms
52,288 KB
testcase_20 AC 115 ms
53,880 KB
testcase_21 AC 49 ms
49,432 KB
testcase_22 AC 47 ms
49,384 KB
testcase_23 AC 46 ms
49,548 KB
testcase_24 AC 48 ms
49,640 KB
testcase_25 AC 47 ms
49,476 KB
testcase_26 AC 46 ms
49,580 KB
testcase_27 AC 63 ms
51,792 KB
testcase_28 AC 45 ms
49,540 KB
testcase_29 AC 46 ms
49,576 KB
testcase_30 AC 45 ms
49,748 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    static HashMap<Integer, Integer> unables = new HashMap<>();
    static HashMap<Integer, Long> dp = new HashMap<>();
    static long a;
    static long b;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int m = sc.nextInt();
        a = sc.nextInt();
        b = sc.nextInt();
        int[] values = new int[m];
        for (int i = 0; i < m; i++) {
            values[i] = sc.nextInt();
        }
        Arrays.sort(values);
        for (int i = 1; i <= Math.sqrt(n); i++) {
            if (n % i == 0) {
                unables.put(i, Integer.MAX_VALUE);
                if (i != 1 && i * i < n) {
                    unables.put(n / i, Integer.MAX_VALUE);
                }
            }
        }
        for (int x : unables.keySet()) {
            for (int y : values) {
                if (y % x == 0) {
                    unables.put(x, y);
                    break;
                }
            }
        }
        dp.put(1, 0L);
        long ans = dfw(n);
        if (ans >= Long.MAX_VALUE / 2) {
            System.out.println(-1);
        } else {
            System.out.println(ans - b);
        }
    }
    
    static long dfw(int x) {
        if (!dp.containsKey(x)) {
            long ans = Long.MAX_VALUE / 2;
            for (int y : unables.keySet()) {
                if (x == y || x % y > 0) {
                    continue;
                }
                if (unables.get(y) <= x) {
                    continue;
                }
                ans = Math.min(ans, dfw(y) + (x / y - 1) * a + b);
            }
            dp.put(x, ans);
        }
        return dp.get(x);
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0