結果

問題 No.458 異なる素数の和
ユーザー rullonzrullonz
提出日時 2017-10-27 21:48:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 1,856 bytes
コンパイル時間 1,336 ms
コンパイル使用メモリ 166,360 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-01 16:17:27
合計ジャッジ時間 2,548 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 13 ms
5,376 KB
testcase_02 AC 17 ms
5,376 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 4 ms
5,376 KB
testcase_05 AC 33 ms
5,376 KB
testcase_06 AC 16 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 35 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 41 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 15 ms
5,376 KB
testcase_28 AC 40 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 10 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct edge {int /*from**/to,cost;};
typedef long long ll;
typedef pair<int,int> P;
typedef vector<int> VI;
typedef vector<long long int> VL;
typedef vector<edge> VE;
//static const int INF = 2147483647;
//static const long long INf = 9223372000000000000;
//static const long long INF = 9223372000000000000/2;
static const int INF = 1000010000;
static const int NIL = -1;
static const int MOD = 1000000007;
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define np next_permutation
#define pq priority_queue
#define itn int
#define scnaf scanf
#define reutnr return
#define scamf scanf
//int dx4[4] = {0,1,0,-1}, dy4[4] = {-1,0,1,0};
//int dx5[5] = {-1,0,0,0,1}, dy5[5] = {0,-1,0,1,0};
//int dx8[8] = {-1,0,1,1,1,0,-1,-1}, dy8[8] = {1,1,1,0,-1,-1,-1,0};
//int dx9[9] = {-1,0,1,1,1,0,-1,-1,0}, dy9[9] = {1,1,1,0,-1,-1,-1,0,0};
//#define int ll

const int MAX_N = 20100;

int prime[MAX_N];
bool is_prime[MAX_N];
int dp[MAX_N];
int n,num;

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[p++] = i;
            for(int j=2*i;j<=n;j += i) is_prime[j] = false;
        }
    }
    return p;
}

void print(){
    for(int i=0;i<=n;i++){
        printf("%d ",dp[i]);
    }
    printf("\n");
}

signed main(){
    scanf("%d",&n);
    num = sieve(n);
    for(int i=0;i<num;i++){
        for(int j=n;j>=0;j--){
            if(j-prime[i]>=0){
                if(dp[j-prime[i]] || j==prime[i]){
                    dp[j] = max(dp[j-prime[i]]+1,dp[j]);
                }
            }
        }
        //print();
    }
    //printf("%d\n",num);
    (dp[n] == 0) ? printf("-1\n") : printf("%d\n",dp[n]);
    return 0;
}
0