結果

問題 No.458 異なる素数の和
ユーザー 赤信号
提出日時 2024-03-17 14:51:12
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
MLE  
実行時間 -
コード長 1,472 bytes
コンパイル時間 3,833 ms
コンパイル使用メモリ 188,656 KB
実行使用メモリ 718,720 KB
最終ジャッジ日時 2024-09-30 04:43:33
合計ジャッジ時間 8,107 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24 MLE * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

#if __has_include(<atcoder/all>)
#include <atcoder/all>
#endif

#ifdef LOCAL
#include "algo/debug.h"
#else
#define _debug(...) 42
#endif

#include <iostream>
#include <cstddef>
#include <vector>
#include <optional>
#include <algorithm>

std::vector<std::size_t> enumerate_primes(std::size_t x) {
    std::vector<bool> bit_primes(x + 1, true);
    bit_primes[0] = bit_primes[1] = false;

    for (std::size_t i = 2; i * i <= x; ++i) if (bit_primes[i]) for (std::size_t j = 2; i * j <= x; ++j) bit_primes[i * j] = false;

    std::vector<std::size_t> ret;
    for (std::size_t i = 0; i <= x; ++i) if (bit_primes[i]) ret.push_back(i);

    return ret;
}

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

    std::size_t n;
    std::cin >> n;

    std::vector<std::size_t> primes = enumerate_primes(n);
    std::size_t m = primes.size();

    std::vector<std::vector<std::optional<std::size_t>>> dp(m + 1, std::vector<std::optional<std::size_t>>(n + 1, std::nullopt));
    dp[0][0] = 0;

    for (std::size_t i = 0; i < m; ++i) {
        for (std::size_t j = 0; j <= n; ++j) {
            if (!dp[i][j].has_value()) continue;

            if (j + primes[i] <= n) dp[i + 1][j + primes[i]] = std::max(dp[i + 1][j + primes[i]].value_or(0), dp[i][j].value() + 1);

            dp[i + 1][j] = std::max(dp[i + 1][j].value_or(0), dp[i][j].value());
        }
    }

    std::cout << (dp[m][n].has_value() ? (int)dp[m][n].value() : -1) << '\n';
    return 0;
}
0