結果

問題 No.7 プライムナンバーゲーム
ユーザー mh72012246mh72012246
提出日時 2016-10-24 15:47:29
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 1,085 bytes
コンパイル時間 775 ms
コンパイル使用メモリ 73,848 KB
実行使用メモリ 6,696 KB
最終ジャッジ日時 2024-04-09 04:03:26
合計ジャッジ時間 1,396 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,564 KB
testcase_01 AC 1 ms
6,692 KB
testcase_02 AC 5 ms
6,688 KB
testcase_03 AC 2 ms
6,696 KB
testcase_04 AC 1 ms
6,688 KB
testcase_05 AC 1 ms
6,688 KB
testcase_06 AC 2 ms
6,688 KB
testcase_07 AC 2 ms
6,692 KB
testcase_08 AC 1 ms
6,692 KB
testcase_09 AC 3 ms
6,692 KB
testcase_10 AC 1 ms
6,692 KB
testcase_11 AC 2 ms
6,696 KB
testcase_12 AC 4 ms
6,692 KB
testcase_13 AC 4 ms
6,692 KB
testcase_14 AC 5 ms
6,688 KB
testcase_15 AC 4 ms
6,692 KB
testcase_16 AC 4 ms
6,688 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cmath>
#include <vector>
#include <queue>
#include <sstream>   // istringstream
#include <algorithm> // sort
#include <utility>   // pair
#include <cfloat>    // DBL_MAX

typedef long long ll;
using namespace std;

const int maxN = 10000;
bool ps[maxN-1];

int main(){
  int N;
  cin >> N;

  // is able to win
  vector<bool> able (max(N+1,4), false);
  able[0] = true; able[1] = true;

  // prime (<= N)
  vector<int> primes;
  for(int i=0; i<N-1; i++){ ps[i] = true; }
  for(int i=2; i<sqrt(N)+1; i++){
    if(ps[i-2]){
      for(int j=i*i; j<=N; j+=i){
        ps[j-2] = false;
      }
    }
  }
  for(int i=2; i<=N; i++){
    if(ps[i-2]){
      primes.push_back(i);
    }
  }

  //// main
  for(int i=4; i<N+1; i++){
    for(uint j=0; j<primes.size(); j++){
      if(i <= primes[j]){ // its enough
        break;
      }
      if(!able[i-primes[j]]){ // choice which opp can't win
        able[i] = true;
        break;
      }
    }
  }

  //// main
  if(able[N]){
    cout << "Win" << endl;
  } else {
    cout << "Lose" << endl;
  }

  return 0;
}
0