結果

問題 No.458 異なる素数の和
ユーザー RonlynesRonlynes
提出日時 2017-09-08 15:00:40
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 63 ms / 2,000 ms
コード長 786 bytes
コンパイル時間 453 ms
コンパイル使用メモリ 54,876 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-24 20:07:30
合計ジャッジ時間 1,744 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,248 KB
testcase_01 AC 33 ms
5,376 KB
testcase_02 AC 38 ms
5,376 KB
testcase_03 AC 15 ms
5,376 KB
testcase_04 AC 18 ms
5,376 KB
testcase_05 AC 60 ms
5,376 KB
testcase_06 AC 37 ms
5,376 KB
testcase_07 AC 4 ms
5,376 KB
testcase_08 AC 61 ms
5,376 KB
testcase_09 AC 10 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 63 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 12 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 2 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 1 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 35 ms
5,376 KB
testcase_28 AC 60 ms
5,376 KB
testcase_29 AC 7 ms
5,376 KB
testcase_30 AC 27 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
using namespace std;

int prime[20001];
bool is_prime[20001];
int p = 0;
void eratos(int n){
    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;
            }
        }
    }
}

int main(void){
    int n;
    cin >> n;
    eratos(20000);
    int dp[20000];
    fill(dp, dp+20000, -1);
    dp[0] = 0;
    for(int i=0; i<p; i++){
        for(int j=n; j >= 0; j--){
            if(dp[j] != -1 && prime[i] + j <= n){
                dp[prime[i]+j] = max(dp[prime[i]+j], dp[j]+1);
            }
        }
    }
    cout << dp[n] << endl;

    return 0;
}
0