結果

問題 No.7 プライムナンバーゲーム
ユーザー ser
提出日時 2017-08-19 17:31:00
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 22 ms / 5,000 ms
コード長 751 bytes
コンパイル時間 412 ms
コンパイル使用メモリ 58,048 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-10-01 16:06:33
合計ジャッジ時間 1,044 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 17
権限があれば一括ダウンロードができます
コンパイルメッセージ
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