結果
| 問題 |
No.458 異なる素数の和
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2021-11-02 10:18:36 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 124 ms / 2,000 ms |
| コード長 | 1,437 bytes |
| コンパイル時間 | 2,363 ms |
| コンパイル使用メモリ | 76,608 KB |
| 実行使用メモリ | 38,140 KB |
| 最終ジャッジ日時 | 2024-10-11 08:28:49 |
| 合計ジャッジ時間 | 5,574 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 28 |
ソースコード
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[] dp = new int[n + 1];
dp[0] = 1;
boolean[] isNotPrime = new boolean[n + 1];
for (int i = 2; i <= n; i++) {
if (isNotPrime[i]) {
continue;
}
for (int j = n - i; j >= 0; j--) {
if (dp[j] == 0) {
continue;
}
dp[j + i] = Math.max(dp[j + i], dp[j] + 1);
}
for (int j = 2; j * i <= n; j++) {
isNotPrime[i * j] = true;
}
}
System.out.println(dp[n] - 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 nextLine() throws Exception {
return br.readLine();
}
public String next() throws Exception {
if (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
}
tenten