結果

問題 No.458 異なる素数の和
ユーザー tac
提出日時 2019-05-03 20:42:48
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 46 ms / 2,000 ms
コード長 1,341 bytes
コンパイル時間 1,186 ms
コンパイル使用メモリ 104,956 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-12-31 16:10:56
合計ジャッジ時間 3,993 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<cmath>
#include<iomanip>
#include<set>
#include<numeric>
#include<sstream>
#include<random>
#include<cassert>
#include<climits>
#include<cstring>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, st, n) for (int i = st; i < n; ++i)
using pii = pair<int, int>;
const int inf = 1e9 + 7;
int dy[] = {0, 0, -1, 1, -1, 1, -1, 1};
int dx[] = {1, -1, 0, 0, -1, 1, 1, -1};
#define ceil(a, b) a / b + !!(a % b)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)

const int lim = 20001;
int table[lim];
vector<int> v;
void prime() {
    rep(i, lim) if (i % 2 == 0) table[i] = 1;
    table[2] = 0;
    table[1] = 1;
    for (int i = 3; i < sqrt(lim) + 1; i += 2) {
        int key = 2 * i;
        while (key < lim) {
            table[key] = 1;
            key += i;
        }
    }
    rep(i, lim) if (table[i] == 0) v.push_back(i);
}
int main() {
    prime();
    int n; cin >> n;
    int dp[lim];
    rep(i, lim) dp[i] = -1;
    dp[0] = 0;
    rep(k, v.size()) {
        for (int i = lim - 1; i >= 0; --i) {
            if (dp[i] != -1 && i + v[k] < lim) {
                chmax(dp[i + v[k]], dp[i] + 1);
            }
        }
    }
    cout << dp[n] << endl;
}

0