题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5795
题目大意:
给n堆石子,A和B两个人可以分别从任意一堆里面取任意石子。也可以选择将一堆石子分成新的三堆。谁最后拿不到石子谁输。
思路:
如果不分堆就是简单的Nim游戏。直接取异或和即可。现在分堆我们可以发现,如果一堆石子个数为7,那么分成1,2,4以后可以对局势不造成影响。所以我们可以通过打SG表发现规律。然后直接进行异或即可。
打表代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<set>
using namespace std;
int main()
{
set<int>st;
int sg[100];
sg[0]=0;
sg[1]=1;
for(int i=2;i<=100;i++){
st.clear();
for(int j=0;j<i;j++){
st.insert(sg[j]);
}
for(int a=i-2;a>=1;a--){
for(int b=a-1;b>=1;b--){
for(int c=a-1;c>=1;c--){
if(a+b+c==i)st.insert(sg[a]^sg[b]^sg[c]);
}
}
}
int tt=0;
while(st.count(tt))tt++;
sg[i]=tt;
printf("sg[%d]=%d\n",i,sg[i]);
}
}
代码:
#include<stdio.h>
#include<math.h>
int main()
{
int T,i,j,k,n,x;
long long sum=0;
scanf("%d",&T);
while(T--){
sum=0;
scanf("%d",&n);
for(i=1;i<=n;i++){
scanf("%d",&x);
if(x%8==0)sum^=(x-1);
else if((x+1)%8==0)sum^=(x+1);
else sum^=x;
}
if(sum==0)printf("Second player wins.\n");
else printf("First player wins.\n");
}
}