結果

問題 No.7 プライムナンバーゲーム
ユーザー purple_jwlpurple_jwl
提出日時 2015-03-05 00:22:23
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 18 ms / 5,000 ms
コード長 1,144 bytes
コンパイル時間 1,439 ms
コンパイル使用メモリ 161,808 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-09 03:43:35
合計ジャッジ時間 2,447 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

#define REP(i, x, n) for(int i = x; i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define F first
#define S second
#define mp make_pair

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

const int MAX = 10000;

vector<int> prime;
bool isPrime[MAX + 1];

void sieve(int N){
  prime.clear();

  for(int i = 0; i <= N; i++) {
    isPrime[i] = true;
  }
  
  isPrime[0] = isPrime[1] = false;

  for(int i = 2; i <= N; i++) {
    if(!isPrime[i]) continue;
    prime.push_back(i);
    for(int j = i + i; j <= N; j += i) {
      isPrime[j] = false;
    }
  }
}

int N;
int memo[MAX + 1];

bool rec(int n) {
  if(n == 0 || n == 1) return true;

  if(memo[n] != -1) return memo[n];

  bool res = false;
  
  rep(i, prime.size()) {
    if(n < prime[i]) break;
    res |= !rec(n - prime[i]);
  }

  return memo[n] = res;
}

int main() {
  // ios_base::sync_with_stdio(false);
  cin >> N;
  sieve(N);
  memset(memo, -1, sizeof(memo));
  cout << (rec(N) ? "Win" : "Lose") << endl;
  return 0;
}
0