結果
問題 | No.458 異なる素数の和 |
ユーザー |
|
提出日時 | 2018-09-13 21:18:25 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 45 ms / 2,000 ms |
コード長 | 992 bytes |
コンパイル時間 | 1,767 ms |
コンパイル使用メモリ | 169,592 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-07-01 04:30:04 |
合計ジャッジ時間 | 3,032 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 28 |
ソースコード
#include <bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i=a;i<b;i++) typedef long long ll; const ll inf = 100100100100100; vector<int> eratosthenes(int n){ int arr[n+1]; rep(i,0,n+1){ arr[i] = 1; } arr[0] = 0; arr[1] = 0; for(int i = 2; i < n; i++){ if(arr[i]==1){ for(int j = 2*i; j < n+1; j+=i){ arr[j] = 0; } } } vector<int> ret; for(int i = 2; i<n+1; i++){ if(arr[i] == 1){ ret.push_back(i); } } return ret; } int main(){ std::ios::sync_with_stdio(false); std::cin.tie(0); int n; cin >> n; vector<int> primes = eratosthenes(n); int dp[n+1]; rep(i,0,n+1) dp[i] = -1; dp[0] = 0; for(auto e : primes){ for(int i = n; i > -1; i--){ if(i + e < n+1 && dp[i] > -1){ dp[i+e] = max(dp[i+e], dp[i] + 1); } } } cout << dp[n] << endl; }