結果

問題 No.7 プライムナンバーゲーム
ユーザー nenuonnenuon
提出日時 2017-03-01 18:43:53
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 39 ms / 5,000 ms
コード長 1,320 bytes
コンパイル時間 587 ms
コンパイル使用メモリ 76,976 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-07-24 20:47:31
合計ジャッジ時間 1,740 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
using namespace std;

#define ll         long long
#define PI         acos(-1.0)
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)

//方針


vector<int> vp;

//素数作成
void MakePrimeNumber(int n){
    vp.clear();
    int tmp[n+1];
    FOR(i, 0, n+1) tmp[i] = 1;
    FOR(i, 2, n+1){
        if(tmp[i]==0) continue;
        vp.push_back(i);
        int j = i;
        while(j <= n){
            tmp[j] = 0;
            j += i;
        }
    }
}

//メモ化再帰
int dp[10001][2];
int dfs(int n, int me){
    if(dp[n][me]!=-1) return dp[n][me];
    if(n==1 || n==0){
        if(me==1){
            cout << 1 << endl;
            return 1;
        }
        else return 0;
    }

    int j = 0;
    int ret = 0;
    while(1){
        if(n-vp[j]>=2) ret = ret || !dfs(n-vp[j], (me+1)%2);
        else break;
        j++;
        if(j==vp.size()) break;
    }
    return dp[n][me] = ret;
}

int main(){
  int N;
  cin >> N;
  MakePrimeNumber(N);
  //配列初期化
  FOR(i, 0, 10001) FOR(j, 0, 2) dp[i][j] = -1;
  cout << (dfs(N, 1)==1 ? "Win" : "Lose") << endl;
}
0