結果

問題 No.458 異なる素数の和
ユーザー motakinemotakine
提出日時 2020-05-13 10:39:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 230 ms / 2,000 ms
コード長 2,348 bytes
コンパイル時間 1,530 ms
コンパイル使用メモリ 171,564 KB
実行使用メモリ 180,292 KB
最終ジャッジ日時 2023-10-12 16:52:34
合計ジャッジ時間 4,211 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 68 ms
53,284 KB
testcase_02 AC 86 ms
69,160 KB
testcase_03 AC 17 ms
14,152 KB
testcase_04 AC 21 ms
17,316 KB
testcase_05 AC 192 ms
150,296 KB
testcase_06 AC 83 ms
65,196 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 191 ms
151,652 KB
testcase_09 AC 6 ms
6,176 KB
testcase_10 AC 1 ms
4,352 KB
testcase_11 AC 230 ms
180,292 KB
testcase_12 AC 1 ms
4,352 KB
testcase_13 AC 1 ms
4,352 KB
testcase_14 AC 1 ms
4,352 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 10 ms
9,096 KB
testcase_17 AC 1 ms
4,352 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,352 KB
testcase_21 AC 1 ms
4,352 KB
testcase_22 AC 1 ms
4,352 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 1 ms
4,352 KB
testcase_25 AC 1 ms
4,352 KB
testcase_26 AC 1 ms
4,348 KB
testcase_27 AC 79 ms
62,000 KB
testcase_28 AC 225 ms
176,208 KB
testcase_29 AC 4 ms
4,740 KB
testcase_30 AC 49 ms
39,196 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, n) for (int i=0; i<(int)(n); i++)
#define all(v) v.begin(), v.end()
#define PRINT(v) for (auto x : (v)) cout <<x <<" " ; cout <<endl;
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using mat = vector<vector<ll>>;
const ll MOD = 1000000007;
const ll INF = 10000000000000000;
const int inf = 1001001001;
vector<int> x4 = {0, 1, 0, -1}, x8 = {0, 1, 1, 1, 0, -1, -1, -1};
vector<int> y4 = {1, 0, -1, 0}, y8 = {1, 1, 0, -1, -1, -1, 0, 1};
template<class T> inline bool chmin(T& a, T b){if (a>b){a = b; return true;}return false;}
template<class T> inline bool chmax(T& a, T b){if (a<b){a = b; return true;}return false;}
template<class T> inline T powerM(T a,T b){if (b==0) return 1;
T tmp = powerM(a,b/2); if (b%2==0) return tmp*tmp%MOD; else return tmp*tmp%MOD*a%MOD; }
template<class T> inline T power(T a,T b,T m){ if (b==0) return 1;
  T tmp = power(a,b/2,m); if (b%2==0) return tmp*tmp%m; else return tmp*tmp%m*a%m; }
template<class T> inline T gcd(T a, T b){if (b==0) return a; return gcd(b, a%b);}
template<class T> inline T lcm(T a, T b){return a / gcd(a,b) * b;}
// ax+by=gcd(a,b)を解く
template<class T> inline T extgcd(T a,T b,T &x,T &y){if (b==0){x=1; y=0; return a;} T d=extgcd(b,a%b,y,x); y -= a/b*x; return d;}
void hey(){ cout <<"hey" <<endl; }

template<class T> struct edge { int to; T cost;};


// Eratosthenesの篩-------------------------------------------
// n以下の素数の数を返す
int sieve(int n, vector<int> &prime){
  // vector<int> prime(n);             // i番目の素数
  vector<bool> is_prime(n+1, true); // is_prime[i]=trueならiは素数
  int p=0;
  is_prime[0] = is_prime[1] = false;
  for (int i=2; i<=n; i++){
    if (is_prime[i]){
      prime[p++] = i;
      for (int j=2*i; j<=n; j+=i) is_prime[j] = false;
    }
  }
  return p;
}

int main() {
  int M; cin >>M;
  vector<int> prime(M);
  int N = sieve(M, prime);
  vector<vector<int>> dp(N+1, vector<int>(M+1, -1));
  dp[0][0] = 0;
  // dp[i][j] := i番目までの素数を使って和をjにするときの最大の回数
  for (int i=0; i<N; i++){
    for (int j=0; j<=M; j++){
      if (dp[i][j] == -1) continue;
      chmax(dp[i+1][j], dp[i][j]);
      if (j+prime[i] <= M) chmax(dp[i+1][j+prime[i]], dp[i][j] + 1);
    }
  }
  int res = dp[N][M];
  cout <<res <<endl;
}
0