結果

問題 No.458 異なる素数の和
ユーザー tentententen
提出日時 2021-11-02 10:18:36
言語 Java21
(openjdk 21)
結果
AC  
実行時間 132 ms / 2,000 ms
コード長 1,437 bytes
コンパイル時間 2,555 ms
コンパイル使用メモリ 76,908 KB
実行使用メモリ 38,336 KB
最終ジャッジ日時 2024-04-19 16:24:08
合計ジャッジ時間 5,452 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
37,412 KB
testcase_01 AC 82 ms
38,256 KB
testcase_02 AC 90 ms
38,272 KB
testcase_03 AC 78 ms
38,156 KB
testcase_04 AC 75 ms
38,076 KB
testcase_05 AC 117 ms
38,312 KB
testcase_06 AC 88 ms
38,272 KB
testcase_07 AC 55 ms
37,236 KB
testcase_08 AC 116 ms
38,100 KB
testcase_09 AC 70 ms
37,492 KB
testcase_10 AC 54 ms
37,108 KB
testcase_11 AC 126 ms
38,104 KB
testcase_12 AC 52 ms
37,332 KB
testcase_13 AC 52 ms
36,956 KB
testcase_14 AC 52 ms
37,296 KB
testcase_15 AC 52 ms
37,312 KB
testcase_16 AC 73 ms
37,940 KB
testcase_17 AC 54 ms
36,712 KB
testcase_18 AC 53 ms
37,216 KB
testcase_19 AC 52 ms
36,972 KB
testcase_20 AC 52 ms
37,052 KB
testcase_21 AC 52 ms
37,232 KB
testcase_22 AC 51 ms
36,956 KB
testcase_23 AC 53 ms
37,180 KB
testcase_24 AC 53 ms
37,348 KB
testcase_25 AC 53 ms
37,216 KB
testcase_26 AC 54 ms
37,020 KB
testcase_27 AC 88 ms
38,300 KB
testcase_28 AC 132 ms
38,244 KB
testcase_29 AC 64 ms
37,516 KB
testcase_30 AC 81 ms
38,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int[] dp = new int[n + 1];
        dp[0] = 1;
        boolean[] isNotPrime = new boolean[n + 1];
        for (int i = 2; i <= n; i++) {
            if (isNotPrime[i]) {
                continue;
            }
            for (int j = n - i; j >= 0; j--) {
                if (dp[j] == 0) {
                    continue;
                }
                dp[j + i] = Math.max(dp[j + i], dp[j] + 1);
            }
            for (int j = 2; j * i <= n; j++) {
                isNotPrime[i * j] = true;
            }
        }
        System.out.println(dp[n] - 1);
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public String nextLine() throws Exception {
        return br.readLine();
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0