結果

問題 No.7 プライムナンバーゲーム
コンテスト
ユーザー tottoripaper
提出日時 2014-11-16 02:44:31
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 7 ms / 5,000 ms
コード長 992 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 364 ms
コンパイル使用メモリ 55,124 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-04-17 17:11:01
合計ジャッジ時間 1,050 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <algorithm>
#include <cstdio>

int N;
bool isPrime[10011];
int primes[10011], p_length, memo[10001];

// rec(n): 自分にnで回ってきたとき勝てるか
bool rec(int n){
    if(n <= 1){return true;}
    if(memo[n] != -1){return memo[n];}

    int maxIndex = std::upper_bound(primes, primes+p_length, n) - primes;
    bool f = false;
    for(int i=0;i<maxIndex;i++){
        f = f || !rec(n-primes[i]);
    }
    
    return memo[n] = f;
}

int main(){
    std::fill(isPrime, isPrime+10011, true);

    primes[0] = 2;
    for(int i=4;i<=10010;i+=2){isPrime[i] = false;}
    p_length = 1;
    
    for(int i=3;i<=10010;i+=2){
        if(isPrime[i]){
            primes[p_length++] = i;
            for(int j=i*i;j<=10010;j+=i){
                isPrime[j] = false;
            }
        }
    }

    scanf("%d", &N);

    std::fill(memo, memo+10001, -1);
    if(rec(N)){
        puts("Win");
    }else{
        puts("Lose");
    }
}
0