結果

問題 No.7 プライムナンバーゲーム
ユーザー t-0ga
提出日時 2019-07-11 13:44:54
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 403 ms / 5,000 ms
コード長 927 bytes
コンパイル時間 618 ms
コンパイル使用メモリ 67,180 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-10-01 16:22:30
合計ジャッジ時間 3,609 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstring>
#include <math.h> 
using namespace std;
#define MAX 10001

bool dp[MAX];
bool IsPrime(int num)
{
    if (num < 2) return false;
    else if (num == 2) return true;
    else if (num % 2 == 0) return false; // 偶数はあらかじめ除く

    double sqrtNum = sqrt((double)num);
    for (int i = 3; i <= sqrtNum; i += 2)
    {
        if (num % i == 0)
        {
            // 素数ではない
            return false;
        }
    }

    // 素数である
    return true;
}
int main()
{
	int N;
	cin >> N;
	
	if(N < 2)
	{
		cout << "Lose" << endl;
		return 0;
	}
	dp[0] = true;
	dp[1] = true;
	for(int i = 2 ; i <= N ; ++i)
	{
		for(int k = 2 ; k <= i ; ++k)
		{
			if(IsPrime(k) && !dp[i-k])
			{
				dp[i] = true;
				break;
			}
		}
	}
	if(dp[N]) cout << "Win" << endl;
	else      cout << "Lose" << endl;
    return 0;
}
0