結果

問題 No.7 プライムナンバーゲーム
ユーザー OnjuOnju
提出日時 2015-02-08 22:49:59
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 43 ms / 5,000 ms
コード長 941 bytes
コンパイル時間 473 ms
コンパイル使用メモリ 59,432 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-09 03:42:29
合計ジャッジ時間 1,460 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 1 ms
6,948 KB
testcase_02 AC 43 ms
6,944 KB
testcase_03 AC 4 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 12 ms
6,948 KB
testcase_07 AC 8 ms
6,948 KB
testcase_08 AC 4 ms
6,948 KB
testcase_09 AC 18 ms
6,944 KB
testcase_10 AC 2 ms
6,948 KB
testcase_11 AC 8 ms
6,948 KB
testcase_12 AC 29 ms
6,948 KB
testcase_13 AC 32 ms
6,944 KB
testcase_14 AC 43 ms
6,948 KB
testcase_15 AC 40 ms
6,948 KB
testcase_16 AC 37 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cmath>
#include <cstring>
#define N_MAX 10000
using namespace std;

enum eANS
{
	ANS_NONE,
	ANS_WIN,
	ANS_LOSE
};

bool* calcPrime()
{
	bool* list = new bool[N_MAX + 1];
	int sqrtnmax = sqrt(N_MAX);

	for (int i = 2; i <= N_MAX; ++i)
		list[i] = true;

	for (int i = 2; i <= sqrtnmax; ++i)
	{
		if (list[i])
		{
			int jmax = N_MAX / i;
			for (int j = i; j <= jmax; ++j)
				list[i*j] = false;
		}
	}

	return list;
}

int main()
{
	bool* prime = calcPrime();
	eANS ans[N_MAX + 1];
	int N;

	cin >> N;

	ans[0] = ans[1] = ANS_WIN;
	for (int i = 2; i <= N_MAX; ++i)
		ans[i] = ANS_NONE;

	for (int i = 2; i <= N; ++i)
	{
		for (int j = 2; j < i; ++j)
		{
			if (prime[j] && (ans[i - j] == ANS_LOSE))
			{
				ans[i] = ANS_WIN;
				break;
			}
		}

		if (ans[i] != ANS_WIN)
			ans[i] = ANS_LOSE;
	}

	if (ans[N] == ANS_WIN)
		cout << "Win" << endl;
	else
		cout << "Lose" << endl;

	delete[] prime;

	return 0;
}
0