結果

問題 No.7 プライムナンバーゲーム
ユーザー kuisiba
提出日時 2016-04-19 14:46:27
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 11 ms / 5,000 ms
コード長 1,249 bytes
コンパイル時間 477 ms
コンパイル使用メモリ 62,800 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-10-01 15:42:46
合計ジャッジ時間 1,130 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

using namespace std;

namespace {
    vector<int> primeList;
    vector<int> worl;
}

//nまでの素数の表を生成
vector<int> furui(int n) {
    vector<int> primeList;
    vector<int> allNum(n, 1); //素数なら1、違うなら0
    allNum.at(0) = 0;
    allNum.at(1) = 0;
    for (int i = 2; i * i <= n; ++i) {
        if(allNum.at(i) == 1) {
            for (int j = i + i; j < n; j += i) {
                allNum.at(j) = 0;
            }
        }
    }
    primeList.push_back(2);
    for (int i = 3; i < n; i +=2) {
        if(allNum.at(i)) {
            primeList.push_back(i);
        }
    }
    return primeList;
}

//nのとき、Win(1) or Lose(0)を返す
int func(int n) {
    if(worl.at(n) != -1) return worl.at(n);
    for (auto i : primeList) {
        if ((n - i) < 2) break;
        if (func(n - i) == 0) {
            worl.at(n) = 1;
            return worl.at(n);
        }
    }
    worl.at(n) = 0;
    return worl.at(n);
}

int main() {

    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    int n;
    cin >> n;
    primeList = furui(n);
    for (int i = 0; i <= n; ++i) {
        worl.push_back(-1);
    }
    cout << (func(n) ? "Win" : "Lose") << endl;
    return 0;
}
0