結果

問題 No.458 異なる素数の和
ユーザー heabiheabi
提出日時 2020-11-22 10:46:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 395 ms / 2,000 ms
コード長 2,245 bytes
コンパイル時間 1,688 ms
コンパイル使用メモリ 174,700 KB
実行使用メモリ 365,440 KB
最終ジャッジ日時 2023-09-30 22:34:24
合計ジャッジ時間 5,915 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
9,164 KB
testcase_01 AC 192 ms
184,192 KB
testcase_02 AC 222 ms
212,596 KB
testcase_03 AC 85 ms
83,776 KB
testcase_04 AC 100 ms
95,980 KB
testcase_05 AC 359 ms
328,868 KB
testcase_06 AC 216 ms
206,024 KB
testcase_07 AC 13 ms
15,380 KB
testcase_08 AC 356 ms
329,224 KB
testcase_09 AC 45 ms
45,216 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 395 ms
365,440 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 63 ms
62,484 KB
testcase_17 AC 3 ms
4,408 KB
testcase_18 AC 4 ms
4,392 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 4 ms
5,112 KB
testcase_21 AC 2 ms
4,384 KB
testcase_22 AC 3 ms
4,380 KB
testcase_23 AC 4 ms
4,928 KB
testcase_24 AC 3 ms
4,940 KB
testcase_25 AC 3 ms
4,380 KB
testcase_26 AC 3 ms
4,580 KB
testcase_27 AC 210 ms
200,536 KB
testcase_28 AC 387 ms
356,328 KB
testcase_29 AC 32 ms
31,552 KB
testcase_30 AC 162 ms
155,664 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define P pair<ll, ll>
#define Graph vector<vector<ll>>
#define fi first
#define se second
#define vvvvll vector<vector<vector<vector<ll>>>>
#define vvvll vector<vector<vector<ll>>>
#define vvll vector<vector<ll>>
#define vll vector<ll>
#define pqll priority_queue<ll>
#define pqllg priority_queue<ll, vector<ll>, greater<ll>>

constexpr ll INF = (1ll << 60);
constexpr ll mod = 1000000007;
// constexpr ll mod = 998244353;
// constexpr ll mod = 67280421310721;
constexpr double pi = 3.14159265358979323846;
template <typename T>
inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T>
inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}

void pt_vvll(vvll v) {
    ll vs = v.size(), vs0 = v[0].size();
    rep(i, vs) {
        rep(j, vs0) {
            cout << v[i][j];

            if (j != vs0 - 1)
                cout << " ";
            else
                cout << "\n";
        }
    }
}
void pt_vll(vll v) {
    ll vs = v.size();
    rep(i, vs) {
        cout << v[i];
        if (i == vs - 1)
            cout << "\n";
        else
            cout << " ";
    }
}

vector<bool> prime(100000, true);
vector<bool> isprime(int N) {
    if (N >= 0) prime[0] = false;
    if (N >= 1) prime[1] = false;
    for (ll i = 2; i * i <= N; i++) {
        if (!prime[i]) {
            continue;
        }
        for (ll j = i * i; j <= N; j += i) {
            prime[j] = false;
        }
    }
    return prime;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    ll N;
    cin >> N;
    isprime(20010);
    vll prs;
    rep(i, 20010) {
        if (prime[i]) prs.push_back(i);
    }

    ll ps = prs.size();
    vvll dp(ps + 1, vll(N + 1, -1));
    dp[0][0] = 0;
    rep(i, ps) {
        rep(j, N + 1) {
            if (dp[i][j] == -1) continue;

            // use prs[i]
            if (j + prs[i] <= N) chmax(dp[i + 1][j + prs[i]], dp[i][j] + 1);

            // dont use prs[i]
            chmax(dp[i + 1][j], dp[i][j]);
        }
    }

    cout << dp[ps][N] << "\n";
    return 0;
}
0