您的位置:首页 > 其它

【博弈+SG函数+打表找规律】HDU_5795_A Simple Nim

2017-08-05 23:26 435 查看
Two players take turns picking candies from n heaps,the player who picks the last one will win the game.On each turn they can pick any number of candies which come from the same heap(picking no candy is not allowed).To make the game
more interesting,players can separate one heap into three smaller heaps(no empty heaps)instead of the picking operation.Please find out which player will win the game if each of them never make mistakes.

InputIntput contains multiple test cases. The first line is an integer 1≤T≤1001≤T≤100,
the number of test cases. Each case begins with an integer n, indicating the number of the heaps, the next line contains N integers s[0],s[1],....,s[n−1]s[0],s[1],....,s[n−1],
representing heaps with s[0],s[1],...,s[n−1]s[0],s[1],...,s[n−1] objects
respectively.(1≤n≤106,1≤s[i]≤109)(1≤n≤106,1≤s[i]≤109)
OutputFor each test case,output a line whick contains either"First player wins."or"Second player wins".
Sample Input
2
2
4 4
3
1 2 4


Sample Output
Second player wins.
First player wins.

#include <bits/stdc++.h>
using namespace std;
int vis[10010],sg[10010];
/*
//打表找规律
int cal(int x){
memset(vis,0,sizeof(vis));
for(int i=0;i<x;i++)//x能到达0到x-1所有的状态
vis[sg[i]]=1;
for(int i=1;i<x;i++){//分为3堆
for(int j=1;j<x&&i+j<x;j++){
for(int k=1;k<x&&i+j+k<=x;k++){
if(i+j+k==x)
vis[sg[i]^sg[j]^sg[k]]=1;
}
}
}
for(int i=0;;i++)//mex(0,1,...x-1,分成三堆的sg值)
if(!vis[i])
return sg[x]=i;
}*/
int cal(int x){
if(x%8==7)
return x+1;
else if(x%8==0)
return x-1;
return x;
}
int main()
{
int t,n,x;
scanf("%d",&t);
while(t--){
int ans=0;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&x);
ans^=cal(x);
}
if(!ans) puts("Second player wins.");//异或值为0,必败状态
else puts("First player wins.");
}
/*
memset(sg,-1,sizeof(sg));
sg[0]=0;sg[1]=1;sg[2]=2;
for(int i=3;i<=100;i++){
printf("%d %d\n",i,cal(i));
}*/
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: