結果
問題 |
No.458 異なる素数の和
|
ユーザー |
![]() |
提出日時 | 2020-12-14 14:17:08 |
言語 | Java (openjdk 23) |
結果 |
TLE
|
実行時間 | - |
コード長 | 825 bytes |
コンパイル時間 | 2,558 ms |
コンパイル使用メモリ | 77,224 KB |
実行使用メモリ | 41,876 KB |
最終ジャッジ日時 | 2024-09-20 00:42:28 |
合計ジャッジ時間 | 6,129 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | -- * 3 |
other | AC * 1 TLE * 1 -- * 26 |
ソースコード
import java.util.*; public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); boolean[] isNotPrime = new boolean[n + 1]; TreeMap<Integer, Integer> map = new TreeMap<>(); map.put(0, 0); for (int i = 2; i <= n; i++) { if (!isNotPrime[i]) { for (int j = 2; j * i <= n; j++) { isNotPrime[j * i] = true; } Integer x = n + 1; while ((x = map.lowerKey(x)) != null) { int value = map.get(x); if (x + i <= n && (!map.containsKey(x + i) || map.get(x + i) <= value)) { map.put(x + i, value + 1); } } } } if (!map.containsKey(n)) { System.out.println(-1); } else { System.out.println(map.get(n)); } } }