結果

問題 No.458 異なる素数の和
ユーザー kohaku_kohakukohaku_kohaku
提出日時 2016-12-09 02:19:43
言語 Java21
(openjdk 21)
結果
AC  
実行時間 253 ms / 2,000 ms
コード長 1,092 bytes
コンパイル時間 3,268 ms
コンパイル使用メモリ 73,828 KB
実行使用メモリ 56,184 KB
最終ジャッジ日時 2023-09-18 14:59:04
合計ジャッジ時間 9,355 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
54,240 KB
testcase_01 AC 174 ms
55,724 KB
testcase_02 AC 182 ms
56,004 KB
testcase_03 AC 147 ms
55,680 KB
testcase_04 AC 150 ms
55,612 KB
testcase_05 AC 233 ms
55,860 KB
testcase_06 AC 179 ms
55,788 KB
testcase_07 AC 132 ms
55,720 KB
testcase_08 AC 233 ms
55,796 KB
testcase_09 AC 136 ms
55,716 KB
testcase_10 AC 124 ms
55,376 KB
testcase_11 AC 253 ms
55,608 KB
testcase_12 AC 125 ms
56,016 KB
testcase_13 AC 127 ms
56,184 KB
testcase_14 AC 125 ms
55,376 KB
testcase_15 AC 125 ms
55,612 KB
testcase_16 AC 141 ms
55,996 KB
testcase_17 AC 125 ms
55,940 KB
testcase_18 AC 124 ms
55,404 KB
testcase_19 AC 123 ms
55,416 KB
testcase_20 AC 124 ms
56,172 KB
testcase_21 AC 125 ms
55,636 KB
testcase_22 AC 126 ms
55,780 KB
testcase_23 AC 124 ms
55,780 KB
testcase_24 AC 126 ms
55,940 KB
testcase_25 AC 126 ms
55,640 KB
testcase_26 AC 126 ms
55,408 KB
testcase_27 AC 178 ms
55,844 KB
testcase_28 AC 249 ms
56,088 KB
testcase_29 AC 137 ms
55,604 KB
testcase_30 AC 163 ms
55,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        boolean [] b = new boolean [N+1];
        ArrayList<Integer> prime = new ArrayList<Integer>();
        prime.add(2);
        for(int i=3; i<=N; i+=2){
            if(b[i]==false){
                prime.add(i);
                int k=2;
                while(i*k<=N){
                    b[i*k]=true;
                    k++;
                }
            }
        }
        boolean [] dp = new boolean [N+1];
        dp[0]=true;
        int [] num = new int [N+1];
        for(int i=0; i<prime.size(); i++){
            int t=prime.get(i);
            for(int j=N; j>=0; j--){
                if(dp[j]){
                    int tmp=j+t;
                    if(tmp<=N){
                        dp[tmp]=true;
                        num[tmp]=Math.max(num[tmp],num[j]+1);
                    }
                }
            }
        }
        int ans=num[N];
        if(ans==0)ans=-1;
        System.out.println(ans);
    }
}
0