1. 程式人生 > >I - Again Stone Game LightOJ - 1296(博弈論,sg打表找規律)

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;
}