結果

問題 No.458 異なる素数の和
ユーザー jjbnmhkjjbnmhk
提出日時 2020-04-29 14:46:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 280 ms / 2,000 ms
コード長 2,006 bytes
コンパイル時間 1,355 ms
コンパイル使用メモリ 168,864 KB
実行使用メモリ 358,576 KB
最終ジャッジ日時 2023-08-19 02:02:20
合計ジャッジ時間 4,327 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 52 ms
104,420 KB
testcase_02 AC 65 ms
135,776 KB
testcase_03 AC 12 ms
25,744 KB
testcase_04 AC 12 ms
32,340 KB
testcase_05 AC 223 ms
298,772 KB
testcase_06 AC 62 ms
128,148 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 214 ms
301,408 KB
testcase_09 AC 5 ms
10,028 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 280 ms
358,576 KB
testcase_12 AC 1 ms
4,384 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 8 ms
16,008 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 60 ms
121,868 KB
testcase_28 AC 260 ms
350,576 KB
testcase_29 AC 3 ms
6,624 KB
testcase_30 AC 35 ms
76,216 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
#include <algorithm> 
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define FORR(i,a,b)for(ll i=(a);i<=(b);i++)
#define repR(i,n) for(ll i=n;i>=0;i--)
#define all(v)(v).begin(),(v).end()
#define rall(v)(v).rbegin(),(v).rend()
#define F first
#define S second
#define pb push_back
#define pu push
#define COUT(x) cout<<(x)<<endl
#define PQ priority_queue<ll>
#define PQR priority_queue<ll,vector<ll>,greater<ll>>
#define YES(n) cout << ((n) ? "YES" : "NO"  ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl
#define mp make_pair
#define maxs(x,y) (x = max(x,y))
#define mins(x,y) (x = min(x,y))
#define sz(x) (ll)(x).size()
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll MOD = 1000000007LL;
const ll INF = 1LL << 60;
using vll = vector<ll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvll = vector<vll>;
using vstr = vector<string>;
using pll = pair<ll, ll>;
using vc = vector<char>;
using vvc = vector<vc>;
template<class T> inline bool chmax(T& a, T b) { 
 if (a < b) { a = b; return true; } return false; 
}
template<class T> inline bool chmin(T& a, T b) {
 if (a > b) { a = b; return true; } return false; 
}
ll dx[4]={0,1,0,-1};
ll dy[4]={1,0,-1,0};
int main(){
  ll n;
  cin>>n;
  vll s(0);
  vll arr(n);
  for(ll i = 2; i < n; i++){
		arr[i] = 1;
	}
	for(ll i = 2; i < sqrt(n); i++){
		if(arr[i]){
			for(int j = 0; i * (j + 2) < n; j++){
				arr[i *(j + 2)] = 0;
			}
		}
	}

	for(ll i = 2; i < n; i++){
		if(arr[i]){
		 s.pb(i);
		}
	}
  ll dp[sz(s)+10][n+1];
  memset(dp,-1,sizeof(dp));
  dp[0][0]=0;
  rep(i,sz(s))rep(j,n+1){
    if(dp[i][j]>=0) dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
    if(dp[i][j]>=0&&j+s[i]<=n) dp[i+1][j+s[i]]=max(dp[i+1][j+s[i]],dp[i][j]+1);
  }
  if(dp[sz(s)][n]==-1&&arr[n]){
    COUT(1);
  }
  else COUT(dp[sz(s)][n]);
}
0