結果

問題 No.458 異なる素数の和
ユーザー tottoripaper
提出日時 2018-03-12 18:18:50
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 240 ms / 2,000 ms
コード長 1,305 bytes
コンパイル時間 2,074 ms
コンパイル使用メモリ 169,300 KB
実行使用メモリ 184,064 KB
最終ジャッジ日時 2024-11-08 05:28:02
合計ジャッジ時間 10,649 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if(!(p))
#define until(p) while(!(p))

using ll = long long;
using P = std::tuple<int,int>;

const int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1}, dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};

int dp[2300][20100];
bool isNotPrime[20100];
vector<int> primes;

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

    fill(&dp[0][0], &dp[0][0]+2300*20100, -30000);
    dp[0][0] = 0;

    primes.emplace_back(2);

    for(int i=4;i<=20000;i+=2){
        isNotPrime[i] = true;
    }

    for(int i=3;i<=20000;i+=2){
        if(isNotPrime[i]){continue;}

        primes.emplace_back(i);
        for(int j=2*i;j<=20000;j+=i){
            isNotPrime[j] = true;
        }
    }

    int Q = primes.size();
    for(int i=0;i<Q;++i){
        int p = primes[i];
        for(int j=0;j<=20000;++j){
            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);

            if(dp[i][j] >= 0 && j + p <= 20000){
                dp[i + 1][j + p] = max(dp[i + 1][j + p], dp[i][j] + 1);
            }
        }
    }

    int N;
    std::cin >> N;
    
    int res = dp[Q][N];
    if(res == -30000){
        res = -1;
    }

    std::cout << res << std::endl;
}
0