結果

問題 No.458 異なる素数の和
ユーザー amowweeamowwee
提出日時 2016-12-29 15:53:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 811 bytes
コンパイル時間 641 ms
コンパイル使用メモリ 72,136 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-18 15:16:05
合計ジャッジ時間 1,953 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
外部呼び出し有り
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,380 KB
testcase_01 AC 9 ms
4,380 KB
testcase_02 AC 11 ms
4,376 KB
testcase_03 AC 5 ms
4,380 KB
testcase_04 AC 5 ms
4,380 KB
testcase_05 AC 19 ms
4,380 KB
testcase_06 AC 10 ms
4,380 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 19 ms
4,380 KB
testcase_09 AC 4 ms
4,380 KB
testcase_10 AC 4 ms
4,380 KB
testcase_11 AC 22 ms
4,376 KB
testcase_12 AC 4 ms
4,376 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 4 ms
4,384 KB
testcase_15 AC 4 ms
4,376 KB
testcase_16 AC 4 ms
4,384 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 4 ms
4,384 KB
testcase_19 AC 3 ms
4,376 KB
testcase_20 AC 4 ms
4,376 KB
testcase_21 AC 4 ms
4,384 KB
testcase_22 AC 3 ms
4,376 KB
testcase_23 AC 4 ms
4,376 KB
testcase_24 AC 3 ms
4,380 KB
testcase_25 AC 4 ms
4,384 KB
testcase_26 AC 4 ms
4,380 KB
testcase_27 AC 10 ms
4,380 KB
testcase_28 AC 22 ms
4,376 KB
testcase_29 AC 4 ms
4,380 KB
testcase_30 AC 8 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <iostream>

std::vector<int> sieve(int n)
{
	std::vector<bool> nums(n + 1, true);
	std::vector<int> primes;

	for (int i = 2; i *i <= n; i++)
	{
		if (nums[i])
		{
			for (int j = 2; i*j <= n; j++)
			{
				nums[i*j] = false;
			}
		}
	}
	for (int i = 2; i < nums.size(); i++)
	{
		if (nums[i])
		{
			primes.push_back(i);
		}
	}
	return primes;
}

int main(void)
{
	int n;
	std::cin >> n;
	std::vector<int> primes = sieve(n);
	std::vector<int> dp(n + 1, 0);
	for (auto p: primes)
	{
		for (int i = n; i  >= p+2; i--)
		{
			int ref = i - p;
			if (dp[ref] && dp[ref] >= dp[i])
			{
				dp[i ] = dp[ref] + 1;
			}
		}
		if (!dp[p])
		{
			dp[p] = 1;
		}
	}
	if (dp[n])
	{
		std::cout << dp[n] << std::endl;
	}
	else
	{
		std::cout << -1 << std::endl;
	}
	system("pause");
	return 0;
}
0