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 ;
}

最新文章

  1. PHP面向对象讲解
  2. mysql的ONLY_FULL_GROUP_BY语义 --转自http://www.wtoutiao.com/p/19dh3ec.html
  3. [转]undefined reference问题总结
  4. tomcat调优的几个方面
  5. 【DFS+记忆搜索】NYOJ-10-Skiing
  6. Linux Systemd——在RHEL/CentOS 7中启动/停止/重启服务
  7. Dp_F Pku1157
  8. Java乔晓松-android中的帧动画FrameByFrame
  9. Object.create() 实现
  10. [转]动态管理视图和函数 (Transact-SQL)
  11. 宝塔Linux面板命令大全
  12. Unique-paths (动态规划)
  13. Cocos2D中Node的userObject实例变量使用时一个要注意的地方
  14. 【LeetCode】二叉搜索树的前序,中序,后续遍历非递归方法
  15. [原创]zabbix工具介绍,安装及使用
  16. 添加字体与字符集locale支持(基于busybox文件系统)
  17. FFMPEG转换WAV到MP3
  18. OpenCV学习(16) 细化算法(4)
  19. 为啥我喜欢在Windows 7环境下做Unity开发?
  20. elementUI 学习入门之 container 布局容器

热门文章

  1. 爬虫学习06用selenium爬取空间
  2. xml文件中[Invalid byte 1 of 1-byte UTF-8 sequence.]的解决方案
  3. 计算概论(A)/基础编程练习(数据成分)/3:整数的个数
  4. 基于Theano的深度学习框架keras及配合SVM训练模型
  5. 如何将OpenCV中的Mat类绑定为OpenGL中的纹理
  6. RTSP 与 RTMP 协议 (转)
  7. An Example of How Oracle Works
  8. Spring-Data-Redis 下实现jedis连接断开后自动重连
  9. loj#2305. 「NOI2017」游戏 2-sat
  10. HDU 3506 Monkey Party(区间DP)题解