結果

問題 No.458 異なる素数の和
ユーザー IlagIlag
提出日時 2020-02-05 03:46:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 30 ms / 2,000 ms
コード長 1,064 bytes
コンパイル時間 1,619 ms
コンパイル使用メモリ 169,476 KB
実行使用メモリ 4,848 KB
最終ジャッジ日時 2023-10-21 14:31:40
合計ジャッジ時間 3,291 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,804 KB
testcase_01 AC 11 ms
4,820 KB
testcase_02 AC 13 ms
4,824 KB
testcase_03 AC 4 ms
4,812 KB
testcase_04 AC 5 ms
4,812 KB
testcase_05 AC 26 ms
4,840 KB
testcase_06 AC 13 ms
4,820 KB
testcase_07 AC 3 ms
4,804 KB
testcase_08 AC 25 ms
4,844 KB
testcase_09 AC 3 ms
4,804 KB
testcase_10 AC 3 ms
4,800 KB
testcase_11 AC 30 ms
4,848 KB
testcase_12 AC 3 ms
4,800 KB
testcase_13 AC 3 ms
4,800 KB
testcase_14 AC 3 ms
4,800 KB
testcase_15 AC 3 ms
4,800 KB
testcase_16 AC 4 ms
4,808 KB
testcase_17 AC 3 ms
4,804 KB
testcase_18 AC 3 ms
4,804 KB
testcase_19 AC 2 ms
4,800 KB
testcase_20 AC 3 ms
4,804 KB
testcase_21 AC 3 ms
4,800 KB
testcase_22 AC 2 ms
4,800 KB
testcase_23 AC 3 ms
4,804 KB
testcase_24 AC 2 ms
4,804 KB
testcase_25 AC 3 ms
4,800 KB
testcase_26 AC 3 ms
4,804 KB
testcase_27 AC 12 ms
4,820 KB
testcase_28 AC 29 ms
4,848 KB
testcase_29 AC 3 ms
4,804 KB
testcase_30 AC 9 ms
4,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
typedef long long ll;
// Welcome to my source code!

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

const int MAX_N = 1e5*3;

vector<int> prime;
bool is_prime[MAX_N];

void sieve(int n) {
    for (int i = 0; i <= n; i++) is_prime[i] = true;
    is_prime[0] = is_prime[1] = false;
    for (int i = 2; i <= n; i++) {
        if (is_prime[i]) {
            prime.push_back(i);
            for (int j = 2 * i; j <= n; j += i) is_prime[j] = false;
        }
    }
}

int main() {
    int n;
    cin >> n;
    sieve(n);
    int m = prime.size();
    int dp[MAX_N];
    fill(dp, dp+MAX_N, -1);
    dp[0] = 0;
    for (int i = 0; i < m; i++) {
        for (int j = n; j >= prime[i]; j--) {
            if (dp[j - prime[i]] != -1) chmax(dp[j], dp[j - prime[i]] + 1);
        }
    }
    cout << dp[n] << endl;
}
0