結果

問題 No.7 プライムナンバーゲーム
ユーザー commycommy
提出日時 2018-08-10 17:16:33
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 98 ms / 5,000 ms
コード長 1,702 bytes
コンパイル時間 829 ms
コンパイル使用メモリ 83,300 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-09 04:30:59
合計ジャッジ時間 2,272 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,820 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 98 ms
6,944 KB
testcase_03 AC 8 ms
6,944 KB
testcase_04 AC 3 ms
6,948 KB
testcase_05 AC 3 ms
6,944 KB
testcase_06 AC 29 ms
6,948 KB
testcase_07 AC 20 ms
6,948 KB
testcase_08 AC 10 ms
6,948 KB
testcase_09 AC 43 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 21 ms
6,948 KB
testcase_12 AC 70 ms
6,944 KB
testcase_13 AC 75 ms
6,948 KB
testcase_14 AC 96 ms
6,944 KB
testcase_15 AC 94 ms
6,948 KB
testcase_16 AC 85 ms
6,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <functional>

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
#define dump(val) cerr << __LINE__ << ":\t" << #val << " = " << (val) << endl

using namespace std;

typedef long long int lli;

template<typename T>
vector<T> make_v(size_t a, T b) {
    return vector<T>(a, b);
}

template<typename... Ts>
auto make_v(size_t a, Ts... ts) {
    return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}

int main() {
    int N;
    cin >> N;
    vector<bool> isPrime(N + 1, true);
    isPrime[0] = isPrime[1] = false;
    vector<int> Prime;
    REP(i, 0, N + 1) {
        if (isPrime[i]) {
            Prime.push_back(i);
            for (int j = i * 2; j < N + 1; j += i) {
                isPrime[j] = false;
            }
        }
    }
    auto dp = make_v(N + 1, 2, -1);
    function<int(int, int)> rec = [&](int n, int turn) -> int {
        if (dp[n][turn] != -1) {
            return dp[n][turn];
        }
        if (n == 0 || n == 1) {
            return (turn == 0);
        }
        bool res;
        if (turn == 0) {
            res = false;
            REP(i, 0, Prime.size()) {
                if (Prime[i] > n) {
                    break;
                }
                res |= rec(n - Prime[i], !turn);
            }
        } else {
            res = false;
            REP(i, 0, Prime.size()) {
                if (Prime[i] > n) {
                    break;
                }
                res |= (!rec(n - Prime[i], !turn));
            }
            res = !res;
        }
        return dp[n][turn] = res;
    };
    cout << (rec(N, 0) ? "Win" : "Lose") << endl;
    return 0;
}
0