結果

問題 No.7 プライムナンバーゲーム
ユーザー t-0gat-0ga
提出日時 2019-07-11 13:44:54
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 384 ms / 5,000 ms
コード長 927 bytes
コンパイル時間 556 ms
コンパイル使用メモリ 67,860 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-07-24 21:21:17
合計ジャッジ時間 3,711 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 384 ms
4,380 KB
testcase_03 AC 11 ms
4,380 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 4 ms
4,380 KB
testcase_06 AC 79 ms
4,380 KB
testcase_07 AC 47 ms
4,376 KB
testcase_08 AC 17 ms
4,376 KB
testcase_09 AC 135 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 48 ms
4,376 KB
testcase_12 AC 251 ms
4,380 KB
testcase_13 AC 270 ms
4,376 KB
testcase_14 AC 379 ms
4,380 KB
testcase_15 AC 356 ms
4,380 KB
testcase_16 AC 324 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

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