結果
問題 | No.1330 Multiply or Divide |
ユーザー | tenten |
提出日時 | 2021-08-04 12:48:48 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 366 ms / 2,000 ms |
コード長 | 2,481 bytes |
コンパイル時間 | 2,324 ms |
コンパイル使用メモリ | 78,632 KB |
実行使用メモリ | 52,508 KB |
最終ジャッジ日時 | 2024-09-16 15:06:01 |
合計ジャッジ時間 | 12,424 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 54 ms
36,860 KB |
testcase_01 | AC | 304 ms
47,780 KB |
testcase_02 | AC | 53 ms
37,068 KB |
testcase_03 | AC | 53 ms
36,508 KB |
testcase_04 | AC | 53 ms
36,836 KB |
testcase_05 | AC | 54 ms
36,524 KB |
testcase_06 | AC | 253 ms
46,056 KB |
testcase_07 | AC | 366 ms
48,344 KB |
testcase_08 | AC | 317 ms
50,876 KB |
testcase_09 | AC | 323 ms
50,152 KB |
testcase_10 | AC | 322 ms
50,336 KB |
testcase_11 | AC | 316 ms
50,116 KB |
testcase_12 | AC | 324 ms
50,380 KB |
testcase_13 | AC | 53 ms
36,968 KB |
testcase_14 | AC | 52 ms
37,072 KB |
testcase_15 | AC | 52 ms
36,840 KB |
testcase_16 | AC | 53 ms
37,068 KB |
testcase_17 | AC | 51 ms
36,512 KB |
testcase_18 | AC | 328 ms
50,136 KB |
testcase_19 | AC | 324 ms
50,272 KB |
testcase_20 | AC | 324 ms
50,448 KB |
testcase_21 | AC | 328 ms
50,284 KB |
testcase_22 | AC | 319 ms
50,404 KB |
testcase_23 | AC | 330 ms
52,508 KB |
testcase_24 | AC | 56 ms
37,080 KB |
testcase_25 | AC | 54 ms
36,824 KB |
testcase_26 | AC | 54 ms
37,052 KB |
testcase_27 | AC | 56 ms
36,912 KB |
testcase_28 | AC | 55 ms
36,536 KB |
testcase_29 | AC | 55 ms
37,068 KB |
testcase_30 | AC | 54 ms
36,944 KB |
testcase_31 | AC | 55 ms
37,048 KB |
testcase_32 | AC | 54 ms
37,092 KB |
testcase_33 | AC | 59 ms
36,632 KB |
testcase_34 | AC | 295 ms
47,988 KB |
testcase_35 | AC | 181 ms
43,808 KB |
testcase_36 | AC | 278 ms
47,648 KB |
testcase_37 | AC | 276 ms
46,664 KB |
testcase_38 | AC | 225 ms
45,132 KB |
testcase_39 | AC | 200 ms
44,212 KB |
testcase_40 | AC | 211 ms
44,596 KB |
testcase_41 | AC | 160 ms
42,448 KB |
testcase_42 | AC | 268 ms
46,296 KB |
testcase_43 | AC | 272 ms
47,428 KB |
testcase_44 | AC | 259 ms
46,128 KB |
testcase_45 | AC | 260 ms
45,788 KB |
ソースコード
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(); } }