結果

問題 No.7 プライムナンバーゲーム
ユーザー hanorverhanorver
提出日時 2016-05-03 17:11:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 987 bytes
コンパイル時間 802 ms
コンパイル使用メモリ 73,792 KB
実行使用メモリ 27,904 KB
最終ジャッジ日時 2024-04-15 10:30:16
合計ジャッジ時間 1,807 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 WA -
testcase_02 AC 48 ms
27,904 KB
testcase_03 AC 5 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 WA -
testcase_06 AC 16 ms
10,624 KB
testcase_07 AC 12 ms
8,192 KB
testcase_08 AC 6 ms
5,504 KB
testcase_09 WA -
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 12 ms
8,320 KB
testcase_12 AC 35 ms
20,992 KB
testcase_13 AC 38 ms
22,016 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 43 ms
24,832 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

bool r[10000];
bool rr[10000];

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) {
	if (r[n]) {
		if (turn) return rr[n];
		else return !rr[n];
	}
	// true なら勝ち、falseなら負け
	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);
			r[n] = true;
			rr[n] = result;
			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