結果

問題 No.7 プライムナンバーゲーム
ユーザー troro_kelptroro_kelp
提出日時 2018-12-30 17:06:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,938 bytes
コンパイル時間 864 ms
コンパイル使用メモリ 97,432 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-09 04:34:19
合計ジャッジ時間 1,674 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <cmath>
#include <stack>
#include <algorithm>
#include <iomanip>
#include <map>
#include <queue>
#include <functional>
#include <numeric>
using ll = long long;
using namespace std;

const ll MOD = 1e9 + 7;
vector<int> prime;

bool sieve[200010];

int key[100010];
int Hash[55];

int visited[2][10010]; //0-先行 1-後行 現在の値

bool dfs(int T, int N)
{
    if (N == 0 || N == 1)
    {
        return T == 0 ? 1 : 0;
    }
    if (visited[T][N] >= 0)
    {
        return visited[T][N];
    }

    if (T == 0)
    {
        for (auto &x : prime)
        {
            if (N - x >= 0)
            {
                if (dfs(1, N - x))
                {
                    visited[T][N] = 1;
                    return 1;
                }
            }
        }
        visited[T][N] = 0;
        return 0;
    }
    else
    {
        for (auto &x : prime)
        {
            if (N - x >= 0)
            {
                if (!dfs(0, N - x))
                {
                    visited[T][N] = 0;
                    return 0;
                }
            }
        }
        visited[T][N] = 1;
        return 1;
    }
}
int main(void)
{

    int N;
    cin >> N;

    for (int i = 0; i < 2; ++i)
    {
        for (int j = 0; j < 10010; ++j)
        {
            visited[i][j] = -1;
        }
    }
    for (int i = 0; i < 10010; ++i)
        sieve[i] = true;

    sieve[0] = sieve[1] = false;

    for (int i = 2; i * i < 10010; ++i)
    {
        if (sieve[i])
        {
            for (int j = 2; i * j < 10010; ++j)
            {
                sieve[i * j] = false;
            }
        }
    }

    for (int i = 0; i <= N; ++i)
        if (sieve[i])
            prime.emplace_back(i);
    reverse(prime.begin(), prime.end());

    if (dfs(0, N))
        cout << "Win" << endl;
    else
        cout << "Lose" << endl;
    return 0;
}
0