結果

問題 No.458 異なる素数の和
ユーザー algon_320algon_320
提出日時 2016-12-09 01:16:40
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 278 ms / 2,000 ms
コード長 2,291 bytes
コンパイル時間 2,114 ms
コンパイル使用メモリ 174,224 KB
実行使用メモリ 358,564 KB
最終ジャッジ日時 2023-09-18 14:56:38
合計ジャッジ時間 8,050 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 254 ms
358,424 KB
testcase_01 AC 278 ms
358,352 KB
testcase_02 AC 154 ms
358,292 KB
testcase_03 AC 112 ms
358,352 KB
testcase_04 AC 118 ms
358,436 KB
testcase_05 AC 186 ms
358,420 KB
testcase_06 AC 149 ms
358,352 KB
testcase_07 AC 94 ms
358,292 KB
testcase_08 AC 185 ms
358,380 KB
testcase_09 AC 102 ms
358,564 KB
testcase_10 AC 89 ms
358,364 KB
testcase_11 AC 200 ms
358,360 KB
testcase_12 AC 90 ms
358,296 KB
testcase_13 AC 90 ms
358,364 KB
testcase_14 AC 92 ms
358,368 KB
testcase_15 AC 91 ms
358,300 KB
testcase_16 AC 106 ms
358,312 KB
testcase_17 AC 91 ms
358,360 KB
testcase_18 AC 91 ms
358,300 KB
testcase_19 AC 90 ms
358,368 KB
testcase_20 AC 91 ms
358,564 KB
testcase_21 AC 91 ms
358,312 KB
testcase_22 AC 91 ms
358,308 KB
testcase_23 AC 91 ms
358,488 KB
testcase_24 AC 91 ms
358,296 KB
testcase_25 AC 91 ms
358,424 KB
testcase_26 AC 91 ms
358,296 KB
testcase_27 AC 149 ms
358,460 KB
testcase_28 AC 195 ms
358,296 KB
testcase_29 AC 97 ms
358,360 KB
testcase_30 AC 131 ms
358,308 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define int long long
using ll=long long;
using vi=vector<int>;
using vl=vector<long long>;
using pii=pair<int,int>;
using pll=pair<long long,long long>;
#define ITR(i,c) for(auto i=begin(c);i!=end(c);++i)
#define FORE(x,c) for(auto &x:c)
#define FOR(i,a,n) for(int i=a,i##_len=(int)n;i<i##_len;++i)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) for(int i=((int)(n)-1);i>=0;--i)
#define ALL(c) begin(c),end(c)
#define RALL(c) rbegin(c),rend(c)
#define SZ(c) ((int)c.size())
#define dump(...)
const int DX[9]={0,1,0,-1,1,1,-1,-1,0}, DY[9]={-1,0,1,0,-1,1,1,-1,0};
const int INF=1e9*2; const long long INFLL=1e18*2;
template<class T> ostream& operator << (ostream &os, const vector<T> &v) {
    ITR(i,v) os << *i << (i==end(v)-1 ? "" : " "); return os; }
template<class T> istream& operator >> (istream &is, vector<T> &v) {
    ITR(i,v) is >> * i; return is; }
template<class T> istream& operator >> (istream &is, pair<T,T> &p) {
        is >> p.second >> p.first; return is; }
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return 1;}return 0;}
//------------------------------------------------------------------------------

//-------- エラトステネスの篩
vector<bool> isPrime;
void eratosthenes(int N) {
    isPrime.resize(N+1);
    fill(ALL(isPrime),true);
	FOR(i,2,sqrt(N)+1)
        if(isPrime[i])
            for(int j=2; i*j<=N; j++)
                isPrime[i*j]=false;
}
vector<int> getPrime(int N) {
    eratosthenes(N);
    vector<int> prime;
    FOR(i,2,N+1) if(isPrime[i]) prime.push_back(i);
    return prime;
}
int dp[2270][20010];
signed main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    REP(i,2270) REP(j,20010) dp[i][j]=-1;

    int N,ans=-1;
    cin>>N;
    vi p=getPrime(20010);

    dp[0][0]=0;
    dp[0][p[0]]=1;

    FOR(i,0,SZ(p)-1) {
        REP(j,N+1) {
            if(dp[i][j]==-1) continue;
            if(chmax(dp[i+1][j],dp[i][j])) {
                if(j==N) ans=dp[i+1][j];
            }
            if(j+p[i+1]>N) continue;
            if(chmax(dp[i+1][j+p[i+1]],dp[i][j]+1)) {
                if(j==N) ans=dp[i+1][j+p[i+1]];
            }
        }
    }
    cout<<(ans==-1?-1:ans)<<endl;
    return 0;
}
0