hdu 5795 A Simple Nim (sg函数)

A Simple Nim

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

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≤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)

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.

题意:有n堆石子,你每次可以任选一堆取任意个或者将一堆分为3个小堆,谁最后拿走谁赢

思路:sg打表可以看出来规律

ac代码:

/* ***********************************************
Author       : AnICoo1
Created Time : 2016-08-06-11.14 Saturday
File Name    : D:\MyCode\2016-8月\2016-8-6.cpp
LANGUAGE     : C++
Copyright  2016 clh All Rights Reserved
************************************************ */
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 1010000
#define LL long long
#define ll __int64
#define INF 0xfffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define eps 1e-8
using namespace std;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}
//head
int sg[10010];
int getSG(int x)
{
   // cout<<x<<endl;
    if(sg[x]!=-1) return sg[x];
    if(x<=2)
        return x;
    int vis[1010];mem(vis);
    for(int a=1;a<x;a++)
    {
        int sa=getSG(a);
        for(int b=1;b<x;b++)
        {
            int c=x-a-b;
            if(a>0&&b>0&&c>0&&a+b+c==x)
            {
                int sb=getSG(b),sc=getSG(c);
                vis[sb]=1;vis[sc]=1;
                vis[sa^sb^sc]=1;
            }
        }
        vis[sa]=1;
    }
    vis[0]=1;
    for(int i=0;;i++)
        if(!vis[i]) return i;
}
int main()
{
    int t;cin>>t;
    while(t--)
    {
        int n;cin>>n;int ans=0;
        for(int i=1;i<=n;i++)
        {
            int a;cin>>a;
            if(a%8==7)
                ans^=a+1;
            else if(a%8==0)
                ans^=a-1;
            else
                ans^=a;
        }
        if(ans) printf("First player wins.\n");
        else printf("Second player wins.\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值