結果
問題 |
No.458 異なる素数の和
|
ユーザー |
|
提出日時 | 2017-03-30 21:04:33 |
言語 | Java (openjdk 23) |
結果 |
WA
|
実行時間 | - |
コード長 | 894 bytes |
コンパイル時間 | 3,747 ms |
コンパイル使用メモリ | 78,196 KB |
実行使用メモリ | 42,112 KB |
最終ジャッジ日時 | 2024-07-07 03:12:55 |
合計ジャッジ時間 | 8,987 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 WA * 9 |
ソースコード
import java.util.ArrayList; import java.util.Scanner; public class DifferentSosu { public static void main(String[] args) { Scanner s = new Scanner(System.in); int N = s.nextInt(); s.close(); ArrayList<Integer> p = new ArrayList<>(); p.add(2); for(int i = 3;i <= N;i++){ int a = 1; for(int j = 0;j < p.size();j++){ if(i % p.get(j) == 0){ a = 0; break; } } if(a == 1){ p.add(i); } } //System.out.println(p); int[] root = new int[N+1]; root[0] = 0; root[1] = -1; for(int i:p){ for(int j = N;j >= 0;j--){ if(j-i >= 0&&root[j-i] != -1){ root[j] = Math.max(root[j-i] + 1, root[j]); if(root[j] == 1 && !p.contains(j)){ root[j] = -1; } } } /*for(int k:root){ System.out.print(k + " "); } System.out.println();*/ } System.out.println(root[N]); //System.out.println(t.get(N)); } }