A Simple Nim
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 948 Accepted Submission(s): 559
Problem Description
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.
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.
Input
Intput contains multiple test cases. The first line is an integer 1≤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], representing heaps with s[0],s[1],...,s[n−1] objects respectively.(1≤n≤106,1≤s[i]≤109)
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], representing heaps with s[0],s[1],...,s[n−1] objects respectively.(1≤n≤106,1≤s[i]≤109)
Output
For 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
2
4 4
3
1 2 4
Sample Output
Second player wins.
First player wins.
First player wins.
Author
UESTC
Source
最近学长讲博弈论 给我们讲了些题目 包括这道题
主要是求sg函数,打表1-40就可以看出规律,(带码学别人的)
x%8==7 sg()=x+1;
x%8==0 sg()=x-1;
其他 sg()=x
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
int visited[];
int sg[];
int main(){
/*int i,j,k;
for(i=1;i<40;i++){
memset(visited,0,sizeof(visited));
for(j=0;j<i;j++)visited[sg[j]]=1;
for(j=1;j<i;j++){
for(k=1;k+j<i;k++){
visited[sg[j]^sg[k]^sg[i-k-j]]=1;
}
}
for(j=0;j<40;j++){
if(visited[j]==0){
break;
}
}
sg[i]=j;
}
for(i=1;i<40;i++){
cout<<i<<" "<<sg[i]<<endl;
}*/
int t;
scanf("%d",&t);
int n,i,x;
int ans;
while(t--){
scanf("%d",&n);
ans=;
for(i=;i<n;i++){
scanf("%d",&x);
if(x%==){
x=x-;
ans=ans^x;
}
else if(x%==){
x=x+;
ans=ans^x;
}
else
ans=ans^x; }
cout<<ans<<endl;
}
}