結果

問題 No.458 異なる素数の和
ユーザー soemonosoemono
提出日時 2020-07-25 00:40:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,653 bytes
コンパイル時間 720 ms
コンパイル使用メモリ 79,684 KB
実行使用メモリ 179,924 KB
最終ジャッジ日時 2023-09-08 07:06:08
合計ジャッジ時間 3,301 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 58 ms
53,156 KB
testcase_02 AC 76 ms
68,996 KB
testcase_03 AC 14 ms
14,056 KB
testcase_04 AC 17 ms
17,300 KB
testcase_05 AC 167 ms
150,356 KB
testcase_06 AC 70 ms
65,004 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 164 ms
151,636 KB
testcase_09 AC 5 ms
6,164 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 196 ms
179,924 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 WA -
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 8 ms
9,180 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 67 ms
61,920 KB
testcase_28 AC 190 ms
176,152 KB
testcase_29 AC 3 ms
4,552 KB
testcase_30 AC 41 ms
39,176 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] >= 2 ? dp[primes_n][n] : -1;
    
    /* for(auto &v : dp){
        for(auto &i : v){
            cout << i << " ";
        }
        cout << endl;
    } */
    cout << ans << endl;
}
0