結果

問題 No.7 プライムナンバーゲーム
ユーザー hanorverhanorver
提出日時 2016-05-03 17:00:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 896 bytes
コンパイル時間 854 ms
コンパイル使用メモリ 73,628 KB
実行使用メモリ 34,844 KB
最終ジャッジ日時 2024-04-15 10:29:53
合計ジャッジ時間 7,316 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>

bool isPrime(int n) {
	for (int i = 2; i * i <= n; i++) {
		if (n % i == 0) return false;
	}
	return true;
}

// turn が奇数なら自分のターン,偶数なら相手のターン
bool game(std::vector<int> primes, int n, int turn) {
	// true なら勝ち、falseなら負け
	if (n == 1 || n == 0) return 1 - turn;
	for (int i = 0; i < primes.size() && primes[i] <= n; i++) {
		if (n - primes[i] > 1) {
			bool result = game(primes, n - primes[i], 1 - turn);
			if (turn == 1 && result) return true;
			if (turn == 0 && !result) return false;
		}
	}
	return 1 - turn;
}

int main() {
	int n;

	std::cin >> n;

	std::vector<int> primes;
	for (int i = 2; i <= n; i++) {
		if (isPrime(i)) primes.push_back(i);
	}

	if (game(primes, n, 1)) {
		std::cout << "Win" << std::endl;
	} else {
		std::cout << "Lose" << std::endl;
	}

	return 0;
}
0