結果

問題 No.458 異なる素数の和
ユーザー tentententen
提出日時 2022-08-10 16:23:10
言語 Java21
(openjdk 21)
結果
AC  
実行時間 125 ms / 2,000 ms
コード長 1,580 bytes
コンパイル時間 2,469 ms
コンパイル使用メモリ 77,376 KB
実行使用メモリ 54,368 KB
最終ジャッジ日時 2023-10-21 02:10:01
合計ジャッジ時間 6,150 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
53,548 KB
testcase_01 AC 91 ms
54,316 KB
testcase_02 AC 97 ms
54,144 KB
testcase_03 AC 79 ms
54,340 KB
testcase_04 AC 79 ms
54,312 KB
testcase_05 AC 119 ms
54,304 KB
testcase_06 AC 92 ms
54,140 KB
testcase_07 AC 57 ms
53,548 KB
testcase_08 AC 117 ms
54,368 KB
testcase_09 AC 71 ms
54,120 KB
testcase_10 AC 57 ms
52,448 KB
testcase_11 AC 124 ms
54,316 KB
testcase_12 AC 57 ms
52,476 KB
testcase_13 AC 55 ms
53,536 KB
testcase_14 AC 56 ms
53,536 KB
testcase_15 AC 56 ms
53,548 KB
testcase_16 AC 76 ms
54,228 KB
testcase_17 AC 55 ms
53,548 KB
testcase_18 AC 55 ms
52,460 KB
testcase_19 AC 55 ms
53,544 KB
testcase_20 AC 56 ms
52,452 KB
testcase_21 AC 55 ms
52,440 KB
testcase_22 AC 55 ms
52,468 KB
testcase_23 AC 54 ms
53,540 KB
testcase_24 AC 57 ms
52,436 KB
testcase_25 AC 55 ms
52,480 KB
testcase_26 AC 56 ms
52,460 KB
testcase_27 AC 92 ms
54,116 KB
testcase_28 AC 125 ms
54,320 KB
testcase_29 AC 68 ms
51,520 KB
testcase_30 AC 86 ms
52,276 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[] counts = new int[n + 1];
        Arrays.fill(counts, -1);
        counts[0] = 0;
        int max = 0;
        for (int i = 2; i <= n; i++) {
            if (!isNotPrime[i]) {
                for (int j = Math.min(max, n - i); j >= 0; j--) {
                    if (counts[j] >= 0) {
                        counts[j + i] = Math.max(counts[j + i], counts[j] + 1);
                    }
                }
                max = Math.min(n, max + i);
                for (int j = 2; j * i <= n; j++) {
                    isNotPrime[j * i] = true;
                }
            }
        }
        System.out.println(counts[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 {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0