結果

問題 No.458 異なる素数の和
ユーザー chocopuuchocopuu
提出日時 2018-09-01 18:56:43
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 286 ms / 2,000 ms
コード長 1,162 bytes
コンパイル時間 2,119 ms
コンパイル使用メモリ 168,220 KB
実行使用メモリ 413,976 KB
最終ジャッジ日時 2023-10-19 20:53:18
合計ジャッジ時間 8,823 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 164 ms
320,728 KB
testcase_01 AC 185 ms
319,800 KB
testcase_02 AC 199 ms
321,464 KB
testcase_03 AC 170 ms
318,804 KB
testcase_04 AC 159 ms
345,892 KB
testcase_05 AC 243 ms
385,096 KB
testcase_06 AC 185 ms
348,692 KB
testcase_07 AC 147 ms
373,060 KB
testcase_08 AC 252 ms
397,884 KB
testcase_09 AC 149 ms
373,068 KB
testcase_10 AC 145 ms
373,060 KB
testcase_11 AC 286 ms
413,976 KB
testcase_12 AC 149 ms
373,056 KB
testcase_13 AC 144 ms
373,056 KB
testcase_14 AC 147 ms
372,088 KB
testcase_15 AC 146 ms
373,056 KB
testcase_16 AC 147 ms
373,064 KB
testcase_17 AC 146 ms
372,092 KB
testcase_18 AC 147 ms
373,060 KB
testcase_19 AC 145 ms
373,056 KB
testcase_20 AC 145 ms
373,060 KB
testcase_21 AC 148 ms
373,056 KB
testcase_22 AC 146 ms
373,056 KB
testcase_23 AC 145 ms
373,060 KB
testcase_24 AC 145 ms
373,060 KB
testcase_25 AC 145 ms
373,060 KB
testcase_26 AC 146 ms
373,060 KB
testcase_27 AC 176 ms
373,072 KB
testcase_28 AC 279 ms
411,748 KB
testcase_29 AC 145 ms
373,060 KB
testcase_30 AC 165 ms
373,072 KB
権限があれば一括ダウンロードができます

ソースコード

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