結果

問題 No.7 プライムナンバーゲーム
ユーザー H3PO4
提出日時 2023-02-12 12:57:52
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 26 ms / 5,000 ms
コード長 937 bytes
コンパイル時間 944 ms
コンパイル使用メモリ 75,884 KB
最終ジャッジ日時 2025-02-10 14:44:14
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

std::vector<int> primes(int n) {
    std::vector<bool> is_prime(n + 1, true);
    is_prime.at(0) = false;
    is_prime.at(1) = false;
    std::vector<int> res;
    for (int i = 2; i < n + 1; i++) {
        if (is_prime.at(i)) {
            res.push_back(i);
            for (int j = i * 2; j < n + 1; j += i) {
                is_prime.at(j) = false;
            }
        }
    }
    return res;
}

bool solve(int N) {
    auto P = primes(N);
    std::vector<bool> dp(N + 1, false);
    dp.at(0) = true;
    dp.at(1) = true;
    for (int n = 2; n < N + 1; n++) {
        for (auto &p: P) {
            if (n - p < 0) {
                break;
            }
            if (!dp.at(n - p)) {
                dp.at(n) = true;
            }
        }
    }
    return dp.at(N);
}

int main() {
    int N;
    std::cin >> N;
    auto ans = solve(N) ? "Win" : "Lose";
    std::cout << ans << std::endl;
}
0