結果

問題 No.458 異なる素数の和
ユーザー 8991tae
提出日時 2018-01-02 23:33:02
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 197 ms / 2,000 ms
コード長 1,231 bytes
コンパイル時間 978 ms
コンパイル使用メモリ 68,776 KB
実行使用メモリ 180,096 KB
最終ジャッジ日時 2024-12-23 01:14:26
合計ジャッジ時間 2,496 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #



#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<iostream>
typedef long long ll;
using namespace std;

#define N 20005
int a[N];
int dp[3000][N];
void prime(int n){
    int t=1;
    for(int i=2;i<=n;i++){
        bool flag=true;
        for(int j=2;j<=sqrt(i);j++){
            if(i%j==0)flag=false;
        }
        if(flag==true){
            a[t]=i;
            t++;
        }
    }
    a[0]=t-1;/*a[0]が個数*/
}

int main(){
    int n;cin>>n;
    if(n!=1)prime(n);
    else prime(2);
    for(int i=1;i<=a[0];i++)for(int j=0;j<=n;j++)dp[i][j]=-1;
    dp[1][2]=1;
    dp[1][0]=0;
    for(int i=2;i<=a[0];i++){
        for(int j=0;j<=n;j++){
            if(j<a[i])dp[i][j]=dp[i-1][j];
            else if(dp[i-1][j]!=-1&&dp[i-1][j-a[i]]!=-1){
                dp[i][j]=max(dp[i-1][j],dp[i-1][j-a[i]]+1);
            }else if(dp[i-1][j]!=-1){
                dp[i][j]=dp[i-1][j];
            }else if(dp[i-1][j-a[i]]!=-1){
                dp[i][j]=dp[i-1][j-a[i]]+1;
            }else{
                dp[i][j]=-1;
            }
        }
    }
        cout<<dp[a[0]][n]<<endl;
    
    
    return 0;
}


0