結果

問題 No.458 異なる素数の和
ユーザー naotaka nakanenaotaka nakane
提出日時 2019-10-27 00:29:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 202 ms / 2,000 ms
コード長 1,615 bytes
コンパイル時間 1,635 ms
コンパイル使用メモリ 173,536 KB
実行使用メモリ 179,860 KB
最終ジャッジ日時 2023-10-12 22:46:58
合計ジャッジ時間 4,450 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,476 KB
testcase_01 AC 57 ms
53,060 KB
testcase_02 AC 75 ms
69,028 KB
testcase_03 AC 14 ms
14,084 KB
testcase_04 AC 18 ms
17,232 KB
testcase_05 AC 169 ms
150,236 KB
testcase_06 AC 71 ms
65,268 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 168 ms
151,580 KB
testcase_09 AC 5 ms
6,096 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 202 ms
179,860 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 2 ms
4,352 KB
testcase_15 AC 2 ms
4,352 KB
testcase_16 AC 9 ms
9,564 KB
testcase_17 AC 1 ms
4,352 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,352 KB
testcase_21 AC 1 ms
4,352 KB
testcase_22 AC 2 ms
4,352 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 1 ms
4,348 KB
testcase_26 AC 2 ms
4,352 KB
testcase_27 AC 67 ms
61,912 KB
testcase_28 AC 194 ms
176,284 KB
testcase_29 AC 3 ms
4,568 KB
testcase_30 AC 42 ms
39,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)   FOR(i,0,n)
#define ALL(obj)   (obj).begin(),(obj).end()

#define debug(var)  do{std::cout << __LINE__ << "> " << #var << ": ";view(var);}while(0)
template<typename T> void view(T e){std::cout << e << std::endl;}
template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cout << e << " "; } std::cout << std::endl;}
template<typename T> void view(const std::vector<std::vector<T> >& vv){ for(const auto& v : vv){ view(v); } }

using namespace std;
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;

struct SieveOfEstosthenes {
    int n;
    vector<bool> is_prime;
    vector<int> prime;
    SieveOfEstosthenes(int n) : n(n), is_prime(n, 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;
            }
        }
    }
};

void Main() {
	int N; cin >> N;
	SieveOfEstosthenes era(N);
	vi prime = era.prime;
	vvi dp(prime.size() + 1, vi(N + 1, -1));
	dp[0][0] = 0;
	REP(i, prime.size()) {
		REP(j, N + 1) {
			dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
			if (j >= prime[i] && dp[i][j - prime[i]] != -1) {
				dp[i + 1][j] = max(dp[i + 1][j], dp[i][j - prime[i]] + 1);
			}
		}
	}
	cout << dp[prime.size()][N] << endl;
}

int main() {
    cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	cout << fixed << setprecision(15);
	Main();
}
0