zoukankan      html  css  js  c++  java
  • hdu 5795 A Simple Nim 博弈sg函数

    A Simple Nim

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)



    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

    题意:跟普通的nim不一样的是,一堆可以分成三小堆;

    思路:sg函数打表找规律;

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    #include<set>
    #include<map>
    #include<queue>
    #include<stack>
    #include<vector>
    using namespace std;
    typedef long long ll;
    #define PI acos(-1.0)
    const int N=1e3+100,maxm=1e5+100,inf=0x3f3f3f3f,mod=1e9+7;
    const ll INF=1e18+7;
    
    int sg[N],mex[N];
    void initsg()
    {
        for(int i=1;i<=500;i++)
        {
            memset(mex,0,sizeof(mex));
            for(int j=1;j<=i-2;j++)
            {
                for(int k=1;k+j<i;k++)
                {
                    int l=i-j-k;
                    mex[sg[l]^sg[j]^sg[k]]=1;
                }
            }
            for(int j=1;j<=i;j++)
                mex[sg[j]]=1;
            for(int j=1;;j++)
            if(!mex[j])
            {
                sg[i]=j;
                break;
            }
        }
        for(int i=1;i<=500;i++)
        if(sg[i]!=i)cout<<sg[i]<<" "<<i<<endl;
    }
    int SG(int x)
    {
        if(x%8==7)return x+1;
        if(x%8==0)return x-1;
        return x;
    }
    int main()
    {
        //initsg();
        int T;
        scanf("%d",&T);
        while(T--)
        {
            int n;
            scanf("%d",&n);
            int ans=0;
            for(int i=1;i<=n;i++)
            {
                int x;
                scanf("%d",&x);
                ans^=SG(x);
            }
            if(ans)printf("First player wins.
    ");
            else printf("Second player wins.
    ");
        }
        return 0;
    }
  • 相关阅读:
    [机器学习]决策树
    [机器学习]Bagging and Boosting
    [机器学习]SVM原理
    [算法]排序算法综述
    天目山大峡谷和西天目山游记【转】
    西天目山出游攻略
    京东物流深度研究报告:京东物流VS亚马逊物流VS顺丰
    「数据标签体系」中台价值链路中“核心的核心”
    网络空间安全概论第一、四章笔记
    21牛客多校第一场
  • 原文地址:https://www.cnblogs.com/jhz033/p/7399844.html
Copyright © 2011-2022 走看看