結果

問題 No.458 異なる素数の和
ユーザー mamekinmamekin
提出日時 2016-12-15 22:37:01
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 1,164 bytes
コンパイル時間 1,051 ms
コンパイル使用メモリ 108,676 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-05 05:48:17
合計ジャッジ時間 1,721 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;

void findPrime(int N, vector<bool>& isPrime)
{
    isPrime.assign(N+1, true);
    isPrime[0] = isPrime[1] = false;
    for(int i=2; i*i<=N; i++){
        if(isPrime[i]){
            for(int j=i; i*j<=N; j++){
                isPrime[i*j] = false;
            }
        }
    }
}

const int INF = INT_MAX / 2;

int main()
{
    int n;
    cin >> n;

    vector<bool> isPrime;
    findPrime(n, isPrime);

    vector<int> dp(n+1, -INF);
    dp[0] = 0;
    for(int a=1; a<=n; ++a){
        if(!isPrime[a])
            continue;
        for(int i=n-a; i>=0; --i)
            dp[i+a] = max(dp[i+a], dp[i] + 1);
    }

    if(dp[n] < 0)
        cout << -1 << endl;
    else
        cout << dp[n] << endl;

    return 0;
}
0