結果
| 問題 |
No.458 異なる素数の和
|
| コンテスト | |
| ユーザー |
takune1024
|
| 提出日時 | 2017-11-26 23:44:08 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 48 ms / 2,000 ms |
| コード長 | 934 bytes |
| コンパイル時間 | 1,288 ms |
| コンパイル使用メモリ | 69,380 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-27 11:54:16 |
| 合計ジャッジ時間 | 1,918 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 28 |
ソースコード
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <utility>
using namespace std;
typedef pair<int, int> P;
typedef unsigned long long ll;
const int inf = 999999999;
const int mod = 1000000007;
vector<int> eratosthenes(int n){
bool arr[n];
for(int i = 0; i <= n; i++){
arr[i] = true;
}
arr[0] = false; arr[1] = false;
for(int i = 2; i * i <= n; i++){
if(arr[i]){
for(int j = i * 2; j <= n; j += i){
arr[j] = false;
}
}
}
vector<int> primes;
for(int i = 0; i <= n; i++){
if(arr[i]) primes.push_back(i);
}
return primes;
}
int main(){
int n; cin >> n;
vector<int> primes = eratosthenes(n);
vector<int> dp(n + 1, -1);
dp[0] = 0;
for(int k = 0; k < primes.size(); k++){
int p = primes[k];
for(int i = n; i >= 0; i--){
if(i + p < n + 1 && dp[i] != -1){
dp[i + p] = max(dp[i + p], dp[i] + 1);
}
}
}
cout << dp[n] << endl;
return 0;
}
takune1024