結果

問題 No.458 異なる素数の和
ユーザー crazybbb3crazybbb3
提出日時 2017-01-16 20:47:51
言語 Java21
(openjdk 21)
結果
AC  
実行時間 758 ms / 2,000 ms
コード長 2,829 bytes
コンパイル時間 3,521 ms
コンパイル使用メモリ 74,568 KB
実行使用メモリ 453,520 KB
最終ジャッジ日時 2023-09-18 15:16:58
合計ジャッジ時間 8,995 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
49,788 KB
testcase_01 AC 222 ms
187,596 KB
testcase_02 AC 241 ms
188,072 KB
testcase_03 AC 112 ms
75,928 KB
testcase_04 AC 134 ms
85,060 KB
testcase_05 AC 616 ms
354,308 KB
testcase_06 AC 283 ms
188,336 KB
testcase_07 AC 54 ms
50,104 KB
testcase_08 AC 506 ms
361,044 KB
testcase_09 AC 79 ms
56,164 KB
testcase_10 AC 45 ms
49,412 KB
testcase_11 AC 758 ms
453,520 KB
testcase_12 AC 48 ms
47,476 KB
testcase_13 AC 44 ms
49,532 KB
testcase_14 AC 45 ms
49,300 KB
testcase_15 AC 45 ms
49,544 KB
testcase_16 AC 95 ms
64,332 KB
testcase_17 AC 45 ms
49,380 KB
testcase_18 AC 44 ms
49,272 KB
testcase_19 AC 45 ms
49,392 KB
testcase_20 AC 45 ms
49,604 KB
testcase_21 AC 45 ms
49,364 KB
testcase_22 AC 44 ms
47,536 KB
testcase_23 AC 45 ms
49,456 KB
testcase_24 AC 45 ms
49,484 KB
testcase_25 AC 46 ms
49,416 KB
testcase_26 AC 45 ms
49,500 KB
testcase_27 AC 234 ms
190,188 KB
testcase_28 AC 614 ms
451,492 KB
testcase_29 AC 65 ms
53,760 KB
testcase_30 AC 185 ms
130,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

public class Main {

    void solve() throws IOException {
        int n = ni();

        Prime prime = new Prime(n);
        ArrayList<Integer> primes = prime.primes;

        int m = primes.size();

        long[][] dp = new long[m + 1][n + 1];
        for (int i = 0; i <= m; i++) {
            Arrays.fill(dp[i], -1);
        }
        dp[0][0] = 0;

        for (int i = 0; i < m; i++) {
            int p = primes.get(i);
            for (int j = n; j >= 0; j--) {
                if (dp[i][j] == -1) continue;
                dp[i + 1][j] = dp[i][j];
                if (j + p <= n) {
                    dp[i + 1][j + p] = Math.max(dp[i + 1][j + p], dp[i][j] + 1);
                }
            }
        }

        out.println(dp[m][n]);
    }

    class Prime {
        int n;
        boolean[] isPrime;
        ArrayList<Integer> primes;

        Prime(int n) {
            this.n = n;
            isPrime = new boolean[n + 1];
            primes = new ArrayList<>();
            sieve();
        }

        void sieve() {
            for (int i = 2; i <= n; i++) {
                if (!isPrime[i]) {
                    primes.add(i);
                    for (long j = (long) i * i; j <= n; j += i) {
                        isPrime[(int) j] = true;
                    }
                }
            }
        }
    }

    String ns() throws IOException {
        while (!tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine(), " ");
        }
        return tok.nextToken();
    }

    int ni() throws IOException {
        return Integer.parseInt(ns());
    }

    long nl() throws IOException {
        return Long.parseLong(ns());
    }

    double nd() throws IOException {
        return Double.parseDouble(ns());
    }

    String[] nsa(int n) throws IOException {
        String[] res = new String[n];
        for (int i = 0; i < n; i++) {
            res[i] = ns();
        }
        return res;
    }

    int[] nia(int n) throws IOException {
        int[] res = new int[n];
        for (int i = 0; i < n; i++) {
            res[i] = ni();
        }
        return res;
    }

    long[] nla(int n) throws IOException {
        long[] res = new long[n];
        for (int i = 0; i < n; i++) {
            res[i] = nl();
        }
        return res;
    }

    static BufferedReader in;
    static PrintWriter out;
    static StringTokenizer tok;

    public static void main(String[] args) throws IOException {
        in = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        tok = new StringTokenizer("");
        Main main = new Main();
        main.solve();
        out.close();
    }
}
0