結果

問題 No.7 プライムナンバーゲーム
ユーザー H3PO4H3PO4
提出日時 2023-02-12 12:57:52
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 24 ms / 5,000 ms
コード長 937 bytes
コンパイル時間 723 ms
コンパイル使用メモリ 77,128 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-23 04:57:32
合計ジャッジ時間 1,887 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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