結果
問題 | No.458 異なる素数の和 |
ユーザー |
![]() |
提出日時 | 2020-04-28 09:27:15 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 279 ms / 2,000 ms |
コード長 | 2,136 bytes |
コンパイル時間 | 2,361 ms |
コンパイル使用メモリ | 82,808 KB |
実行使用メモリ | 43,452 KB |
最終ジャッジ日時 | 2024-11-24 03:02:41 |
合計ジャッジ時間 | 8,590 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 28 |
ソースコード
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; import java.util.ArrayList; /** * Built using CHelper plug-in * Actual solution is at the top * * @author silviase */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); DifferentSumOfPrime solver = new DifferentSumOfPrime(); solver.solve(1, in, out); out.close(); } static class DifferentSumOfPrime { public void solve(int testNumber, Scanner in, PrintWriter out) { // 2万なのでN^2くらいは通りそうだけど… int n = in.nextInt(); ArrayList<Integer> primes = Prime.eratosthenes(n); int[] dp = new int[n + 1]; // dp[i] = i を異なる素数の和で表した時の最大個数 Arrays.fill(dp, -1); for (Integer p : primes) { for (int i = n; i > p; i--) { if (dp[i - p] > 0) { dp[i] = Math.max(dp[i], dp[i - p] + 1); } } dp[p] = Math.max(dp[p], 1); // out.println(Arrays.toString(dp)); } out.println(dp[n]); } } static class Prime { public static ArrayList<Integer> eratosthenes(int n) { // n以下の素数をすべて列挙する(計算量は O(N log(log N))) // ただし10^5が耐用限界っぽい感じはする ArrayList<Integer> res = new ArrayList<>(); ArrayList<Integer> primes = new ArrayList<>(); for (int i = 2; i <= n; i++) { res.add(i); } while (res.size() > 0) { primes.add(res.get(0)); res.removeIf(a -> a % res.get(0) == 0); } return primes; } } }