結果

問題 No.458 異なる素数の和
ユーザー dnishdnish
提出日時 2018-06-23 01:31:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 24 ms / 2,000 ms
コード長 1,264 bytes
コンパイル時間 1,435 ms
コンパイル使用メモリ 168,072 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-13 08:15:32
合計ジャッジ時間 2,892 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 8 ms
4,380 KB
testcase_02 AC 10 ms
4,380 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 4 ms
4,376 KB
testcase_05 AC 20 ms
4,376 KB
testcase_06 AC 10 ms
4,384 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 21 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 24 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 9 ms
4,384 KB
testcase_28 AC 24 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 6 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
#define REP(i,n,N) for(int i=(n); i<(N); i++)
#define RREP(i,n,N) for(ll i=(N-1); i>=n; i--)
#define CK(n,a,b) ((a)<=(n)&&(n)<(b))
#define ALL(v) (v).begin(),(v).end()
#define p(s) cout<<(s)<<endl
#define p2(a,b) cout<<(a)<<" "<<(b)<<endl
#define v2(T) vector<vector<T>>
typedef long long ll;
using namespace std;
const ll mod= 1e10;

int dp[20010];

vector<int> hurui_v(int n) {
    vector<int> v;
    bool *isPrime_v;
    isPrime_v = new bool[n + 1]; // 処理用のメモリ確保

    // 初期化
    isPrime_v[0] = isPrime_v[1] = false;
    for (int i = 2; i < n + 1; i++) isPrime_v[i] = true;

    // isPrime[0~n]で素数のものだけtrueにする
    for (int i = 2; i*i <= n; i++) {
        for (int j = i * 2; j < n + 1; j += i) {
            isPrime_v[j] = false;
        }
    }

    // 素数のみ追加
    for (int i = 0; i < n + 1; i++) {
        if (isPrime_v[i]) v.push_back(i);
    }

    delete[] isPrime_v; //メモリ解放
    return v;
}
int main(){
    int N;
    cin>>N;
    auto prime = hurui_v(N);
    REP(i,1,N+1){
        dp[i]=-1;
    }
    dp[0]=0;
    for(auto pr:prime){
        RREP(i,pr,N+1){
            if(dp[i-pr]!=-1) dp[i] = max(dp[i], dp[i-pr]+1);
        }
    }
    p(dp[N]);
    return 0;
}
0