結果
問題 |
No.458 異なる素数の和
|
ユーザー |
![]() |
提出日時 | 2018-01-02 23:27:26 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,178 bytes |
コンパイル時間 | 715 ms |
コンパイル使用メモリ | 77,336 KB |
実行使用メモリ | 180,152 KB |
最終ジャッジ日時 | 2024-12-23 01:14:23 |
合計ジャッジ時間 | 2,526 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 24 WA * 4 |
ソースコード
#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[2300][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; prime(n); for(int i=1;i<=a[0];i++)for(int j=0;j<=n;j++)dp[i][j]=-1; dp[1][2]=1; 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; }