結果

問題 No.458 異なる素数の和
ユーザー lapilapi
提出日時 2019-04-06 19:44:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,725 bytes
コンパイル時間 1,027 ms
コンパイル使用メモリ 105,132 KB
実行使用メモリ 7,660 KB
最終ジャッジ日時 2023-09-07 00:33:23
合計ジャッジ時間 3,362 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
7,372 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 4 ms
7,496 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 5 ms
7,416 KB
testcase_11 WA -
testcase_12 AC 4 ms
7,504 KB
testcase_13 AC 5 ms
7,496 KB
testcase_14 AC 5 ms
7,544 KB
testcase_15 AC 5 ms
7,508 KB
testcase_16 WA -
testcase_17 AC 4 ms
7,448 KB
testcase_18 AC 4 ms
7,636 KB
testcase_19 AC 4 ms
7,452 KB
testcase_20 AC 5 ms
7,636 KB
testcase_21 AC 4 ms
7,492 KB
testcase_22 AC 4 ms
7,412 KB
testcase_23 AC 5 ms
7,568 KB
testcase_24 AC 4 ms
7,500 KB
testcase_25 AC 5 ms
7,504 KB
testcase_26 AC 4 ms
7,520 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 5 ms
7,384 KB
testcase_30 WA -
権限があれば一括ダウンロードができます

ソースコード

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[2001];

// 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[1000][2001]; // 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 < 400; i++) {
		for (int j = 0; j <= 2000; j++) dp[i][j] = -1;
	}

	// 素数表の準備
	primeTable(2000);
	for (int i = 0; i <= 2000; 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()-1; i++) {
		dp[i][primeArray[i]] = max(dp[i][primeArray[i]], 1);
		for (int j = 0; j <= 2000; j++) {
			if (dp[i][j] > 0) {
				dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
				if (j + primeArray[i + 1] <= 2000) 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