hdu 5795
Posted 见字如面
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 5795相关的知识,希望对你有一定的参考价值。
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.
Input
Intput contains multiple test cases. The first line is an integer 1≤T,
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
Sample Output
Second 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
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstring> 5 using namespace std; 6 int visited[100]; 7 int sg[100]; 8 int main(){ 9 /*int i,j,k; 10 for(i=1;i<40;i++){ 11 memset(visited,0,sizeof(visited)); 12 for(j=0;j<i;j++)visited[sg[j]]=1; 13 for(j=1;j<i;j++){ 14 for(k=1;k+j<i;k++){ 15 visited[sg[j]^sg[k]^sg[i-k-j]]=1; 16 } 17 } 18 for(j=0;j<40;j++){ 19 if(visited[j]==0){ 20 break; 21 } 22 } 23 sg[i]=j; 24 } 25 for(i=1;i<40;i++){ 26 cout<<i<<" "<<sg[i]<<endl; 27 }*/ 28 int t; 29 scanf("%d",&t); 30 int n,i,x; 31 int ans; 32 while(t--){ 33 scanf("%d",&n); 34 ans=0; 35 for(i=0;i<n;i++){ 36 scanf("%d",&x); 37 if(x%8==0){ 38 x=x-1; 39 ans=ans^x; 40 } 41 else if(x%8==7){ 42 x=x+1; 43 ans=ans^x; 44 } 45 else 46 ans=ans^x; 47 48 } 49 cout<<ans<<endl; 50 } 51 }
以上是关于hdu 5795的主要内容,如果未能解决你的问题,请参考以下文章
HDU 5795 A Simple Nim 打表求SG函数的规律
[hdu-5795]A Simple Nim 博弈 尼姆博弈 SG函数打表找规律
hdu5795 A Simple Nim 求nim求法,打表找sg值规律 给定n堆石子,每堆有若干石子,两个人轮流操作,每次操作可以选择任意一堆取走任意个石子(不可以为空) 或者选择一堆,把它分成