您的位置:首页 > 其它

LIGHT OJ 1199 - Partitioning Game

2016-04-15 11:35 302 查看
传送门

1199 - Partitioning Game





PDF (English)StatisticsForum
Time Limit: 4 second(s)Memory Limit: 32 MB
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

Output for Sample Input

3

1

4

3

1 2 3

1

7

Case 1: Bob

Case 2: Alice

Case 3: Bob

Explanation

In case 1, Alice has only 1 move, she divides the pile with 4 cells into two unequal piles, where one pile has 1 cell and the other pile has 3 cells. Now it's Bob's turn. Bob divides the pile with 3 cells into two piles, where one pile has 1 cell and another
pile has 2 cells. So, now there are three piles having cells 1, 1, 2. And Alice loses, since she doesn't have any moves now.

题目大意:

有n堆石子(1<=n<=100),每一堆分别有ai个石子(1<=ai<=10000),一次操作可以使一堆石子变成两堆数目不相等(注意是不相等)的石子,最后不能操作就算输,问先手赢还是后手赢。

解题思路:

就是一个SG函数,提到SG函数这个就是求一下 当前状态的下一个状态,又因为 这 n 堆石子是相互独立的,没有影响 所以说 可以开用SG函数,

根据SG定理,假设 当前堆中有 m块石子 那么他的下一状态就可能有 {1,m-1},{2,n-2},...,{(m-1)/2,m-(m-1)/2}(把每一种情况都想到
并且分析出来),

然后分完的那些 a和b块石子又可以进行分,以此类推,那么SG(x) = mex{ SG(1)^SG(x-1), SG(2)^SG(x-2),...,
SG((x-1)/2)^SG(x-(x-1)/2) },

然后我们要求的就是 SG[a[0]]^SG[a[1]]^...^SG[a[n-1]],如果结果是0就是 后手赢,否则 先手赢

My Code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 10000+5;
int sg[MAXN];
int hash[MAXN];
void Get_sg()///模板
{
memset(sg, 0, sizeof(sg));
for(int i=1; i<MAXN; i++)
{
memset(hash, 0, sizeof(hash));
for(int j=1; j*2<i; j++)
{
hash[sg[j]^sg[i-j]] = 1;
}
int j;
for(j=0; j<MAXN; j++)
if(!hash[j])
break;
sg[i] = j;
}
}
int main()
{
Get_sg();
int T;
scanf("%d",&T);
for(int cas=1; cas<=T; cas++)
{
int m, sum = 0;
scanf("%d",&m);
for(int i=0; i<m; i++)
{
int x;
scanf("%d",&x);
sum ^= sg[x];
}
if(sum)
printf("Case %d: Alice\n",cas);
else
printf("Case %d: Bob\n",cas);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: