結果

問題 No.7 プライムナンバーゲーム
ユーザー あおこうあおこう
提出日時 2023-07-12 11:44:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 1,518 bytes
コンパイル時間 3,991 ms
コンパイル使用メモリ 202,100 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-12 01:54:16
合計ジャッジ時間 3,695 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

#define rep(i, r) for(int i = 0; i < (r); ++i)
#define reps(i, s, r) for(int i = (s); i < (r); ++i)
#define fore(i, m2) for(auto &i : m2)
#define vi vector<int>
#define vl vector<ll>
#define pl pair<ll, ll>
#define all(i) (i).begin(), (i).end()
#define fs first
#define sc second

template <class T> bool chmin(T &i, T b) {
    if(i > b) {
        i = b;
        return true;
    }
    return false;
}
template <class T> bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

const ll INF = LONG_LONG_MAX / 3;
const ll MOD = 1'000'000'007;
const ll MAX = 1e4 + 5;

vl getPrmAry(ll mx) {

    int i, j, k;
    vl p;
    p.push_back(2);

    for(i = 3; i <= mx; i += 2) {
        k = 0;
        for(j = 3; j <= sqrt(i); j += 2) {
            if(i % j == 0) {
                k = 1;
                break;
            }
        }
        if(k == 0) {

            p.push_back(i);
        }
    }
    return p;
}

int main() {

    ll n;
    cin >> n;

    ll dp[MAX];
    dp[0] = dp[1] = true;
    vl p = getPrmAry(n);

    reps(i, 2, n + 1) {

        ll d = lower_bound(all(p), i) - p.begin();
        if(p[d] == i)
            d++;

        ll win = 0;
        rep(j, d) {
            if(dp[i - p[j]] == 0) {
                win = 1;
                break;
            }
        }
        dp[i] = win;
    }

    if(dp[n])
        puts("Win");
    else
        puts("Lose");
}
0