結果

問題 No.458 異なる素数の和
ユーザー ShibuyapShibuyap
提出日時 2020-03-09 02:55:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 38 ms / 2,000 ms
コード長 1,511 bytes
コンパイル時間 1,995 ms
コンパイル使用メモリ 164,728 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-25 09:54:06
合計ジャッジ時間 3,666 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
5,248 KB
testcase_01 AC 35 ms
5,376 KB
testcase_02 AC 37 ms
5,376 KB
testcase_03 AC 37 ms
5,376 KB
testcase_04 AC 37 ms
5,376 KB
testcase_05 AC 36 ms
5,376 KB
testcase_06 AC 35 ms
5,376 KB
testcase_07 AC 37 ms
5,376 KB
testcase_08 AC 36 ms
5,376 KB
testcase_09 AC 36 ms
5,376 KB
testcase_10 AC 35 ms
5,376 KB
testcase_11 AC 35 ms
5,376 KB
testcase_12 AC 36 ms
5,376 KB
testcase_13 AC 37 ms
5,376 KB
testcase_14 AC 35 ms
5,376 KB
testcase_15 AC 35 ms
5,376 KB
testcase_16 AC 35 ms
5,376 KB
testcase_17 AC 36 ms
5,376 KB
testcase_18 AC 36 ms
5,376 KB
testcase_19 AC 34 ms
5,376 KB
testcase_20 AC 35 ms
5,376 KB
testcase_21 AC 35 ms
5,376 KB
testcase_22 AC 38 ms
5,376 KB
testcase_23 AC 36 ms
5,376 KB
testcase_24 AC 36 ms
5,376 KB
testcase_25 AC 36 ms
5,376 KB
testcase_26 AC 36 ms
5,376 KB
testcase_27 AC 37 ms
5,376 KB
testcase_28 AC 37 ms
5,376 KB
testcase_29 AC 37 ms
5,376 KB
testcase_30 AC 35 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
#define dame { puts("-1"); return 0;}
#define yn {puts("Yes");}else{puts("No");}


/*
    Max_Prime以下の素数をprimes[]に格納
*/

const int Max_Prime = 21000;
int tmp_primes[Max_Prime];
vector<int> primes;

void PrimeInit() {
    rep(i,Max_Prime)tmp_primes[i] = 1;
    tmp_primes[0] = 0;
    tmp_primes[1] = 0;

    rep(i,Max_Prime){
        if(tmp_primes[i] == 0)continue;
        int j = 2;
        while(i * j <= Max_Prime){
            tmp_primes[i*j] = 0;
            j++;
        }
    }
    
    rep(i,Max_Prime){
        if(tmp_primes[i])primes.push_back(i);
    }
}

int main(){
    PrimeInit();

    int dp[21000] = {};

    int i = 0;
    while(primes[i] <= 20000){
        int x = primes[i];
        int dp2[21000] = {};
        rep(j,21000)dp2[j] = dp[j];
        srep(j,x,20001){
            if(j == x){
                dp2[j] = max(dp[j], 1);
            }else if(dp[j-x]){
                dp2[j] = max(dp[j], dp[j-x]+1);
            }
        }
        rep(j,21000)dp[j] = dp2[j];
        i++;
    }

    int n;
    cin >> n;

    if(dp[n] == 0)dp[n] = -1;
    cout << dp[n] << endl;
    return 0;
}
 
 
0