結果

問題 No.7 プライムナンバーゲーム
ユーザー vintersn0wvintersn0w
提出日時 2018-05-13 00:57:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 25 ms / 5,000 ms
コード長 1,103 bytes
コンパイル時間 717 ms
コンパイル使用メモリ 77,836 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-09 04:28:21
合計ジャッジ時間 1,536 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,820 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 25 ms
6,944 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 8 ms
6,944 KB
testcase_07 AC 6 ms
6,944 KB
testcase_08 AC 4 ms
6,944 KB
testcase_09 AC 12 ms
6,948 KB
testcase_10 AC 1 ms
6,948 KB
testcase_11 AC 6 ms
6,948 KB
testcase_12 AC 18 ms
6,944 KB
testcase_13 AC 20 ms
6,948 KB
testcase_14 AC 25 ms
6,944 KB
testcase_15 AC 24 ms
6,948 KB
testcase_16 AC 22 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <stdio.h>
#include <cmath>
#include <vector>
#include <set>

using namespace std;
typedef uint uint32_t;

bool isPrime(int n) {
    if (n <= 1) return false;
    if (n == 2) return true;
    if (n % 2 == 0) return false;

    for (int i = 3; i <= sqrt(n); i+=2) {
        if (n % i == 0) return false;
    }

    return true;
}

int prime[10001];
int ps = 0;

bool check(int N) {
    int dp[10001] = {};
    // vector<int> prime;

    // if (N == 2 || N == 3) return true;

    dp[0] = dp[1] = 2;  // 1は負け, 2なら勝ち
    dp[2] = dp[3] = 1;

    for (int i = 4; i <= N; i++) {
        dp[i] = 1;
        for (int j = 0; j < ps; j++) {
            int p = prime[j];
            if (i - p < 2) break;
            if (dp[i - p] == 1) {
                dp[i] = 2;
            }
        }
    }

    return dp[N] == 2;
}

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

    for (int i = 2; i < N; i++) {
        if (isPrime(i))
            prime[ps++] = i;
    }

    if (check(N)) cout << "Win" << endl;
    else cout << "Lose" << endl;
}
0