結果

問題 No.1330 Multiply or Divide
ユーザー tentententen
提出日時 2021-08-04 12:48:48
言語 Java
(openjdk 23)
結果
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
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 46
権限があれば一括ダウンロードができます

ソースコード

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();
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0