HDU 5795 A Simple Nim (SG函式+打表找規律)

Mr_Treeeee發表於2020-04-06

A Simple Nim

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1129    Accepted Submission(s): 637


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 1T100, 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[n1], representing heaps with s[0],s[1],...,s[n1] objects respectively.(1n106,1s[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

2016 Multi-University Training Contest 6


POINT:

遊戲的和的SG函式值是它的所有子游戲的SG函式值的異或(重要!!)


#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define ll long long
/*int sg[10005]= {0,1,2};//打表
int vis[10005];
void play()
{
    for(int i=3;i<=100;i++)
    {
        memset(vis,0,sizeof vis);
        for(int j=1;j<=i;j++)
        {
            for(int k=1;k<=i;k++)
            {
                if(j+k<i)
                {
                    vis[sg[j]^sg[k]^sg[i-j-k]]=1;
                }
            }
        }
        for(int j=0;j<i;j++)
        {
            vis[sg[j]]=1;
        }
        for(int j=0;;j++)
        {
            if(!vis[j])
            {
                sg[i]=j;
                break;
            }
        }
    }
    for(int i=0;i<=100;i++)
    {
        printf("%d %d\n",i,sg[i]);
    }
}
*/
ll sg(ll n)
{
    if(n%8==0) return n-1;
    else if((n+1)%8==0) return n+1;
    else return n;
}
int main()
{
   // play();
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        ll res=0;
        while(n--)
        {
            ll a;
            scanf("%lld",&a);
            res=res^sg(a);
        }
        if(res==0) printf("Second player wins.\n");
        else printf("First player wins.\n");
        
    }
    return 0;
}



相關文章