結果

問題 No.458 異なる素数の和
ユーザー tsutajtsutaj
提出日時 2017-05-15 15:54:11
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 62 ms / 2,000 ms
コード長 1,952 bytes
コンパイル時間 1,838 ms
コンパイル使用メモリ 97,292 KB
実行使用メモリ 4,372 KB
最終ジャッジ日時 2023-10-14 13:13:05
合計ジャッジ時間 3,169 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,368 KB
testcase_01 AC 19 ms
4,368 KB
testcase_02 AC 25 ms
4,372 KB
testcase_03 AC 6 ms
4,368 KB
testcase_04 AC 7 ms
4,372 KB
testcase_05 AC 52 ms
4,368 KB
testcase_06 AC 23 ms
4,368 KB
testcase_07 AC 2 ms
4,368 KB
testcase_08 AC 53 ms
4,372 KB
testcase_09 AC 3 ms
4,372 KB
testcase_10 AC 2 ms
4,372 KB
testcase_11 AC 62 ms
4,372 KB
testcase_12 AC 2 ms
4,368 KB
testcase_13 AC 2 ms
4,372 KB
testcase_14 AC 1 ms
4,368 KB
testcase_15 AC 2 ms
4,368 KB
testcase_16 AC 5 ms
4,372 KB
testcase_17 AC 2 ms
4,368 KB
testcase_18 AC 2 ms
4,368 KB
testcase_19 AC 2 ms
4,368 KB
testcase_20 AC 1 ms
4,368 KB
testcase_21 AC 2 ms
4,368 KB
testcase_22 AC 2 ms
4,368 KB
testcase_23 AC 2 ms
4,368 KB
testcase_24 AC 2 ms
4,372 KB
testcase_25 AC 2 ms
4,372 KB
testcase_26 AC 2 ms
4,368 KB
testcase_27 AC 22 ms
4,368 KB
testcase_28 AC 62 ms
4,368 KB
testcase_29 AC 2 ms
4,368 KB
testcase_30 AC 14 ms
4,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <climits>
#include <ctime>
#include <cassert>
using namespace std;

#define rep(i,a,n) for(int i=a; i<n; i++)
#define repq(i,a,n) for(int i=a; i<=n; i++)
#define repr(i,a,n) for(int i=a; i>=n; i--)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF 2000000000
#define int long long int

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) ( EQ((a).X, (b).X) && EQ((a).Y, (b).Y) )
#define LE(n, m) ((n) < (m) + EPS)
#define LEQ(n, m) ((n) <= (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define GEQ(n, m) ((n) + EPS >= (m))

typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long ll;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;

int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};
int const MOD = 1000000007;

namespace std {
    bool operator<(const P& a, const P& b) {
        return a.X != b.X ? a.X < b.X : a.Y < b.Y;
    }
}

vector<int> primes(int n) {
    bool used[20010] = {};
    vector<int> ret;
    rep(i,2,n+1) {
        if(!used[i]) {
            ret.push_back(i);
            int d = i;
            while(d <= n) used[d] = true, d += i;
        }
    }
    return ret;
}

int dp[20010];

signed main() {
    int n; cin >> n;
    vector<int> prm = primes(n);
    memset(dp, -1, sizeof(dp));
    dp[0] = 0;
    rep(i,0,prm.size()) {
        // printf("debug: prime = %lld\n", prm[i]);
        repr(j,n,0) {
            if(j - prm[i] < 0 || dp[j-prm[i]] == -1) continue;
            dp[j] = max(dp[j], dp[j-prm[i]] + 1);
        }
    }
    cout << dp[n] << endl;
    return 0;
}
0