結果

問題 No.7 プライムナンバーゲーム
ユーザー tadanoningentadanoningen
提出日時 2020-09-23 07:12:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 412 ms / 5,000 ms
コード長 933 bytes
コンパイル時間 737 ms
コンパイル使用メモリ 76,856 KB
実行使用メモリ 23,168 KB
最終ジャッジ日時 2024-04-09 04:59:42
合計ジャッジ時間 4,157 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 412 ms
23,168 KB
testcase_03 AC 22 ms
6,944 KB
testcase_04 AC 6 ms
6,948 KB
testcase_05 AC 7 ms
6,948 KB
testcase_06 AC 106 ms
9,472 KB
testcase_07 AC 69 ms
7,424 KB
testcase_08 AC 30 ms
6,944 KB
testcase_09 AC 166 ms
12,288 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 70 ms
7,424 KB
testcase_12 AC 284 ms
16,640 KB
testcase_13 AC 303 ms
17,664 KB
testcase_14 AC 412 ms
23,168 KB
testcase_15 AC 389 ms
22,016 KB
testcase_16 AC 354 ms
20,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#define ll long long

using namespace std;


vector<int> prime(int n){
    vector<bool> is_prime(n+1,true);
    vector<int> p;
    for(int i=2;i <= n;i++){
        if(is_prime[i]){
            for(int j=2*i;j <= n;j+=i){
                is_prime[j] = false;
            }
            p.push_back(i);
        }
    }
    return p;
}

vector<bool> dp(10010,false);
vector<bool> calced(10010,false);

bool dfs(int n){


    if(n == 0 || n == 1){
        return dp[n] = true;
    } 
    if(n == 2 || n == 3) {
        return dp[n] = false;
    }
    if(calced[n]) return dp[n];

    calced[n] = true;
    
    bool res = false;
    for(auto &x : prime(n)){
        if(!dfs(n-x)){
            return dp[n] = true;
        }
    }
    return dp[n] = false;
}

int main(){
    int n;
    cin >> n;
    if(dfs(n)) cout << "Win" << endl;
    else cout << "Lose" << endl;
    return 0;
}
0