結果

問題 No.7 プライムナンバーゲーム
ユーザー tottoripapertottoripaper
提出日時 2014-11-16 02:44:31
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 9 ms / 5,000 ms
コード長 992 bytes
コンパイル時間 251 ms
コンパイル使用メモリ 35,136 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-09 03:40:29
合計ジャッジ時間 1,033 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,948 KB
testcase_02 AC 8 ms
6,944 KB
testcase_03 AC 1 ms
6,948 KB
testcase_04 AC 1 ms
6,948 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 3 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 5 ms
6,944 KB
testcase_10 AC 1 ms
6,948 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 6 ms
6,948 KB
testcase_13 AC 7 ms
6,944 KB
testcase_14 AC 9 ms
6,948 KB
testcase_15 AC 8 ms
6,944 KB
testcase_16 AC 8 ms
6,944 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:38:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   38 |     scanf("%d", &N);
      |     ~~~~~^~~~~~~~~~

ソースコード

diff #

#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