結果

問題 No.458 異なる素数の和
ユーザー hondohondo
提出日時 2020-12-07 11:21:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 205 ms / 2,000 ms
コード長 1,426 bytes
コンパイル時間 1,571 ms
コンパイル使用メモリ 172,384 KB
実行使用メモリ 472,508 KB
最終ジャッジ日時 2023-10-17 16:26:24
合計ジャッジ時間 7,023 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 106 ms
472,460 KB
testcase_01 AC 134 ms
472,484 KB
testcase_02 AC 142 ms
472,488 KB
testcase_03 AC 111 ms
472,472 KB
testcase_04 AC 113 ms
472,472 KB
testcase_05 AC 188 ms
472,508 KB
testcase_06 AC 140 ms
472,488 KB
testcase_07 AC 105 ms
472,460 KB
testcase_08 AC 188 ms
472,508 KB
testcase_09 AC 106 ms
472,468 KB
testcase_10 AC 105 ms
472,456 KB
testcase_11 AC 205 ms
472,508 KB
testcase_12 AC 105 ms
472,456 KB
testcase_13 AC 105 ms
472,456 KB
testcase_14 AC 106 ms
472,456 KB
testcase_15 AC 107 ms
472,456 KB
testcase_16 AC 110 ms
472,468 KB
testcase_17 AC 105 ms
472,460 KB
testcase_18 AC 105 ms
472,460 KB
testcase_19 AC 105 ms
472,456 KB
testcase_20 AC 105 ms
472,460 KB
testcase_21 AC 105 ms
472,460 KB
testcase_22 AC 105 ms
472,460 KB
testcase_23 AC 105 ms
472,460 KB
testcase_24 AC 105 ms
472,460 KB
testcase_25 AC 104 ms
472,460 KB
testcase_26 AC 105 ms
472,460 KB
testcase_27 AC 144 ms
472,484 KB
testcase_28 AC 201 ms
472,508 KB
testcase_29 AC 107 ms
472,464 KB
testcase_30 AC 142 ms
472,484 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define loop(__x, __start, __end) for(int __x = __start; __x < __end; __x++)
template <class T> bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false; }
template <typename T>  ostream& operator<<(ostream& os, const vector<T>& v)  {  for (int i = 0; i < v.size(); ++i) {  os << v[i];  if (i != v.size() - 1)  os << ' ';  } return os;  }


vector<ll> prime(int n) {
  vector<bool> ok(n+1, true);
  ok[0] = ok[1] = false;
  for (int i=1; i*i<=n; i++) {
    if (!ok[i]) continue;

    for (int j=i*2; j<=n; j+=i) {
      ok[j] = false;
    }
  }

  vector<ll> ret;
  for (int i=2; i<=n; i++)
    if (ok[i]) ret.push_back(i);
  return ret;
}

ll dp[3000][20001];

int main() {
  int n; cin >> n;
  auto P = prime(n);
  int m = P.size();
  memset(dp, -1, sizeof(dp));
  dp[0][0] = 0;
  for (int i=0; i<m; i++) {
    ll w = P[i];
    ll v = 1;
    for (int cap=0; cap<n; cap++) {
      chmax(dp[i+1][cap], dp[i][cap]);
      if (dp[i][cap] == -1) continue;
      if (cap+w>n) continue;
        // cout << i << " -> " << i+p << endl;
        // cout << dp[i] << "   " << dp[i+p] << endl;
      chmax(dp[i+1][cap+w], dp[i][cap] + v);
    }
  }

  // loop(j,0,m+1) {
  //   loop(i,0,n+1) {
  //     cout << dp[j][i] << " ";
  //   }
  //   cout << endl;
  // }

  ll ans = -1;
  loop(i,0,m+1) chmax(ans, dp[i][n]);
  cout << ans << endl;
  return 0;
}
0