結果

問題 No.7 プライムナンバーゲーム
ユーザー nemunemunemunemu
提出日時 2024-01-13 16:26:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 22 ms / 5,000 ms
コード長 962 bytes
コンパイル時間 806 ms
コンパイル使用メモリ 73,020 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-01-13 16:26:06
合計ジャッジ時間 2,248 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

// No.7 プライムナンバーゲーム
#include <iostream>
#include <vector>
using namespace std;

int main() {
    int N;
    cin >> N;
    vector<bool> isPrime(N + 1, true);
    isPrime[0] = isPrime[1] = false;
    for (int i = 2; i <= N; ++i) {
        if (!isPrime[i]) continue;
        for (int j = i * 2; j <= N; j += i) {
            isPrime[j] = false;
        }
    }
    vector<int> primes;
    for (int i = 1; i <= N; ++i) {
        if (isPrime[i]) primes.push_back(i);
    }
    vector<bool> memo(N + 1);
    vector<bool> used(N + 1, false);
    auto dfs = [&](auto dfs, int n) -> bool {
        if (n == 0 || n == 1) return true;
        if (used[n]) return memo[n];
        used[n] = true;
        bool res = false;
        for (auto p: primes) {
            if (p > n) break;
            res = res || !dfs(dfs, n - p);
        }
        return memo[n] = res;
    };
    if (dfs(dfs, N)) cout << "Win" << endl;
    else cout << "Lose" << endl;
}
0