I - Again Stone Game LightOJ - 1296(博弈论,sg打表找规律)

Alice and Bob are playing a stone game. Initially there are n piles of stones and each pile contains some stone. Alice stars the game and they alternate moves. In each move, a player has to select any pile and should remove at least one and no more than half stones from that pile. So, for example if a pile contains 10 stones, then a player can take at least 1 and at most 5 stones from that pile. If a pile contains 7 stones; at most 3 stones from that pile can be removed.

Both Alice and Bob play perfectly. The player who cannot make a valid move loses. Now you are given the information of the piles and the number of stones in all the piles, you have to find the player who will win if both play optimally.

Input

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

Each case starts with a line containing an integer n (1 ≤ n ≤ 1000). The next line contains n space separated integers ranging in [1, 109]. The ith integer in this line denotes the number of stones in the ith pile.

Output

For each case, print the case number and the name of the player who will win the game.

Sample Input

5

1

1

3

10 11 12

5

1 2 3 4 5

2

4 9

3

1 3 9

Sample Output

Case 1: Bob

Case 2: Alice

Case 3: Alice

Case 4: Bob

Case 5: Alice

 

题解:

就是nim博弈变形,直接用sg打表找规律就好了(不知道sg打表是什么的自行百度了解,就不解释了),最后发现所有偶数x的sg值为x / 2, 所有奇数y的sg值计算方法为:每次除以2,向下取整,直到y变成偶数然后就可以用偶数计算sg值的方法计算奇数y的sg值。

 

#include <iostream>
#include <map>
#include <set>
#include <cstring>
#include <cstdio>
using namespace std;

typedef long long ll;
const int maxn = 500;
int sg[maxn];
bool vis[maxn];

set < int > se;

void getsg()
{
	sg[0] = 0;
	for(int i = 1; i <= 100; ++ i)
	{
		memset(vis, false, sizeof(vis));
		for(int j = 1; j <= i / 2; ++ j)
		{
			vis[sg[i - j]] = true;
		}
		int cnt = 0;
		while(vis[cnt] != 0)
			cnt++;
		sg[i] = cnt;
	}
}

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

猜你喜欢

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