結果

問題 No.458 異なる素数の和
ユーザー onakaT_TitaionakaT_Titai
提出日時 2018-10-10 22:58:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 1,798 bytes
コンパイル時間 781 ms
コンパイル使用メモリ 98,600 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-20 19:33:20
合計ジャッジ時間 1,980 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,248 KB
testcase_01 AC 25 ms
5,248 KB
testcase_02 AC 29 ms
5,376 KB
testcase_03 AC 14 ms
5,376 KB
testcase_04 AC 16 ms
5,376 KB
testcase_05 AC 41 ms
5,376 KB
testcase_06 AC 29 ms
5,376 KB
testcase_07 AC 4 ms
5,376 KB
testcase_08 AC 39 ms
5,376 KB
testcase_09 AC 9 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 42 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 11 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 27 ms
5,376 KB
testcase_28 AC 42 ms
5,376 KB
testcase_29 AC 7 ms
5,376 KB
testcase_30 AC 23 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <bitset>
#include <fstream>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <functional>
#include <cctype>
#include <list>
#include <limits>
//#include <boost/multiprecision/cpp_int.hpp>

const double EPS = (1e-10);


using namespace std;
using Int = long long;
//using namespace boost::multiprecision;

const Int MOD = 1000000007;

Int mod_pow(Int x, Int n) {
    Int res = 1;
    while(n > 0) {
        if(n & 1) res = (res * x) % MOD; //ビット演算(最下位ビットが1のとき)
        x = (x * x) % MOD;
        n >>= 1; //右シフト(n = n >> 1)
    }
    return res;
}

// エラトステネスの篩
// [0,n) の範囲の i について、primes[i] != 0 ⇔ i は素数
vector<int> erat(int n) {
    vector<int> primes(n);
    for (int i = 2; i < n; ++i) primes[i] = i;
    for (int i = 2; i*i < n; ++i)
        if (primes[i])
            for (int j = i*i; j < n; j+=i) primes[j] = 0;
    // 素数のみをvectorに格納する場合は以下の行を追加 (0 である要素を全削除)
    // primes.erase(remove(primes.begin(), primes.end(), 0), primes.end());
    return primes;
}

int dp[20001];

int main(){
    cin.tie(0);

    int N; cin >> N;
    vector<int> e = erat(N+1);
    vector<int> p;
    for (int i = 0; i < N+1; i++){
        if (e[i] > 0) p.push_back(i);
    }
    
    for (auto i : p){
        for (int j = 20001; j >= 0; j--){
            if ((j == 0 || dp[j]) && j + i < 20001){
                dp[j+i] = max(dp[j+i], dp[j] + 1);
            } 
        }
    }

    if (dp[N] > 0) cout << dp[N] << endl;
    else cout << -1 << endl;
} 
0