結果

問題 No.458 異なる素数の和
ユーザー lapilapi
提出日時 2019-04-06 19:56:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 128 ms / 2,000 ms
コード長 1,784 bytes
コンパイル時間 902 ms
コンパイル使用メモリ 103,740 KB
実行使用メモリ 183,300 KB
最終ジャッジ日時 2023-09-07 01:07:56
合計ジャッジ時間 3,914 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
130,096 KB
testcase_01 AC 66 ms
157,244 KB
testcase_02 AC 76 ms
162,284 KB
testcase_03 AC 43 ms
140,776 KB
testcase_04 AC 45 ms
142,600 KB
testcase_05 AC 116 ms
180,712 KB
testcase_06 AC 72 ms
161,700 KB
testcase_07 AC 29 ms
129,112 KB
testcase_08 AC 116 ms
180,856 KB
testcase_09 AC 36 ms
135,960 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 128 ms
183,248 KB
testcase_12 AC 32 ms
156,380 KB
testcase_13 AC 33 ms
156,448 KB
testcase_14 AC 33 ms
156,672 KB
testcase_15 AC 33 ms
156,564 KB
testcase_16 AC 40 ms
161,280 KB
testcase_17 AC 33 ms
156,768 KB
testcase_18 AC 33 ms
156,496 KB
testcase_19 AC 33 ms
156,388 KB
testcase_20 AC 32 ms
156,644 KB
testcase_21 AC 32 ms
156,392 KB
testcase_22 AC 32 ms
156,676 KB
testcase_23 AC 33 ms
156,500 KB
testcase_24 AC 33 ms
156,828 KB
testcase_25 AC 33 ms
156,652 KB
testcase_26 AC 33 ms
156,548 KB
testcase_27 AC 68 ms
172,192 KB
testcase_28 AC 116 ms
183,300 KB
testcase_29 AC 36 ms
158,824 KB
testcase_30 AC 57 ms
168,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <numeric>
#include <regex>
#include <tuple>
#include<iomanip>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
#define MOD 1000000007 // 10^9 + 7
#define INF 1000000000 // 10^9
#define LLINF 1LL<<60


bool isPrime[20001];

// n番目までの数について素数かどうか判断する
void primeTable(int n) {
	for (int i = 1; i <= n; i++) isPrime[i] = true;
	isPrime[0] = isPrime[1] = false;

	for (int i = 2; i <= n; i++) {
		if (isPrime[i]) {
			for (int j = 2 * i; j <= n; j += i) isPrime[j] = false; // iの倍数はすべて素数でない
		}
	}
}

vector<int> primeArray;

int dp[2300][20009]; // dp[i][j] : 0番目の素数からi番目の素数を使ってjを表す時の最長の長さ
				    // 無理な時は-1

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int N; cin >> N;
	if (N == 1) {
		cout << -1 << endl;
		return 0;
	}
	for (int i = 0; i < 2300; i++) {
		for (int j = 0; j <= N; j++) dp[i][j] = -1;
	}

	// 素数表の準備
	primeTable(N);
	for (int i = 0; i <= N; i++) if (isPrime[i]) primeArray.push_back(i);

	// for (int i = 0; i < primeArray.size(); i++) cout << primeArray[i] << " ";
	// cout << primeArray.size() << endl;
	
	
	for (int i = 0; i < primeArray.size(); i++) dp[i][primeArray[i]] = 1;
	for (int i = 0; i < primeArray.size()-1; i++) {
		for (int j = 0; j <= N; j++) {
			if (dp[i][j] > 0) {
				dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
				if (j + primeArray[i + 1] <= N) dp[i + 1][j + primeArray[i + 1]] = max(dp[i + 1][j + primeArray[i + 1]], dp[i][j] + 1);
			}
		}
	}

	cout << dp[primeArray.size() - 1][N] << endl;
	

	return 0;
}
0