結果

問題 No.458 異なる素数の和
ユーザー whesonwheson
提出日時 2018-04-30 09:53:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 1,812 bytes
コンパイル時間 1,659 ms
コンパイル使用メモリ 173,616 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-10 08:19:41
合計ジャッジ時間 3,791 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
//#define int long long

using namespace std;
using LL = long long;
using P = pair<int, int>;
using Tapris = tuple<int, int, int>;

#define FOR(i, a, n) for(int i = (int)(a); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)

#define pb(a) push_back(a)
#define all(x) (x).begin(),(x).end()

const int INF = (int)1e9;
const LL INFL = (LL)1e15;
const int MOD = 1e9 + 7;

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

bool isprime(LL x){
    if(x == 2) return true;
    if(x < 2 || x % 2 == 0) return false;
    LL i = 3;
    LL sqrtx = sqrt(x);
    while(i <= sqrtx){
        if(x % i == 0) return false;
        i += 2;
    }
    return true;
}
// maxまでの素数をvectorに入れて返す
vector<LL> eratos(LL max){
    vector<LL> primeVec;
    vector<bool> isprime;
    double sqrtmax = sqrt(max);
    isprime.resize(max + 1);
    isprime[0] = false; isprime[1] = false;
    FOR(i, 2, max + 1){
        isprime[i] = true;
    }
    FOR(i, 2, sqrtmax){
        if(isprime[i]){
            for(int j = 0; i * (j + 2) < max + 1; ++j){
                isprime[i * (j + 2)] = false;
            }
        }
    }
    REP(i, max + 1){
        if(isprime[i])  primeVec.pb(i);
    }
    return primeVec;
}

/*************** using variables ***************/
int n;
vector<LL> sosuu_list;
int dp[100005];
/**********************************************/

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    cin >> n;
    vector<LL> sosuu_list = eratos(20005);

    REP(i, 20005) dp[i] = -1;
    
    for(auto num : sosuu_list){
        for(int x = 20005; x > num; x--){
            if(dp[x - num] != -1) dp[x] = max(dp[x], dp[x - num] + 1);
        }
        dp[num] = max(dp[num], 1);
    }
    // FOR(i, 1, n+1) cout << dp[i] << endl;
    cout << dp[n] << endl;
}
0