結果

問題 No.458 異なる素数の和
ユーザー soemonosoemono
提出日時 2020-07-25 00:42:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 197 ms / 2,000 ms
コード長 1,652 bytes
コンパイル時間 699 ms
コンパイル使用メモリ 79,816 KB
実行使用メモリ 179,916 KB
最終ジャッジ日時 2023-09-08 07:12:12
合計ジャッジ時間 2,993 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 59 ms
53,268 KB
testcase_02 AC 75 ms
68,984 KB
testcase_03 AC 14 ms
14,184 KB
testcase_04 AC 18 ms
17,280 KB
testcase_05 AC 163 ms
150,280 KB
testcase_06 AC 71 ms
65,048 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 164 ms
151,660 KB
testcase_09 AC 5 ms
6,264 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 197 ms
179,916 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 8 ms
9,156 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 66 ms
61,904 KB
testcase_28 AC 191 ms
176,168 KB
testcase_29 AC 3 ms
4,740 KB
testcase_30 AC 42 ms
39,132 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <climits> // FOO_MAX, FOO_MIN
#include <cmath> 
#include <cstdlib> // abs(int)
#include <numeric>

#define roundup(n,d) ( ((n) + ((d)-1)) / (d) )
#define ll long long
#define assign_max(into, compared) (into = max((into), (compared)))
#define assign_min(into, compared) (into = min((into), (compared)))

using namespace std;

bool is_prime(int n){
    if (n <= 1) return false;
    if (n == 2) return true;
    if (n % 2 == 0) return false;
    const int limit = min(((int)sqrt(n))+1,n);
    for(int i = 3;i <= limit;i+=2){
        if (n % i == 0){
            return false;
        }
    }
    return true;
}
//[2,limit]
vector<int> primenumbers(int limit){
    vector<int> primes;
    for(int i = 2;i <= limit;i++){
        if(is_prime(i)){
            primes.push_back(i);
        }
    }
    return primes;
}


int main(void){
    int n;
    cin >> n;
    auto primes = primenumbers(n);
    const int primes_n = primes.size();
    vector<vector<int>> dp (primes_n+1, vector<int> (n+1));
    for(int i = 0;i < primes_n;i++){
        for(int j = 0;j <= n;j++){
            dp[i+1][j] = dp[i][j];
            if(j-primes[i] < 0){
                continue;
            }

            if(dp[i][j-primes[i]] == 0 && j-primes[i] != 0){
                continue;
            }

            dp[i+1][j] = max(dp[i][j], dp[i][j-primes[i]]+1); 
        }
    }

    int ans = dp[primes_n][n] > 0 ? dp[primes_n][n] : -1;
    
    /* for(auto &v : dp){
        for(auto &i : v){
            cout << i << " ";
        }
        cout << endl;
    } */
    cout << ans << endl;
}
0