結果

問題 No.458 異なる素数の和
ユーザー nuxxpp
提出日時 2019-11-19 23:42:17
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,366 bytes
コンパイル時間 622 ms
コンパイル使用メモリ 84,816 KB
実行使用メモリ 814,336 KB
最終ジャッジ日時 2024-10-04 14:12:33
合計ジャッジ時間 4,681 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 3
other RE * 10 MLE * 1 -- * 17
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int sieve(int)’:
main.cpp:52:1: warning: no return statement in function returning non-void [-Wreturn-type]
   52 | }
      | ^

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <queue>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <bitset>
#include <set>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)
#define FOR(i,k,n) for(int i=(k);i<(int)(n);i++)
#define all(i,n) (i),(i+n)

int dx4[4]={1,0,-1,0};
int dy4[4]={0,-1,0,1};
int dx8[8]={1,0,-1,1,-1,1,0,-1};
int dy8[8]={1,1,1,0,0,-1,-1,-1};

typedef pair<int, int> P;
typedef pair<string, int> SP;
typedef long long ll;

const int INF = 1e9;
const ll LLINF = 1e18;
const int MAX_V = 1e6+1;
const ll mod = 1000000007;

// --------------------------------------
int n;
int dp[20005];

vector<int> prime;
bool is_prime[20005];

int sieve(int n) {
  int p = 0;
  for(int i=0; i<=n; i++) is_prime[i] = true;
  is_prime[0] = is_prime[1] = false;

  for(int i=2; i<=n; i++) {
    if(is_prime[i]) {
      prime.push_back(i);
      for(int j=2*i; j<=n; j+=i) is_prime[j] = false;
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n;
  sieve(n);

  REP(i, n + 1) dp[i] = -1;
  dp[0] = 0;

  for(auto p: prime) {
    for(int j=n; j>=0; j--) {
      if(p + j <= n && dp[j] != -1) {
        dp[p + j] = max(dp[p + j], dp[j] + 1);
      }
    }
  }

  cout << dp[n] << endl;
}
0