結果

問題 No.458 異なる素数の和
ユーザー takeya_okinotakeya_okino
提出日時 2017-06-13 12:10:47
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 1,006 bytes
コンパイル時間 2,318 ms
コンパイル使用メモリ 78,176 KB
実行使用メモリ 236,224 KB
最終ジャッジ日時 2023-10-24 21:44:03
合計ジャッジ時間 10,078 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
56,208 KB
testcase_01 AC 268 ms
116,472 KB
testcase_02 AC 318 ms
133,472 KB
testcase_03 AC 167 ms
69,620 KB
testcase_04 AC 174 ms
69,712 KB
testcase_05 AC 571 ms
236,224 KB
testcase_06 AC 300 ms
135,408 KB
testcase_07 AC 138 ms
57,656 KB
testcase_08 AC 515 ms
234,332 KB
testcase_09 AC 153 ms
61,940 KB
testcase_10 RE -
testcase_11 AC 551 ms
232,276 KB
testcase_12 WA -
testcase_13 AC 124 ms
57,324 KB
testcase_14 AC 124 ms
57,308 KB
testcase_15 WA -
testcase_16 AC 146 ms
61,628 KB
testcase_17 AC 128 ms
57,512 KB
testcase_18 AC 125 ms
57,428 KB
testcase_19 AC 126 ms
57,136 KB
testcase_20 AC 127 ms
57,588 KB
testcase_21 AC 116 ms
56,276 KB
testcase_22 AC 127 ms
57,596 KB
testcase_23 AC 128 ms
57,440 KB
testcase_24 AC 129 ms
57,416 KB
testcase_25 AC 128 ms
57,380 KB
testcase_26 AC 128 ms
57,396 KB
testcase_27 AC 267 ms
116,240 KB
testcase_28 AC 550 ms
232,508 KB
testcase_29 WA -
testcase_30 AC 223 ms
91,680 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();
    // 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];
        }
      }
    }
    int ans = -1;
    if(dp[num - 1][N] > 0) ans = dp[num - 1][N];
    System.out.println(ans);
  }
}
0