boj 672

Description
As the title suggest, this problem is yet another boring stone game.
There are N piles of stones, Alice and Bob take turns to take away stones from the game, Alice plays first. Each time one can choose a pile and take n stones away from it, where n=x^y, x is a given number and y is a non-negative integer, i.e. n=1, x, x*x, x*x*x, .... The one who takes the last stone wins the game. Your task is to answer who wins the game with the assumption that both players take the best strategy.
 
Input
The first line contains an integer T(T<=1000), indicating the number of test cases.
For each case, there are two integers on the first line, N(1<=N<=1000) and x(1<=x<=100000), followed by N positive integers(<=10^9) on the second line, the numbers of the N piles of stones.
 
Output
For each case, output the name of the winner.
 
Sample Input
3
3 2
2 1 1
3 2
9 8 5
2 3
3 4
 
Sample Output
Alice
Bob
Alice

 

思路:首先关于sg函数的定义及性质的了解:

http://hi.baidu.com/yy17yy/item/4442dde3c92b53266cabb855

考虑一堆石子的个数N。

当x为奇数时,即x=2m+1时,此时易知,当N为奇数时,先手胜,为偶数时,后手胜,所以奇数sg值为1,偶数sg值为0。

当x为偶数时,即x=2m时,此时,对于个数为N的胜负态,以x+1为循环,对于a*(x+1)+q,(q∈[1,x+1)),胜负太与q相同(证略)。1~x+1中,奇数sg为1,x为2(因为x可到达的状态为0,x-1,g(0)=0,g(x-1)=1,所以g(x)=2)。

N堆的sg值即为N堆石子sg值的异或值。

代码:

#include<iostream>
using namespace std;

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,x;
		scanf("%d%d",&n,&x);
		int judge=x%2;
		int sg=0;
		for(int i=0;i<n;i++)
		{
			int tmp;
			scanf("%d",&tmp);
			if(judge)
			{
				if(tmp%2==1)
					sg=sg^1;
			}
			else
			{
				tmp=tmp%(x+1);
				if(tmp==x)
					sg=sg^2;
				else if(tmp%2==1)
					sg=sg^1;
			}
		}
		if(sg)
			printf("Alice\n");
		else
			printf("Bob\n");
	}
}

 

你可能感兴趣的:(BO)