結果

問題 No.458 異なる素数の和
ユーザー tomorrowtomorrow
提出日時 2022-05-17 21:51:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 36 ms / 2,000 ms
コード長 1,091 bytes
コンパイル時間 2,066 ms
コンパイル使用メモリ 201,068 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-13 23:06:27
合計ジャッジ時間 3,651 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll inf = (1LL << 62);
template <typename T>
bool chmax(T& a, const T& b)
{
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template <typename T>
bool chmin(T& a, const T& b)
{
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
long is_prime[20010];
vector<ll> primes;
void euclid()
{
    is_prime[0] = 2;
    is_prime[1] = 2;
    for (ll i = 2; i < 20010; ++i) {
        if (is_prime[i] == 0) {
            is_prime[i] = 1;
            primes.push_back(i);
            for (ll j = 2 * i; j < 20010; j += i) {
                is_prime[j] = 2;
            }
        }
    }
}

ll dp[20010];
int main()
{
    ll n;
    cin >> n;
    euclid();
    memset(dp, -1, 20010 * sizeof(long));
    dp[0] = 0;
    for (auto& p : primes) {
        for (ll i = n; i >= 0; --i) {
            if (p > i)
                break;
            if (dp[i - p] == -1)
                continue;
            chmax(dp[i], 1 + dp[i - p]);
        }
    }
    cout << dp[n] << endl;
}
0