Partitioning Game LightOJ
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
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;
}