結果
問題 |
No.458 異なる素数の和
|
ユーザー |
![]() |
提出日時 | 2018-03-18 18:46:01 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 47 ms / 2,000 ms |
コード長 | 1,319 bytes |
コンパイル時間 | 931 ms |
コンパイル使用メモリ | 83,000 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-12-29 13:49:48 |
合計ジャッジ時間 | 2,226 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 28 |
ソースコード
#include <iostream> #include <algorithm> #include <vector> #include <map> #include <string> #include <numeric> #include <cstring> #include <cmath> #include <set> #include <climits> #include <queue> #include <stack> const int MOD = 1e9 + 7; const int iINF = 1000000000; const long long int llINF = 1000000000000000000; #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; using ll = long long int; using P = pair<int, int>; using edge = struct { int to; int cost; }; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; bool isPrime[20010]; void Eratosthenes(int N) { memset(isPrime, true, sizeof(isPrime)); isPrime[0] = false; isPrime[1] = false; for(int i = 2; i <= sqrt(N); i++) { if(isPrime[i]) { for(int j = i * 2; j <= N; j += i) { isPrime[j] = false; } } } } int dp[20010]; int main() { int N; cin >> N; Eratosthenes(N); memset(dp, -1, sizeof(dp)); dp[0] = 0; rep(i, N + 1) { if(isPrime[i]) { for(int j = N; 0 <= j; j--) { if(i + j > N || dp[j] == -1) continue; dp[j + i] = max(dp[j + i], dp[j] + 1); } } } cout << dp[N] << endl; return 0; }