結果

問題 No.715 集合と二人ゲーム
ユーザー startcppstartcpp
提出日時 2018-07-17 00:24:30
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 177 ms / 2,000 ms
コード長 2,382 bytes
コンパイル時間 734 ms
コンパイル使用メモリ 68,056 KB
実行使用メモリ 7,368 KB
最終ジャッジ日時 2024-05-01 20:32:37
合計ジャッジ時間 5,228 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,376 KB
testcase_01 AC 4 ms
5,504 KB
testcase_02 AC 5 ms
5,632 KB
testcase_03 AC 7 ms
5,504 KB
testcase_04 AC 6 ms
5,376 KB
testcase_05 AC 8 ms
5,504 KB
testcase_06 AC 8 ms
5,504 KB
testcase_07 AC 10 ms
5,376 KB
testcase_08 AC 10 ms
5,504 KB
testcase_09 AC 12 ms
5,504 KB
testcase_10 AC 12 ms
5,504 KB
testcase_11 AC 14 ms
5,504 KB
testcase_12 AC 14 ms
5,504 KB
testcase_13 AC 16 ms
5,376 KB
testcase_14 AC 17 ms
5,632 KB
testcase_15 AC 17 ms
5,632 KB
testcase_16 AC 19 ms
5,504 KB
testcase_17 AC 17 ms
5,504 KB
testcase_18 AC 21 ms
5,632 KB
testcase_19 AC 22 ms
5,760 KB
testcase_20 AC 23 ms
5,632 KB
testcase_21 AC 24 ms
5,632 KB
testcase_22 AC 26 ms
5,632 KB
testcase_23 AC 26 ms
5,632 KB
testcase_24 AC 28 ms
5,760 KB
testcase_25 AC 28 ms
5,632 KB
testcase_26 AC 27 ms
5,504 KB
testcase_27 AC 32 ms
5,760 KB
testcase_28 AC 33 ms
5,760 KB
testcase_29 AC 33 ms
5,632 KB
testcase_30 AC 30 ms
5,760 KB
testcase_31 AC 26 ms
5,504 KB
testcase_32 AC 23 ms
5,632 KB
testcase_33 AC 26 ms
5,760 KB
testcase_34 AC 24 ms
5,632 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 2 ms
5,504 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 3 ms
5,376 KB
testcase_39 AC 3 ms
5,376 KB
testcase_40 AC 4 ms
5,376 KB
testcase_41 AC 3 ms
5,376 KB
testcase_42 AC 3 ms
5,376 KB
testcase_43 AC 4 ms
5,376 KB
testcase_44 AC 2 ms
5,376 KB
testcase_45 AC 3 ms
5,376 KB
testcase_46 AC 3 ms
5,376 KB
testcase_47 AC 4 ms
5,376 KB
testcase_48 AC 3 ms
5,376 KB
testcase_49 AC 3 ms
5,376 KB
testcase_50 AC 177 ms
7,360 KB
testcase_51 AC 176 ms
7,368 KB
testcase_52 AC 174 ms
7,364 KB
testcase_53 AC 169 ms
7,232 KB
testcase_54 AC 172 ms
7,296 KB
testcase_55 AC 176 ms
7,296 KB
testcase_56 AC 168 ms
7,296 KB
testcase_57 AC 156 ms
7,104 KB
testcase_58 AC 175 ms
7,168 KB
testcase_59 AC 177 ms
7,236 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//実験的な解法。証明はしていない。
//とりあえず{1,2,3,…,n}のgrundy数をgrundy(n)として、grundy(1)~grundy(n)をO(n^2)で求めるところまでは典型。
//grundy数の種類が10種類と少ないことを利用して、grundy数 = iの出現位置を、i=0,1,2,…について求めてみる。
//そして、grundy(0), grundy(1), …を眺めていると、隣り合う出現位置の差が重要っぽいと思うので、↑の階差を取ってみる。
//すると、途中から周期的になっていることに気付く。ただしその周期は複雑で、iの値によってまちまちである。
//そこで、「1周期」を取り出してみる。例えば、grundy数 = i(i = 0)の出現位置の階差の周期は{4, 12, 4, 4, 10}である。
//そして、「1周期分」の和を取ってみる。4 + 12 + 4 + 4 + 10 = 34のように。すると、どのiについてもこれが34になっていることが分かる。
//よって、ある大きな整数Mを取ってくると、M以上のx全てについて、grundy(x) = grundy(x + 34)が成立することが分かる。
//これは、周期の開始位置を見る限り、M >= 85に設定すればOK.よって、O(M^2 + N)でこの問題は解けた。

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int n;
int a[500000];
vector<int> nums;
int dp[500001];

int grundy(int x) {
	if (x >= 85 + 34) {
		return dp[x] = grundy(85 + (x - 85) % 34);
	}
	
	if (x == 0) return 0;
	if (x <= 2) return 1;
	if (dp[x] != -1) return dp[x];
	
	vector<int> gs;
	gs.push_back(grundy(x - 2));
	for (int i = 0; i + 3 <= x; i++) {
		int g = grundy(i) ^ grundy(x - i - 3);
		gs.push_back(g);
	}
	sort(gs.begin(), gs.end());
	
	for (int i = 0; ; i++) {
		int pos = lower_bound(gs.begin(), gs.end(), i) - gs.begin();
		if (pos == gs.size() || gs[pos] > i) {
			return dp[x] = i;
		}
	}
	return -1;
}

int main() {
	int i;
	
	for (i = 0; i < 500001; i++) dp[i] = -1;
	
	cin >> n;
	for (i = 0; i < n; i++) cin >> a[i];
	sort(a, a + n);
	
	int len = 1;
	for (i = 1; i < n; i++) {
		if (a[i] >= a[i - 1] + 2) {
			nums.push_back(len);
			len = 1;
		}
		else {
			len++;
		}
	}
	nums.push_back(len);
	
	int g = 0;
	for (i = 0; i < nums.size(); i++) g ^= grundy(nums[i]);
	if (g == 0) cout << "Second" << endl;
	else cout << "First" << endl;
	return 0;
}
0