結果

問題 No.7 プライムナンバーゲーム
ユーザー keikei
提出日時 2018-03-29 20:45:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 21 ms / 5,000 ms
コード長 3,099 bytes
コンパイル時間 1,750 ms
コンパイル使用メモリ 166,292 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-09 04:26:51
合計ジャッジ時間 2,671 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }

/*
 <url:https://yukicoder.me/problems/no/7>
 問題文============================================================
 あなたと素数を習ったばかりのEveは、素数のゲームを思いついた。
 
 ゲームの内容は以下のとおりです。
 ・まず初めに、先攻のプレイヤーに2以上の自然数Nが与えられます。
 ・その番のプレイヤーはNに対して、「N以下(Nも含む)の素数」のどれかで減算する、
 その数をN′とすると、N′が0または1になってしまったら、そのプレイヤーの負けである。
 ・その後N′を新たなNとし、相手にその数を渡し、以上を繰り返します。
 
 まずあなたが先攻となりゲームを始めます。
 この時、どちらも負けないように動くと考える。自然数Nが与えられた時、
 あなたが勝つことが出来る場合Win、それ以外はLoseを返してください。
 
 =================================================================
 解説=============================================================
 ================================================================
 */

const ll MAX_PRIME = 10005;
vector<int> primes;
vector<int> is_prime(MAX_PRIME + 1,true);
void init_primes(){
    is_prime[0] = is_prime[1] = false;
    for(int i = 2; i <= MAX_PRIME;i++){
        if(is_prime[i]){
            primes.push_back(i);
            for(int j = i*2; j <= MAX_PRIME; j+=i) is_prime[j] = false;
        }
    }
}


int dp[MAX_PRIME][2];
ll N;
bool rec(ll n,bool turn){
    int& res = dp[n][turn];
    if(res != -1) return res;
    for(int i = 0; i < primes.size();i++){
        if(primes[i] > n) break;
        if(!rec(n-primes[i],!turn)) return res = true;
    }
    return res = false;
}
string solve(){
    cin >> N;
    fill(*dp,*dp+MAX_PRIME*2,-1);
    dp[0][0] = dp[1][0] = dp[0][1] = dp[1][1] = true;
    return rec(N,false)?"Win":"Lose";
}
int main(void) {
    cin.tie(0); ios::sync_with_stdio(false);
    init_primes();
    cout << solve() << endl;
    
    //    for(int i = 0; i <= 5;i++){
    //        cout << dp[i][0] << " " << dp[i][1] << endl;
    //    }
    return 0;
}
0