結果

問題 No.458 異なる素数の和
ユーザー tentententen
提出日時 2021-12-02 09:58:55
言語 Java21
(openjdk 21)
結果
AC  
実行時間 118 ms / 2,000 ms
コード長 1,527 bytes
コンパイル時間 2,120 ms
コンパイル使用メモリ 74,560 KB
実行使用メモリ 51,596 KB
最終ジャッジ日時 2023-09-18 10:11:26
合計ジャッジ時間 5,378 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
49,292 KB
testcase_01 AC 74 ms
51,120 KB
testcase_02 AC 81 ms
51,336 KB
testcase_03 AC 63 ms
51,440 KB
testcase_04 AC 63 ms
51,144 KB
testcase_05 AC 110 ms
51,388 KB
testcase_06 AC 82 ms
51,248 KB
testcase_07 AC 47 ms
49,328 KB
testcase_08 AC 108 ms
51,104 KB
testcase_09 AC 58 ms
51,596 KB
testcase_10 AC 45 ms
49,696 KB
testcase_11 AC 118 ms
49,336 KB
testcase_12 AC 44 ms
49,396 KB
testcase_13 AC 45 ms
49,432 KB
testcase_14 AC 45 ms
49,356 KB
testcase_15 AC 45 ms
49,696 KB
testcase_16 AC 61 ms
51,360 KB
testcase_17 AC 44 ms
47,404 KB
testcase_18 AC 44 ms
47,336 KB
testcase_19 AC 44 ms
49,452 KB
testcase_20 AC 45 ms
49,388 KB
testcase_21 AC 44 ms
49,292 KB
testcase_22 AC 44 ms
49,504 KB
testcase_23 AC 44 ms
49,264 KB
testcase_24 AC 45 ms
49,460 KB
testcase_25 AC 44 ms
49,376 KB
testcase_26 AC 44 ms
49,244 KB
testcase_27 AC 78 ms
51,180 KB
testcase_28 AC 118 ms
51,352 KB
testcase_29 AC 55 ms
50,012 KB
testcase_30 AC 70 ms
49,556 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();
        boolean[] isNotPrime = new boolean[n + 1];
        int[] dp = new int[n + 1];
        Arrays.fill(dp, -1);
        dp[0] = 0;
        int max = 0;
        for (int i = 2; i <= n; i++) {
            if (isNotPrime[i]) {
                continue;
            }
            for (int j = 2; j * i <= n; j++) {
                isNotPrime[i * j] = true;
            }
            for (int j = Math.min(max, n - i); j >= 0; j--) {
                if (dp[j] >= 0) {
                    dp[j + i] = Math.max(dp[j + i], dp[j] + 1);
                }
            }
            max = Math.min(max + i, n);
        }
        System.out.println(dp[n]);
    }
}
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 double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0