結果

問題 No.1330 Multiply or Divide
ユーザー tentententen
提出日時 2021-08-04 12:48:48
言語 Java21
(openjdk 21)
結果
AC  
実行時間 350 ms / 2,000 ms
コード長 2,481 bytes
コンパイル時間 2,467 ms
コンパイル使用メモリ 75,364 KB
実行使用メモリ 63,344 KB
最終ジャッジ日時 2023-10-14 21:19:28
合計ジャッジ時間 12,755 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
49,284 KB
testcase_01 AC 294 ms
58,024 KB
testcase_02 AC 45 ms
49,220 KB
testcase_03 AC 45 ms
49,248 KB
testcase_04 AC 45 ms
47,500 KB
testcase_05 AC 46 ms
49,444 KB
testcase_06 AC 236 ms
57,448 KB
testcase_07 AC 350 ms
59,504 KB
testcase_08 AC 301 ms
60,692 KB
testcase_09 AC 307 ms
61,028 KB
testcase_10 AC 307 ms
60,640 KB
testcase_11 AC 302 ms
60,064 KB
testcase_12 AC 310 ms
60,208 KB
testcase_13 AC 48 ms
49,356 KB
testcase_14 AC 46 ms
49,392 KB
testcase_15 AC 46 ms
49,308 KB
testcase_16 AC 47 ms
49,420 KB
testcase_17 AC 46 ms
49,348 KB
testcase_18 AC 312 ms
60,840 KB
testcase_19 AC 308 ms
60,840 KB
testcase_20 AC 305 ms
61,324 KB
testcase_21 AC 309 ms
60,968 KB
testcase_22 AC 306 ms
60,732 KB
testcase_23 AC 314 ms
63,344 KB
testcase_24 AC 45 ms
49,428 KB
testcase_25 AC 46 ms
49,324 KB
testcase_26 AC 45 ms
49,280 KB
testcase_27 AC 46 ms
49,344 KB
testcase_28 AC 45 ms
49,276 KB
testcase_29 AC 46 ms
49,472 KB
testcase_30 AC 47 ms
49,504 KB
testcase_31 AC 46 ms
49,272 KB
testcase_32 AC 46 ms
49,292 KB
testcase_33 AC 47 ms
49,448 KB
testcase_34 AC 276 ms
58,256 KB
testcase_35 AC 178 ms
55,196 KB
testcase_36 AC 265 ms
58,524 KB
testcase_37 AC 254 ms
58,256 KB
testcase_38 AC 213 ms
55,904 KB
testcase_39 AC 190 ms
55,400 KB
testcase_40 AC 196 ms
55,452 KB
testcase_41 AC 142 ms
54,516 KB
testcase_42 AC 256 ms
58,288 KB
testcase_43 AC 264 ms
57,828 KB
testcase_44 AC 248 ms
57,368 KB
testcase_45 AC 246 ms
57,124 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int m = sc.nextInt();
        int p = sc.nextInt();
        int max = 0;
        TreeMap<Integer, Integer> map = new TreeMap<>();
        for (int i = 0; i < n; i++) {
            int x = sc.nextInt();
            max =  Math.max(max, x);
            int count = 0;
            while (x % p == 0) {
                x /= p;
                count++;
            }
            map.put(count, Math.max(map.getOrDefault(count, 0), x));
        }
        boolean all1 = true;
        for (int x : map.values()) {
            if (x > 1) {
                all1 = false;
                break;
            }
        }
        int limit = m / max;
        if (limit < 1) {
            System.out.println(1);
            return;
        }
        if (all1) {
            System.out.println(-1);
            return;
        }
        int ans = Integer.MAX_VALUE;
        Integer current = 0;
        TreeMap<Integer, Integer> dp = new TreeMap<>();
        dp.put(1, 0);
        while ((current = dp.higherKey(current)) != null) {
            for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
                int count = entry.getKey() + 1;
                int value = entry.getValue();
                if (value == 1) {
                    continue;
                }
                if (current * (long)value > limit) {
                    ans = Math.min(ans, dp.get(current) + count);
                } else {
                    if (!dp.containsKey(current * value) || dp.get(current * value) > dp.get(current) + count) {
                        dp.put(current * value, dp.get(current) + count);
                    }
                }
            }
        }
        System.out.println(ans + 1);
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0