結果

問題 No.7 プライムナンバーゲーム
ユーザー keikei
提出日時 2016-08-11 23:09:51
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 41 ms / 5,000 ms
コード長 911 bytes
コンパイル時間 525 ms
コンパイル使用メモリ 60,540 KB
実行使用メモリ 6,696 KB
最終ジャッジ日時 2024-04-09 04:01:45
合計ジャッジ時間 1,417 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,564 KB
testcase_01 AC 1 ms
6,692 KB
testcase_02 AC 41 ms
6,688 KB
testcase_03 AC 3 ms
6,692 KB
testcase_04 AC 2 ms
6,692 KB
testcase_05 AC 2 ms
6,692 KB
testcase_06 AC 11 ms
6,692 KB
testcase_07 AC 8 ms
6,688 KB
testcase_08 AC 4 ms
6,696 KB
testcase_09 AC 17 ms
6,692 KB
testcase_10 AC 2 ms
6,696 KB
testcase_11 AC 8 ms
6,692 KB
testcase_12 AC 29 ms
6,692 KB
testcase_13 AC 30 ms
6,688 KB
testcase_14 AC 41 ms
6,692 KB
testcase_15 AC 38 ms
6,692 KB
testcase_16 AC 36 ms
6,696 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cmath>
using namespace std;

/*dp[i] : 値iの時、自分が勝つ⇒true/ 相手が勝つ⇒false*/
bool dp[10001];
/*素数表*/
bool prime_memo[10001] = { false };
/*素数表の初期化*/
void prime_init(int num) {
	prime_memo[2] = true;
	for (int i = 3; i <= num; i += 2) {
		int k = 0;
		for (int j = 3; j <= sqrt(i); j += 2) {
			if (i%j == 0) {
				k = 1;
				break;
			}
		}
		if (k == 0) {
			prime_memo[i] = true;
		}
	}
}
/*勝敗計算*/
bool calc(int num) {
	for (int i = 2; i <= num;i++) {
		if (prime_memo[i] == true) {
			if (num - i >= 0) {
				if (dp[num - i] == false) {
					return true;
				}
			}
		}
	}
	return false;
}

int main() {
	int num;
	cin >> num;
	prime_init(num);
	dp[0] = true; dp[1] = true;
	for (int i = 2; i <= num; i++) {
		dp[i] = calc(i);
	}
	if (dp[num] == true) { cout << "Win" << endl; }
	else { cout << "Lose" << endl; }
	return 0;
}
0