hdu-5795 A Simple Nim(组合游戏)

题目链接:

A Simple Nim

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

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.

题意:

打表找规律的题,好想知道怎么归纳法证明啊;

一直不知道怎么打表,直接笨死了;

思路:

AC代码;

/************************************************
┆  ┏┓   ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃       ┃ ┆
┆┃   ━   ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃       ┃ ┆
┆┃   ┻   ┃ ┆
┆┗━┓    ┏━┛ ┆
┆  ┃    ┃  ┆      
┆  ┃    ┗━━━┓ ┆
┆  ┃  AC代马   ┣┓┆
┆  ┃           ┏┛┆
┆  ┗┓┓┏━┳┓┏┛ ┆
┆   ┃┫┫ ┃┫┫ ┆
┆   ┗┻┛ ┗┻┛ ┆
************************************************ */ 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>

using namespace std;

#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));

typedef  long long LL;

template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<‘0‘||CH>‘9‘;F= CH==‘-‘,CH=getchar());
    for(num=0;CH>=‘0‘&&CH<=‘9‘;num=num*10+CH-‘0‘,CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + ‘0‘);
    putchar(‘\n‘);
}

const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e6+10;
const int maxn=2e3+14;
const double eps=1e-12;
/*
int f[110],sg[110];
inline void Init()
{
    For(i,0,100)
    {
        mst(f,0);
        For(j,1,i)
        {
            f[sg[i-j]]=1;
        }
        if(i>=3)
        {
            For(j,1,i-1)
            For(k,1,i-1)
            {
                if(j+k<i)f[sg[j]^sg[k]^sg[i-j-k]]=1;
            }
        }
        For(j,0,100)if(!f[j]){sg[i]=j;break;}
        cout<<i<<" "<<sg[i]<<endl;
    }
}
*/
int main()
{
        //Init();
        int t;
        read(t);
        while(t--)
        {
            int n,sum=0,x;
            read(n);
            For(i,1,n)
            {
                read(x);
                if(x%8==7)sum^=x+1;
                else if(x%8==0&&x)sum^=x-1;
                else sum^=x;
            }
            if(sum==0)cout<<"Second player wins.\n";
            else cout<<"First player wins.\n";
        }
        return 0;
}

  

时间: 2024-08-24 23:15:14

hdu-5795 A Simple Nim(组合游戏)的相关文章

?HDU 5795 A Simple Nim(简单Nim)

HDU 5795 A Simple Nim(简单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 e

HDU 5795 A Simple Nim 打表求SG函数的规律

A Simple Nim 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).T

HDU 5795 A Simple Nim (博弈) ---2016杭电多校联合第六场

A Simple Nim Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 79    Accepted Submission(s): 48 Problem Description Two players take turns picking candies from n heaps,the player who picks the las

hdu 5795 A Simple Nim (sg 博弈)

官方题解: A Simple Nim sg[0]=0 当x=8k+7时sg[x]=8k+8, 当x=8k+8时sg[x]=8k+7, 其余时候sg[x]=x:(k>=0) 打表找规律可得,数学归纳法可证. 具体的打表放在了代码里面 ,详见init函数 /*by*/ #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace s

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 an

POJ 2068 Nim 组合游戏

题目大意:有一堆石子,两伙人,围在一起坐,坐的顺序是ABABABAB....每一个人最多能取a[i]个石子,取走最后一个石子的就输了.问谁能赢. 思路:朴素的组合游戏判定问题,这个题给了数据范围,可以进行记忆化搜索.f[i][j]为还剩下i个石子,到了第j个人的时候的状态,然后记忆化一下. CODE: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #

HDU 5794 A Simple Nim 2016多校第六场1003

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5795 题意:给你n堆石子,每一堆有ai石头,每次有两种操作,第一种在任意一堆取出任意数量的石头但不能为0,第二种把一堆石头分成三堆任意数量的石头(不能为0),问是先手赢还是后手赢. 题解:这就是一个Nim游戏!那么Nim游戏一般跟SG函数有关,所以这道题打表找规律即可. 关于SG函数,在这里也说一下吧!毕竟第一次接触. Nim游戏与SG函数:http://baike.baidu.com/link?u

HDU 5794 - A Simple Nim

题意:    n堆石子,先拿光就赢,操作分为两种:        1.任意一堆中拿走任意颗石子        2.将任意一堆分成三小堆 ( 每堆至少一颗 )        分析:    答案为每一堆的SG函数值异或和.    故先打表寻找单堆SG函数规律.    其中,若 x 可分为 三堆 a,b,c ,则 SG[x] 可转移至子状态 SG[a] ^ SG[b] ^ SG[c]  (三堆SG值异或和)        打表后发现:        SG[ 8*k - 1 ] = 8*k       

hdu5795 A Simple Nim 求nim求法,打表找sg值规律 给定n堆石子,每堆有若干石子,两个人轮流操作,每次操作可以选择任意一堆取走任意个石子(不可以为空) 或者选择一堆,把它分成三堆,每堆不为空。求先手必胜,还是后手必胜。

/** 题目:A Simple Nim 链接:http://acm.hdu.edu.cn/showproblem.php?pid=5795 题意:给定n堆石子,每堆有若干石子,两个人轮流操作,每次操作可以选择任意一堆取走任意个石子(不可以为空) 或者选择一堆,把它分成三堆,每堆不为空.求先手必胜,还是后手必胜. 思路: 组合游戏Nim: 计算出每一堆的sg值,然后取异或.异或和>0那么先手,否则后手. 对于每一堆的sg值求解方法: 设:sg(x)表示x个石子的sg值.sg(x) = mex{sg