結果

問題 No.7 プライムナンバーゲーム
ユーザー serser
提出日時 2017-08-19 17:31:00
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 24 ms / 5,000 ms
コード長 751 bytes
コンパイル時間 472 ms
コンパイル使用メモリ 57,132 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-09 04:22:40
合計ジャッジ時間 1,361 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,820 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 24 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,948 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 7 ms
6,944 KB
testcase_07 AC 5 ms
6,944 KB
testcase_08 AC 3 ms
6,944 KB
testcase_09 AC 11 ms
6,948 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 5 ms
6,944 KB
testcase_12 AC 17 ms
6,948 KB
testcase_13 AC 17 ms
6,944 KB
testcase_14 AC 24 ms
6,948 KB
testcase_15 AC 23 ms
6,948 KB
testcase_16 AC 21 ms
6,948 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:17:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   17 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
main.cpp:22:14: warning: iteration 4998 invokes undefined behavior [-Waggressive-loop-optimizations]
   22 |         np[i]=1;
      |         ~~~~~^~
main.cpp:21:19: note: within this loop
   21 |     for(int i=4; i<=10000; i+=2) {
      |                  ~^~~~~~~

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;

int prev_prime(int *np, int s) {
    while(s>1) {
        if(np[s]==0) return s;
        s--;
    }
    return -1;
}

int main() {
    int n;
    scanf("%d", &n);


    int np[10000] = {1, 1};
    for(int i=4; i<=10000; i+=2) {
        np[i]=1;
    }
    for(int i=3; i<=10000; i+=2) {
        for(int j=i*2; j<=10000; j+=i) {
            np[j]=1;
        }
    }


    int dp[10001] = {0, 0, 0, 0};

    for(int i=4; i<=n; i++) {
        int p = i;
        while((p = prev_prime(np, p))>0){
            if(i-p>1 && dp[i-p]==0) {
                dp[i] = 1;
                break;
            }
            p--;
        }
    }

    printf(dp[n]==1 ? "Win" : "Lose");
}
0