結果

問題 No.7 プライムナンバーゲーム
ユーザー けーむけーむ
提出日時 2020-12-21 00:50:38
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 6 ms / 5,000 ms
コード長 2,299 bytes
コンパイル時間 2,576 ms
コンパイル使用メモリ 176,040 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-09 05:01:01
合計ジャッジ時間 2,973 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\n"
template<class T> void chmax(T &a, const T b){ a = max(a, b); }
template<class T> void chmin(T &a, const T b){ a = min(a, b); }
long long gcd(long long a, long long b) { return (a % b) ? gcd(b, a % b) : b; }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }

struct isPrime {
private:
    vector<bool> data;
    
public:
    isPrime() {}
    isPrime(int n) {
        data.resize(n + 1, true);
        data[0] = data[1] = false;
        for(long long i = 2; (i * i) <= n; i++) {
            if (!data[i]) continue;
            for(long long j = 2; (i * j) <= n; j++) {
                data[i * j] = false;
            }
        }
    }
    
    bool is_prime(int n) {
        return data[n];
    }
    
    vector<int> get_primes_vector() {
        vector<int> ans;
        int n = data.size();
        for(int i = 0; i < n; i++) {
            if (data[i]) ans.push_back(i);
        }
        return ans;
    }
    
    set<int> get_primes_set() {
        set<int> ans;
        int n = data.size();
        for(int i = 0; i < n; i++) {
            if (data[i]) ans.insert(i);
        }
        return ans;
    }
};

isPrime ip;
vector<int> p;
vector<bool> dp; // dp[値] = 1: 勝利, 0: 敗北, -1: 未定義

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // ifstream in("input.txt");
    // cin.rdbuf(in.rdbuf());
    ll n;
    cin >> n;
    ip = isPrime(n);
    p = ip.get_primes_vector();
    dp.resize(n + 1, -1);
    rep(i, min(n + 1, 4LL)) dp[i] = 0;
    reps(i, 4, n + 1) {
        bool f = false;
        rep(j, sz(p)) {
            ll idx = i - p[j];
            if (idx < 2) break;
            if (dp[idx] == 0) {
                f = true;
                break;
            }
        }
        dp[i] = f;
    }
    cout << (dp[n] ? "Win" : "Lose") << endl;
    return 0;
}
0