結果

問題 No.458 異なる素数の和
ユーザー tottoripapertottoripaper
提出日時 2018-03-12 18:18:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 121 ms / 2,000 ms
コード長 1,305 bytes
コンパイル時間 1,712 ms
コンパイル使用メモリ 166,928 KB
実行使用メモリ 184,292 KB
最終ジャッジ日時 2024-04-25 18:03:44
合計ジャッジ時間 6,333 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
184,240 KB
testcase_01 AC 118 ms
183,956 KB
testcase_02 AC 113 ms
184,072 KB
testcase_03 AC 113 ms
184,236 KB
testcase_04 AC 113 ms
184,260 KB
testcase_05 AC 111 ms
184,152 KB
testcase_06 AC 112 ms
184,028 KB
testcase_07 AC 114 ms
184,196 KB
testcase_08 AC 109 ms
184,100 KB
testcase_09 AC 113 ms
183,968 KB
testcase_10 AC 112 ms
184,024 KB
testcase_11 AC 115 ms
184,036 KB
testcase_12 AC 109 ms
183,940 KB
testcase_13 AC 109 ms
183,932 KB
testcase_14 AC 111 ms
184,112 KB
testcase_15 AC 111 ms
184,172 KB
testcase_16 AC 109 ms
184,212 KB
testcase_17 AC 115 ms
184,068 KB
testcase_18 AC 113 ms
184,204 KB
testcase_19 AC 113 ms
184,116 KB
testcase_20 AC 112 ms
184,124 KB
testcase_21 AC 110 ms
184,156 KB
testcase_22 AC 112 ms
184,100 KB
testcase_23 AC 116 ms
184,192 KB
testcase_24 AC 112 ms
184,200 KB
testcase_25 AC 114 ms
184,208 KB
testcase_26 AC 114 ms
184,044 KB
testcase_27 AC 111 ms
184,232 KB
testcase_28 AC 111 ms
184,216 KB
testcase_29 AC 112 ms
184,136 KB
testcase_30 AC 111 ms
184,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if(!(p))
#define until(p) while(!(p))

using ll = long long;
using P = std::tuple<int,int>;

const int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1}, dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};

int dp[2300][20100];
bool isNotPrime[20100];
vector<int> primes;

int main(){
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    fill(&dp[0][0], &dp[0][0]+2300*20100, -30000);
    dp[0][0] = 0;

    primes.emplace_back(2);

    for(int i=4;i<=20000;i+=2){
        isNotPrime[i] = true;
    }

    for(int i=3;i<=20000;i+=2){
        if(isNotPrime[i]){continue;}

        primes.emplace_back(i);
        for(int j=2*i;j<=20000;j+=i){
            isNotPrime[j] = true;
        }
    }

    int Q = primes.size();
    for(int i=0;i<Q;++i){
        int p = primes[i];
        for(int j=0;j<=20000;++j){
            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);

            if(dp[i][j] >= 0 && j + p <= 20000){
                dp[i + 1][j + p] = max(dp[i + 1][j + p], dp[i][j] + 1);
            }
        }
    }

    int N;
    std::cin >> N;
    
    int res = dp[Q][N];
    if(res == -30000){
        res = -1;
    }

    std::cout << res << std::endl;
}
0