結果

問題 No.458 異なる素数の和
ユーザー takeya_okinotakeya_okino
提出日時 2017-06-13 12:15:36
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,057 bytes
コンパイル時間 2,339 ms
コンパイル使用メモリ 77,676 KB
実行使用メモリ 235,280 KB
最終ジャッジ日時 2023-10-24 21:44:26
合計ジャッジ時間 10,568 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 138 ms
57,180 KB
testcase_01 AC 269 ms
114,232 KB
testcase_02 AC 310 ms
133,388 KB
testcase_03 AC 177 ms
69,556 KB
testcase_04 AC 183 ms
69,512 KB
testcase_05 AC 579 ms
235,280 KB
testcase_06 AC 316 ms
135,324 KB
testcase_07 AC 148 ms
57,608 KB
testcase_08 AC 523 ms
234,320 KB
testcase_09 AC 164 ms
61,876 KB
testcase_10 AC 134 ms
57,376 KB
testcase_11 AC 557 ms
232,128 KB
testcase_12 WA -
testcase_13 AC 134 ms
57,576 KB
testcase_14 AC 135 ms
57,396 KB
testcase_15 WA -
testcase_16 AC 168 ms
62,032 KB
testcase_17 AC 134 ms
55,500 KB
testcase_18 AC 135 ms
57,504 KB
testcase_19 AC 135 ms
57,416 KB
testcase_20 AC 135 ms
57,552 KB
testcase_21 AC 136 ms
57,600 KB
testcase_22 AC 135 ms
57,684 KB
testcase_23 AC 133 ms
57,376 KB
testcase_24 AC 134 ms
57,412 KB
testcase_25 AC 134 ms
57,360 KB
testcase_26 AC 135 ms
57,416 KB
testcase_27 AC 280 ms
116,352 KB
testcase_28 AC 555 ms
232,356 KB
testcase_29 WA -
testcase_30 AC 249 ms
92,056 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    ArrayList<Integer> prime = new ArrayList<Integer>();
    for(int i = 2; i <= N; i++) {
      boolean flg = true;
      for(int j = 2; j * j <= i; j++) {
        if(i % j == 0) {
          flg = false;
          break;
        }
      }
      if(flg) prime.add(i);
    }
    int num = prime.size();
    int ans = -1;
    if(num != 0) {
      // dp[i][j]はjを異なる素数の和で表す(素数0~素数iまでを用いる)場合の、最大の和の回数を表す
      int[][] dp = new int[num][N + 1];
      dp[0][2] = 1;
      for(int i = 1; i < num; i++) {
        for(int j = 2; j < N + 1; j++) {
          if(j >= prime.get(i)) {
            dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - prime.get(i)] + 1);
          } else {
            dp[i][j] = dp[i - 1][j];
          }
        }
      }
      if(dp[num - 1][N] > 0) ans = dp[num - 1][N];
    }
    System.out.println(ans);
  }
}
0