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 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.
 
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+,maxm=1e5+,inf=0x3f3f3f3f,mod=1e9+;
const ll INF=1e18+; int sg[N],mex[N];
void initsg()
{
for(int i=;i<=;i++)
{
memset(mex,,sizeof(mex));
for(int j=;j<=i-;j++)
{
for(int k=;k+j<i;k++)
{
int l=i-j-k;
mex[sg[l]^sg[j]^sg[k]]=;
}
}
for(int j=;j<=i;j++)
mex[sg[j]]=;
for(int j=;;j++)
if(!mex[j])
{
sg[i]=j;
break;
}
}
for(int i=;i<=;i++)
if(sg[i]!=i)cout<<sg[i]<<" "<<i<<endl;
}
int SG(int x)
{
if(x%==)return x+;
if(x%==)return x-;
return x;
}
int main()
{
//initsg();
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
int ans=;
for(int i=;i<=n;i++)
{
int x;
scanf("%d",&x);
ans^=SG(x);
}
if(ans)printf("First player wins.\n");
else printf("Second player wins.\n");
}
return ;
}

hdu 5795 A Simple Nim 博弈sg函数的更多相关文章

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

    A Simple Nim Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  2. HDU 5795 A Simple Nim(SG打表找规律)

    SG打表找规律 HDU 5795 题目连接 #include<iostream> #include<cstdio> #include<cmath> #include ...

  3. HDU 5795 A Simple Nim(简单Nim)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

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

    A Simple Nim Problem Description   Two players take turns picking candies from n heaps,the player wh ...

  5. HDU 5795 A Simple Nim (博弈 打表找规律)

    A Simple Nim 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5795 Description Two players take turns ...

  6. HDU 5795 A Simple Nim

    打表找SG函数规律. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> ...

  7. HDU 5795 A Simple Nim ——(Nim博弈 + 打表)

    题意:在nim游戏的规则上再增加了一条,即可以将任意一堆分为三堆都不为0的子堆也视为一次操作. 分析:打表找sg值的规律即可. 感想:又学会了一种新的方法,以后看到sg值找不出规律的,就打表即可~ 打 ...

  8. POJ 2311 Cutting Game(Nim博弈-sg函数/记忆化搜索)

    Cutting Game 题意: 有一张被分成 w*h 的格子的长方形纸张,两人轮流沿着格子的边界水平或垂直切割,将纸张分割成两部分.切割了n次之后就得到了n+1张纸,每次都可以选择切得的某一张纸再进 ...

  9. S-Nim HDU 1536 博弈 sg函数

    S-Nim HDU 1536 博弈 sg函数 题意 首先输入K,表示一个集合的大小,之后输入集合,表示对于这对石子只能去这个集合中的元素的个数,之后输入 一个m表示接下来对于这个集合要进行m次询问,之 ...

随机推荐

  1. ASP.NET定时调用WebService 运行后台代码

    效果: 通过在网站的Global.asax的Application_Start方法中 加入定时器 定时调用WebService 该WebService的一个方法 负责在后台 向数据库的某个表加入数据 ...

  2. linux centos6.5 php5.6 安装PHPUnit 5.2.9 (转)

    转自:http://blog.csdn.net/shancunxiaoyazhi/article/details/50765293 操作系统版本:CentOS6.5 PHP版本:5.6 下载phpun ...

  3. Tomcat启动程序端口冲突、确认相应进程及杀死冲突进程的解决方案

    一. 查看所有进程占用的端口 在开始-运行-cmd,输入:netstat –ano可以查看所有进程 二.查看占用指定端口的程序(1)命令窗口输出 命令:netstat –ano | findstr & ...

  4. django crontab 定时任务

    分 时 日 月 周 命令(最好用绝对路径)比如: * * * * * rm -fr /mnt/* //每分钟执行一次对/mnt目录下文件的删除*/2 * * * * rm -fr /mnt/* //每 ...

  5. php stomp.dll 下载地址

    http://pecl.php.net/package/stomp/1.0.9/windows 查看方法,打开phpinfo

  6. mysql引擎和事务

    对于应用程序和用户来说,同样一张表的数据无论用什么引擎来存储,看到的数据都是一样的,只是不同的引擎在功能.占用空间大小.读取性能等方面可能有所差别. mysql最常用的存储引擎为Innodb.MyIS ...

  7. tcpdump 抓包工具使用

    1. 常用命令 监听p4p1网卡上来自 192.168.162.14 的包 tcpdump -i p4p1 src host 192.168.162.14 tcpdump -i p4p1 dst po ...

  8. suse日常操作(含suse/rhel内核与发行版对应关系)

    最近有家客户要求只能使用suse系统,是suse 12 sp3的,而且版本都不同意换,一直以来,都是使用rhel的客户,还没遇到过suse的,可偏偏不巧,我们的系统和suse 12 sp3自带的gli ...

  9. 08: vue组件

    1.1 初识组件 1.什么是组件 1. Html中有组件,是一段可以被复用的结构代码 2. Css中有组件,是一段可以被复用的样式 3. Js中有组件,是一段可以被复用的功能 4. Vue中也有组件, ...

  10. SQL server无法连接上服务器的相关问题

    安装MySql以后,SQL server突然就连接不上服务器了,问了老师,说是啥进程关闭了,都打开了也没搞好,都准备重装了,结果看到度妈上面的一篇文献,原来是SQL  server MSSQLSERV ...