結果

問題 No.458 異なる素数の和
ユーザー puni_kyopropuni_kyopro
提出日時 2019-05-04 23:33:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 44 ms / 2,000 ms
コード長 1,992 bytes
コンパイル時間 703 ms
コンパイル使用メモリ 83,248 KB
実行使用メモリ 5,324 KB
最終ジャッジ日時 2023-09-05 16:44:14
合計ジャッジ時間 2,612 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,920 KB
testcase_01 AC 14 ms
4,992 KB
testcase_02 AC 19 ms
5,112 KB
testcase_03 AC 5 ms
5,004 KB
testcase_04 AC 6 ms
5,108 KB
testcase_05 AC 38 ms
4,956 KB
testcase_06 AC 17 ms
5,188 KB
testcase_07 AC 2 ms
4,904 KB
testcase_08 AC 38 ms
4,968 KB
testcase_09 AC 3 ms
4,944 KB
testcase_10 AC 2 ms
5,024 KB
testcase_11 AC 44 ms
5,232 KB
testcase_12 AC 2 ms
5,020 KB
testcase_13 AC 3 ms
4,972 KB
testcase_14 AC 3 ms
4,912 KB
testcase_15 AC 2 ms
4,960 KB
testcase_16 AC 4 ms
4,996 KB
testcase_17 AC 3 ms
5,044 KB
testcase_18 AC 2 ms
4,916 KB
testcase_19 AC 3 ms
4,908 KB
testcase_20 AC 2 ms
5,104 KB
testcase_21 AC 3 ms
4,972 KB
testcase_22 AC 3 ms
4,908 KB
testcase_23 AC 2 ms
4,932 KB
testcase_24 AC 3 ms
4,916 KB
testcase_25 AC 2 ms
4,904 KB
testcase_26 AC 3 ms
4,960 KB
testcase_27 AC 16 ms
5,324 KB
testcase_28 AC 43 ms
4,964 KB
testcase_29 AC 3 ms
5,088 KB
testcase_30 AC 11 ms
5,028 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<cctype>
#include<utility>
#include<string>
#include<cmath>
#include<cstring>
#include<queue>
#include<map>
#include<set>

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)

using namespace std;
typedef pair<int, int> pii;

typedef long long int ll;



template<typename T>
vector<T> make_v(size_t a) { return vector<T>(a); }
template<typename T, typename... Ts>
auto make_v(size_t a, Ts... ts) {
	return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template<typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type
fill_v(T& t, const V& v) { t = v; }
template<typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type
fill_v(T& t, const V& v) {
	for (auto& e : t) fill_v(e, v);
}


#define ARRAY_MAX 200005
const ll INF = 1e9 + 7;


/*nまでの素数を列挙して配列に保持*/
vector<ll> Eratosthenes(ll n) {

	vector<ll> arr(n);
	for (ll i = 0; i < n; i++) {
		arr[i] = 1;
	}
	arr[0] = arr[1] = 0;
	for (ll i = 2; i < sqrt(n); i++) {
		if (arr[i]) {
			for (ll j = 0; i * (j + 2) < n; j++) {
				arr[i *(j + 2)] = 0;
			}
		}
	}
	return arr;
}

ll dp[ARRAY_MAX];

int main() {

	ll n;
	cin >> n;
	vector<ll> num = Eratosthenes(n + 3);
	for (int i = 0; i < ARRAY_MAX; i++)
	{
		dp[i] = -1LL;
	}

	dp[0] = 0LL;
	
	for (int i = 2; i <= n; i++)
	{
		if (num[i] == 0)
		{
			//素数ではない場合
			continue;
		}
		ll tmp = i;//今見ている素数

		//素数の場合
		for (int j = n; j >= 0; j--)
		{

			if (j - tmp >= 0 && dp[j - tmp] != -1LL)
			{
				dp[j] = max(dp[j], dp[j - tmp] + 1LL);
			}
		}
	}
	if (dp[n] == 0)
	{
		cout << "-1" << endl;
	}
	else
	{
		cout << dp[n] << endl;
	}

	return 0;
}



0