結果
| 問題 |
No.458 異なる素数の和
|
| コンテスト | |
| ユーザー |
puni_kyopro
|
| 提出日時 | 2019-05-04 23:33:37 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 43 ms / 2,000 ms |
| コード長 | 1,992 bytes |
| コンパイル時間 | 762 ms |
| コンパイル使用メモリ | 85,232 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-06-23 12:13:27 |
| 合計ジャッジ時間 | 2,019 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 28 |
ソースコード
#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;
}
puni_kyopro