結果

問題 No.7 プライムナンバーゲーム
ユーザー icchiposticchipost
提出日時 2021-12-18 23:13:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 145 ms / 5,000 ms
コード長 743 bytes
コンパイル時間 1,812 ms
コンパイル使用メモリ 166,916 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-09 05:06:16
合計ジャッジ時間 3,467 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,948 KB
testcase_02 AC 145 ms
6,948 KB
testcase_03 AC 8 ms
6,944 KB
testcase_04 AC 3 ms
6,944 KB
testcase_05 AC 3 ms
6,944 KB
testcase_06 AC 37 ms
6,948 KB
testcase_07 AC 24 ms
6,948 KB
testcase_08 AC 10 ms
6,944 KB
testcase_09 AC 58 ms
6,948 KB
testcase_10 AC 2 ms
6,948 KB
testcase_11 AC 25 ms
6,948 KB
testcase_12 AC 101 ms
6,944 KB
testcase_13 AC 108 ms
6,948 KB
testcase_14 AC 145 ms
6,944 KB
testcase_15 AC 136 ms
6,948 KB
testcase_16 AC 125 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using pii = pair<int, int>;

int main() {
  int n;
  cin >> n;
  
  vector<int> prime(n + 1, 1);
  prime[0] = prime[1] = 0;
  for (int i = 2; i <= n; i++) {
    if (!prime[i]) continue;
    for (int j = i * 2; j <= n; j += i) {
      prime[j] = 0;
    }
  }

  vector<int> dp(n + 1, -1);
  function<int(int)> rec = [&](int x) {
    if (dp[x] != -1) return dp[x];
    if (x <= 1) return dp[x] = 1;
    int res = 0;
    for (int i = 2; i <= x; i++) {
      if (!prime[i]) continue;
      res |= 1 - rec(x - i);
    }
    return dp[x] = res;
  };

  if (rec(n)) cout << "Win" << endl;
  else cout << "Lose" << endl;
  return 0;
}
0