結果

問題 No.7 プライムナンバーゲーム
ユーザー wonda_t_coffeewonda_t_coffee
提出日時 2020-02-27 10:27:57
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 419 ms / 5,000 ms
コード長 1,591 bytes
コンパイル時間 1,073 ms
コンパイル使用メモリ 89,812 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-09 04:50:09
合計ジャッジ時間 4,408 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,948 KB
testcase_02 AC 419 ms
6,944 KB
testcase_03 AC 24 ms
6,948 KB
testcase_04 AC 6 ms
6,948 KB
testcase_05 AC 6 ms
6,948 KB
testcase_06 AC 117 ms
6,948 KB
testcase_07 AC 77 ms
6,944 KB
testcase_08 AC 34 ms
6,948 KB
testcase_09 AC 180 ms
6,948 KB
testcase_10 AC 2 ms
6,948 KB
testcase_11 AC 79 ms
6,948 KB
testcase_12 AC 294 ms
6,944 KB
testcase_13 AC 314 ms
6,944 KB
testcase_14 AC 414 ms
6,948 KB
testcase_15 AC 392 ms
6,944 KB
testcase_16 AC 363 ms
6,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define rep(i,n) for (int i = 0; i < (n); ++i)

using namespace std;
using ll = long long;
using P = pair<int,int>;
using namespace std;

int n;
map<int, bool> memo;
const int MAX = 10000;
bool isPrime[MAX + 1];

void sieve() {
  for (int i = 0; i <= MAX; i++) isPrime[i] = true;

  isPrime[0] = false;
  isPrime[1] = false;
  for (int i = 4; i <= n; i += 2) {
    isPrime[i] = false;
  }

  int lim = (int)sqrt(n);
  // cout << "lim = " << lim << endl;
  for (int i = 3; i <= lim; i += 2) {
    for (int j = 3; i * j <= n; j += 2) {
      isPrime[i * j] = false;
    }
  }
}

int solve(int m) {
  if (memo.find(m) != memo.end()) {
    return memo[m];
  }

  for (int i = 2; i < m; i++) {
    if (!isPrime[i]) continue;
    // cout << "m = " << m << ", i = " << i << endl;
    if (m - i > 1 && !solve(m - i)) {
      memo[m] = true;
      return true;
    }
  }

  memo[m] = false;
  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n;

  sieve();
  memo[0] = false;
  memo[1] = false;
  memo[2] = false;

  // for (int i = 0; i <= n; i++) {
  //   if (isPrime[i]) {
  //     cout << i << endl;
  //   }
  // }

  if (solve(n)) {
    cout << "Win" << endl;
  } else {
    cout << "Lose" << endl;
  }

  // for (int i = 0; i <= n; i++) {
  //   cout << "memo[" << i << "] = " << memo[i] << endl;
  // }
}
0