G - Partitioning Game LightOJ - 1199(博弈论、sg打表)

Alice and Bob are playing a strange game. The rules of the game are:

1.      Initially there are n piles.

2.      A pile is formed by some cells.

3.      Alice starts the game and they alternate turns.

4.      In each tern a player can pick any pile and divide it into two unequal piles.

5.      If a player cannot do so, he/she loses the game.

Now you are given the number of cells in each of the piles, you have to find the winner of the game if both of them play optimally.

Input

Input starts with an integer T (≤ 1000), denoting the number of test cases.

Each case starts with a line containing an integer n (1 ≤ n ≤ 100). The next line contains n integers, where the ith integer denotes the number of cells in the ith pile. You can assume that the number of cells in each pile is between 1 and 10000.

Output

For each case, print the case number and 'Alice' or 'Bob' depending on the winner of the game.

Sample Input

3

1

4

3

1 2 3

1

7

Sample Output

Case 1: Bob

Case 2: Alice

Case 3: Bob

题目意思:

给你n堆石子,每堆有ai个石子,每次操作可以将一堆分成数量不相同的两堆(石子数量不能为0),谁没法分就输了,问你谁能赢。

解题思路:

用sg打表来做,假设有一堆石子数量为x的石子堆,那么他的后继状态就有(1, x - 1)、(2, x - 2)...........等等(不能让两个数相等)

想要计算出x的sg值就需要其后继状态的sg值,那么(1,x - 1)的sg值怎么求呢?毕竟他是一个二元组,不是简单的一维状态。

可以把1, x - 1分别看成(1,x - 1)的两个子游戏,那么sg((1, x - 1)) == sg(1) ^ sg(x - 1),解决了这个问题直接暴力求sg就行了。

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int maxn = 11111;

int sg[maxn], mex[maxn];

void getsg()
{
	sg[0] = 0;
	sg[1] = 0;
	sg[2] = 0;
	for(int i = 3; i <= 10000; ++ i)
	{
		memset(mex, 0, sizeof(mex));
		for(int j = 1; j * 2 < i; ++ j)
		{
			mex[sg[j] ^ sg[i - j]] = 1;
		}
		int cnt = 0;
		while(mex[cnt] != 0)
			cnt++;
		sg[i] = cnt;
	}
}

int main()
{
	//freopen("in.txt", "r", stdin);
	getsg();
	int t, n;
	cin >> t;
	int k = 0;
	while(t --)
	{
		cin >> n;
		int x;
		int ans;
		for(int i = 1; i <= n; ++ i)
		{
			cin >> x;
			if(i == 1)
				ans = sg[x];
			else
				ans ^= sg[x];
		}
		printf("Case %d: ", ++ k);
		if(ans)
			cout << "Alice" << endl;
		else
			cout << "Bob" << endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/aqa2037299560/article/details/83929826