結果

問題 No.458 異なる素数の和
ユーザー ssssskkkkkssssskkkkk
提出日時 2017-11-27 13:49:45
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 964 bytes
コンパイル時間 524 ms
コンパイル使用メモリ 70,404 KB
実行使用メモリ 180,096 KB
最終ジャッジ日時 2024-05-05 13:15:15
合計ジャッジ時間 2,844 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 17 ms
14,080 KB
testcase_04 AC 20 ms
17,536 KB
testcase_05 AC 183 ms
150,400 KB
testcase_06 WA -
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 186 ms
151,680 KB
testcase_09 AC 6 ms
6,528 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 222 ms
180,096 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 10 ms
9,344 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 1 ms
5,376 KB
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 1 ms
5,376 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 4 ms
5,376 KB
testcase_30 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
using namespace std;
int N;
vector<vector<int>> dp;
int idx = 0;
bool is_prime(int num)
{
	if (num < 2) return false;
	else if (num == 2) return true;
	else if (num % 2 == 0) return false; // 偶数はあらかじめ除く
	double sqrtNum = sqrt(num);
	for (int i = 3; i <= sqrtNum; i += 2)
	{
		if (num % i == 0)
		{
			return false;
		}
	}
	return true;
}
int main(void) 
{
	cin >> N;
	dp.push_back(vector<int>(N + 1, 0));
	++dp[idx][2];
	for (int i = 3; i <= N; ++i) {
		if (is_prime(i)) {
			++idx;
			dp.push_back(vector<int>(N + 1, 0));
			for (int j = 0; j <= N; ++j) {
				if (dp[idx - 1][j] != 0) {
					dp[idx][j] = dp[idx - 1][j];
					if (N < j + i) continue;
					dp[idx][j + i] = max(dp[idx][j + i], dp[idx - 1][j] + 1);
				}
			}
			dp[idx][i] = max(dp[idx][i], 1);
		}
	}
	if (dp[idx][N] == 0) dp[idx][N] = -1;
	cout << dp[idx][N] << endl;
	return 0;
}
0