結果

問題 No.458 異なる素数の和
ユーザー xxxasdfghjk
提出日時 2018-08-17 17:26:17
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 1,658 bytes
コンパイル時間 765 ms
コンパイル使用メモリ 76,156 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-10-09 13:18:22
合計ジャッジ時間 2,783 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<cstdio>
#include<queue>
#include<utility>
#include<cstring>
#include<stack>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<map>
#define MAX_N 100001
#define INF_INT 2147483647
#define INF_LL 9223372036854775807
#define REP(i,n) for(int i=0;i<(int)(n);i++)
void init(int n);
int find(int n);
void unite(int x,int y);
bool same(int x, int y);
int dx[4] = {1,0,0,-1};
int dy[4] = {0,1,-1,0};

using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;
bool cmp_P(const P &a,const P &b){
  return a.second < b.second;
}

vector<int> primes(0);
void setprime(){
  bool prime[20001];
  fill(prime,prime+20001,true);
  for(int i=2;i<=20000;i++){
    if(prime[i]){
      primes.push_back(i);
      for(int j=i+i;j<=20000;j+=i)
        prime[j] = false;
    }
  }
}

int main()
{
  setprime();
  int dp[20001];
  fill(dp,dp+20001,-1);
  dp[0] = 0;
  for(int j=0;j<primes.size();j++)
    for(int i=20001;i>=0;i--){
      if(i-primes[j] < 0)
        break;
      if((dp[i-primes[j]] != -1) && (dp[i-primes[j]] + 1 >= dp[i]))
        dp[i] = dp[i-primes[j]] + 1;
    }
  int N;
  cin >> N;
  cout << dp[N] << endl;
  return 0;
}

int par[MAX_N];
int ranks[MAX_N];

//n要素で初期化
void init(int n){
  REP(i,n){
    par[i] = i;
    ranks[i] = 0;
  }

}

//木の根を求める
int find(int x){
  if(par[x] == x){
    return x;
  }else{
    return par[x] = find(par[x]);
  }
}

void unite(int x,int y){
  x = find(x);
  y = find(y);
  if(x == y) return ;
  if(ranks[x] < ranks[y]){
    par[x] = y;
  }else{
    par[y] = x;
    if(ranks[x] == ranks[y]) ranks[x]++;
  }
}

bool same(int x, int y){
  return find(x) == find(y);
}

0