結果

問題 No.458 異なる素数の和
ユーザー 波多野充波多野充
提出日時 2021-10-21 13:25:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 221 ms / 2,000 ms
コード長 1,918 bytes
コンパイル時間 4,538 ms
コンパイル使用メモリ 263,984 KB
実行使用メモリ 180,504 KB
最終ジャッジ日時 2023-10-21 07:45:29
合計ジャッジ時間 6,793 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 64 ms
53,784 KB
testcase_02 AC 84 ms
69,360 KB
testcase_03 AC 16 ms
14,448 KB
testcase_04 AC 21 ms
17,616 KB
testcase_05 AC 184 ms
150,672 KB
testcase_06 AC 81 ms
65,400 KB
testcase_07 AC 3 ms
4,348 KB
testcase_08 AC 186 ms
151,992 KB
testcase_09 AC 7 ms
6,792 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 221 ms
180,504 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 3 ms
4,348 KB
testcase_16 AC 10 ms
9,696 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 75 ms
62,232 KB
testcase_28 AC 216 ms
176,544 KB
testcase_29 AC 4 ms
4,944 KB
testcase_30 AC 47 ms
39,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repd(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define reps(i, a, n) for (int i = (int)(a); i < (int)(n); i++)
#define sortu(a) sort(a.begin(), a.end())
#define sortd(a) sort(a.rbegin(), a.rend())
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define spaced(a) do {cout << a[0]; for (int _ = 1; _ < a.size(); _++) cout << " " << a[_]; cout << endl;} while (0)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
using vi = vector<int>;
using ll = long long;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vs = vector<string>;
using pii = pair<int, int>;
using mint = modint1000000007;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;
template <class T> bool chmax(T &a, const T &b) {if (a < b) {a = b;return 1;} return 0;}
template <class T> bool chmin(T &a, const T &b) {if (b < a) {a = b; return 1;} return 0;}

int main() {
    int n; cin >> n;
    vector<bool> isPrime(n + 1, true); isPrime[0] = isPrime[1] = false;
    reps(i, 2, n) {
        if (!isPrime[i]) continue;
        for (int j = 2 * i; j <= n; j+= i) {
            isPrime[j] = false;
        }
    }
    int count = 0;
    rep(i, n + 1) if (isPrime[i]) count++;

    vvi dp(count + 2, vi(n + 1));
    int index = 0;
    reps(i, 2, n + 1) {
        if (isPrime[i]) {
            index++;
            dp[index][i] = 1;
            reps(j, 2, n + 1) {
                chmax(dp[index][j], dp[index - 1][j]);
                if (i <= j && dp[index - 1][j - i] > 0) chmax(dp[index][j], dp[index - 1][j - i] + 1);
            }
        }
    }
    if (dp[count][n] > 0) cout << dp[count][n] << endl;
    else cout << -1 << endl;
    return 0;
}
0