結果

問題 No.458 異なる素数の和
ユーザー sawfishsawfish
提出日時 2022-10-23 21:25:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 809 bytes
コンパイル時間 580 ms
コンパイル使用メモリ 74,336 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-15 06:45:23
合計ジャッジ時間 2,417 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 9 ms
4,376 KB
testcase_02 AC 11 ms
4,380 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 22 ms
4,376 KB
testcase_06 AC 10 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 22 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 26 ms
4,384 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 10 ms
4,376 KB
testcase_28 AC 26 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 7 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cmath>
#include <vector>

using namespace std;
using LL = long long;
using ULL = unsigned long long;

int main() {
    int N;
    cin >> N;

    bool P_[N + 1];
    for (bool &p: P_) p = true;

    P_[0] = false;
    P_[1] = false;
    for (int i = 2; i <= sqrt(N); i++) {
        if (P_[i]) {
            for (int j = i * 2; j <= N; j += i) {
                P_[j] = false;
            }
        }
    }

    vector<int> P;
    for (int i = 0; i <= N; i++) {
        if (P_[i]) P.push_back(i);
    }

    //
    int dp[N + 1];
    for (int &i: dp) i = -1;

    dp[0] = 0;
    for (int i = 0; i < P.size(); i++) {
        for (int j = N - P[i]; j >= 0; j--) {
            if (dp[j] != -1) dp[j + P[i]] = max(dp[j + P[i]], dp[j] + 1);
        }
    }

    cout << dp[N] << endl;
}
0