結果
問題 | No.458 異なる素数の和 |
ユーザー |
![]() |
提出日時 | 2018-09-20 10:52:08 |
言語 | C++11 (gcc 13.3.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,289 bytes |
コンパイル時間 | 673 ms |
コンパイル使用メモリ | 82,516 KB |
実行使用メモリ | 10,624 KB |
最終ジャッジ日時 | 2024-07-18 08:28:28 |
合計ジャッジ時間 | 8,095 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 27 WA * 1 |
ソースコード
#include <cstdio> #include <cstring> #include <iostream> #include <string> #include <cmath> #include <bitset> #include <vector> #include <map> #include <set> #include <queue> #include <deque> #include <algorithm> using namespace std; typedef long long int ll; typedef pair<int, int> P; const int MAX=7001; const int INF=1e9; vector<int> prime; bool isprime[MAX]; void sieve(){ for(int i=3; i<MAX; i+=2){ isprime[i]=1; } isprime[2]=1; prime.push_back(2); for(int i=3; i<MAX; i++){ if(isprime[i]){ prime.push_back(i); for(int j=2*i; j<MAX; j+=i){ isprime[j]=0; } } } return; } int main() { int n; cin>>n; sieve(); int dp[20001][92]; int sum[92]; sum[0]=0; for(int i=1; i<92; i++){ sum[i]=sum[i-1]+prime[i-1]; } for(int i=2; i<=n; i++) fill(dp[i], dp[i]+92, INF); for(auto x:prime) dp[x][1]=x; for(int i=1; i<91; i++){ for(int j=sum[i]; j<=n; j++){ if(dp[j][i]==INF) continue; int k=upper_bound(prime.begin(), prime.end(), dp[j][i])-prime.begin(); if(k==prime.size()) continue; for(int l=k; (l<prime.size() && j+prime[l]<=n); l++){ dp[j+prime[l]][i+1]=min(dp[j+prime[l]][i+1], prime[l]); } } } for(int i=91; i>=1; i--){ if(dp[n][i]<INF){ cout<<i<<endl; return 0; } } cout<<-1<<endl; return 0; }