結果

問題 No.458 異なる素数の和
ユーザー tenten
提出日時 2021-12-02 09:58:55
言語 Java
(openjdk 23)
結果
AC  
実行時間 113 ms / 2,000 ms
コード長 1,527 bytes
コンパイル時間 2,090 ms
コンパイル使用メモリ 77,648 KB
実行使用メモリ 38,372 KB
最終ジャッジ日時 2024-07-05 01:48:34
合計ジャッジ時間 5,110 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

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