結果

問題 No.458 異なる素数の和
ユーザー chocopuu
提出日時 2018-09-01 18:56:43
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 205 ms / 2,000 ms
コード長 1,162 bytes
コンパイル時間 1,600 ms
コンパイル使用メモリ 166,428 KB
実行使用メモリ 343,112 KB
最終ジャッジ日時 2024-09-19 16:59:17
合計ジャッジ時間 6,880 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,s,n) for(int i = s;i<n;i++)
#define repe(i,s,n) for(int i = s;i<=n;i++)
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef pair<int,int>pint;
typedef vector<int>vint;
typedef vector<pint>vpint;
static const ll maxLL = (ll)1 << 62;
const int mod=1000000007;
int dy[]={-1,0,1,0};
int dx[]={0,1,0,-1};

//#define int ll


int n,m;
int dp[3010][40101];
vector<int> P;
void make_primes(int n)
{
	vector<bool> primes;

	primes.resize(n + 1, true);
	primes[0] = primes[1] = false;
	rep(i, 2, sqrt(n)) if (primes[i]) for (int j = 0; i * (j + 2) < n; j++)
		primes[i * (j + 2)] = false;

	rep(i, 2, n + 1) if (primes[i]) P.push_back(i);
}

signed main(){
    cin>>n;
    make_primes(n+1);
    rep(i,0,3010)rep(j,0,20101)dp[i][j]=-1;
    dp[0][0]=0;
    
    rep(i,0,P.size())rep(j,0,n+1)if(dp[i][j]>=0){
        dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
        dp[i+1][j+P[i]]=max(dp[i+1][j+P[i]],dp[i][j]+1);
        
    }
    int ans=-1;
    rep(i,0,P.size()){
        ans=max(ans,dp[i][n]);
        
    }
    if(ans)cout<<ans<<endl;
    else cout<<"-1"<<endl;
    return 0;
}
0