結果

問題 No.458 異なる素数の和
ユーザー lapilapi
提出日時 2019-04-06 19:54:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,732 bytes
コンパイル時間 3,351 ms
コンパイル使用メモリ 105,748 KB
実行使用メモリ 183,452 KB
最終ジャッジ日時 2023-09-07 01:01:50
合計ジャッジ時間 4,906 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
179,788 KB
testcase_01 AC 59 ms
181,748 KB
testcase_02 AC 66 ms
181,848 KB
testcase_03 AC 40 ms
180,520 KB
testcase_04 AC 42 ms
180,824 KB
testcase_05 AC 101 ms
183,020 KB
testcase_06 AC 67 ms
181,752 KB
testcase_07 AC 36 ms
179,860 KB
testcase_08 AC 101 ms
183,344 KB
testcase_09 AC 37 ms
180,180 KB
testcase_10 RE -
testcase_11 AC 112 ms
183,452 KB
testcase_12 AC 34 ms
179,668 KB
testcase_13 AC 34 ms
179,676 KB
testcase_14 AC 34 ms
180,004 KB
testcase_15 AC 37 ms
179,732 KB
testcase_16 AC 38 ms
180,340 KB
testcase_17 AC 33 ms
179,744 KB
testcase_18 AC 33 ms
180,012 KB
testcase_19 AC 35 ms
179,676 KB
testcase_20 AC 35 ms
179,756 KB
testcase_21 AC 34 ms
179,872 KB
testcase_22 AC 34 ms
179,676 KB
testcase_23 AC 37 ms
179,744 KB
testcase_24 AC 35 ms
179,740 KB
testcase_25 AC 34 ms
179,740 KB
testcase_26 AC 34 ms
179,668 KB
testcase_27 AC 63 ms
181,764 KB
testcase_28 AC 116 ms
183,304 KB
testcase_29 AC 37 ms
180,028 KB
testcase_30 AC 53 ms
181,252 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;
	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