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. GUI常用对象的属性

    %常用对象的属性 %.figure %hf=figure; %get(hf); %改变颜色 set Color %set(hf,'Color','w'); %去掉默认的菜单 Menubar %set( ...

  2. HDU 1207 汉诺塔II (递推)

    经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往上按大小顺序摞着64片黄金圆盘.上 ...

  3. python装饰器介绍

    """装饰器 定义:本质是函数(器:就是函数的意思),功能:装饰其他函数,就是为其他函数添加附加功能 原则: 1. 不能修改被装饰的函数的源代码 2. 不能修改被装饰的函 ...

  4. 计算概论(A)/基础编程练习1(8题)/1:大象喝水

    #include<stdio.h> int main() { ; // n < 100 scanf("%d", &n); // 循环遍历判断 再进行平方和 ...

  5. jquery获取包含本身的元素

    我们知道,使用jquery获取一个元素内的所有元素非常容易,使用jQuery.html()就可以. 如果是js语法的话,使用domObj.innerHTML也很容易实现. 那么问题来了,要想获取包涵节 ...

  6. alert.log中的minact-scn: useg scan erroring out with error e:376警告

    早上,开发说昨晚一台服务器的undo好像有异常,早上上去一看,库停了,正常startup之后,随手crud了一把,都正常.去看alert.log日志,发现undo某个数据块访问报I/O读错误,如下: ...

  7. devexpress 之 ChartControl

    \}5#~mV#cr(/k1yIZ7.Lg

  8. UVa 11997 K Smallest Sums - 优先队列

    题目大意 有k个长度为k的数组,从每个数组中选出1个数,再把这k个数进行求和,问在所有的这些和中,最小的前k个和. 考虑将前i个数组合并,保留前k个和.然后考虑将第(i + 1)个数组和它合并,保留前 ...

  9. 为什么不能用drop function add 去删除函数? 因为不能使用 mysql中的保留字!

    mysql中有很多的 保留字, 也叫关键字, 你在使用 数据库中的任何东西, 都最好是 避开这些关键字/保留字, 包括 数据库名, 表名, 字段名, 函数名, 存储过程名. 这些关键字包括: mysq ...

  10. POJ 2594 Treasure Exploration(最小可相交路径覆盖)题解

    题意:有n个点,m条单向边,每个机器人能沿着单向边走,能重复经过一个点,问最少几个机器人走遍n个点 思路:原来以前学的都是不能相交的算法....可相交的做法是跑Floyd把能到达的都加上边,然后跑最小 ...