标签:
Description
Alice and Bob are playing a strange game. The rules of the game are:
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
3
1
4
3
1 2 3
1
7
Sample Output
Case 1: Bob
Case 2: Alice
Case 3: Bob
题意:有n堆石子(1<=n<=100),每一堆分别有xi个石子(1<=xi<=10000),
一次操作可以使一堆石子变成两堆数目不相等的石子,
最后不能操作的算输,问先手胜还是后手胜。
思路:n堆石子相互独立,所以可以应用SG定理,只需要算出一堆石子的SG函数。
一堆石子(假设有x个)的后继状态可以枚举出来,分别是{1,x-1},{2,x-2},...,{(x-1)/2,x-(x-1)/2},
一堆石子分成的两堆石子又相互独立,再次应用SG定理。
所以SG(x) = mex{ SG(1)^SG(x-1), SG(2)^SG(x-2),..., SG((x-1)/2)^SG(x-(x-1)/2) },
最后的答案是SG(x1)^SG(x2)^...^SG(xn)
#include <iostream> #include <string.h> #include <stdio.h> using namespace std; int hash[10005],sg[10005]; void getsg() //获得sg函数的模板 { memset(sg,0,sizeof(sg)); for(int i=1;i<=10000;i++) { memset(hash,0,sizeof(hash)); for(int j=1;j+j<i;j++) hash[sg[j]^sg[i-j]]++; for(int j=0;j<=10000;j++) if(!hash[j]) { sg[i]=j; break; } } } int main() { getsg(); int i,n,t,cas=1; cin>>t; while(t--) { cin>>n; int ans=0,data; for(i=0;i<n;i++) { cin>>data; ans^=sg[data]; //常规尼姆异或 } if(ans) printf("Case %d: Alice\n",cas++); else printf("Case %d: Bob\n",cas++); } return 0; }
Light OJ 1199 - Partitioning Game (博弈sg函数)
标签:
原文地址:http://www.cnblogs.com/Ritchie/p/5396893.html