A Simple Nim

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5795

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.

Source


2016 Multi-University Training Contest 6


##题意:

两人以最优策略对n堆物品进行操作,不能操作者输.
1. 从同一堆中取任意个(不为零).
2. 把一堆分成任意三堆(任一堆非空).


##题解:

变形的Nim博弈. 这里推导部分sg值找规律:
sg[0] = 0;
sg[1] = 1;
sg[2] = mex{sg[0], sg[1]} = mex{0,1} = 2;
sg[3] = mex{sg[0], sg[1], sg[2], sg[1,1,1]} = mex{0,1,2, sg[1]^sg[1]^sg[1] = 1} = 3;
sg[4] = 4;
sg[5] = 5;
sg[6] = 6;
sg[7] = 8;
sg[8] = 7;
sg[9] = 9;
综上,可以看出规律:
sg[0] = 0;
sg[8*k+7] = 8*k+8;
sg[8*k+8] = 8*k+7;
最后异或起来就可以了.

主要是之前做过类似版本:操作2是把一堆分成两堆.
HDU-3032 Nim or not Nim? (http://acm.hust.edu.cn/vjudge/contest/102108#problem/E)
这个版本的结果是sg[4k+3]=4k+4; sg[4k+4]=4k+3;


##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define mid(a,b) ((a+b)>>1)
#define eps 1e-8
#define maxn 2100
#define mod 1000000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;

int n;

int main(int argc, char const *argv[])

{

//IN;

int t; cin >> t;
while(scanf("%d", &n) != EOF)
{
LL ans = 0;
for(int i=1; i<=n; i++) {
LL x; scanf("%I64d", &x);
if(x % 8LL == 0LL) ans ^= x - 1;
else if(x % 8LL == 7LL) ans ^= x + 1;
else ans ^= x;
} if(ans) puts("First player wins.");
else puts("Second player wins.");
} return 0;

}

HDU 5795 A Simple Nim (博弈 打表找规律)的更多相关文章

  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函数

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

  3. A Simple Nim (SG打表找规律)

    题意:有n堆石子,每次可以将其中一堆分为数量不为0的3堆,或者从其中一堆中拿走若干个,最终拿完的那个人赢. 思路:直接暴力SG状态,然后找出其中的规律,异或一下每一堆的状态就可以了. #include ...

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

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

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

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

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

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

  7. hdu 3032 Nim or not Nim? (SG函数博弈+打表找规律)

    Nim or not Nim? Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Sub ...

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

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

  9. HDU 5795 A Simple Nim

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

随机推荐

  1. 内核MKDEV(MAJOR, MINOR)宏

    版本:linux-2.6.24.4宏:    MKDEV(MAJOR, MINOR);  说明: 获取设备在设备表中的位置.        MAJOR   主设备号        MINOR   次设 ...

  2. sqlsevrer中output的用法

    近日,看到代码中有output写法,不知其意,经过一番查找,终于找到了原因,它的作用是将修改影响的结果给输出出来. 比如update语句, 除了修改数据以外, 对于发生更新的列, update语句还可 ...

  3. Asp.Net连接Mysql报错Out of sync with server

    Asp.Net连接Mysql报错Out of sync with server 原因:程序引用的MySql.Data.dll版本高于服务器版本 解决:下载一个低版本的MySql.Data.dll,项目 ...

  4. Android中Java反射技术的使用示例

    import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Metho ...

  5. codeforces 334A - Candy Bags

    忘了是偶数了,在纸上画奇数画了半天... #include<cstdio> #include<cstring> #include<cstdlib> #include ...

  6. VirtualBox的工作原理&参考网上文章

    事先申明,我这里有好多东西都是看网上的,文末给出参考博客链接. 1.在设置里面为什么要选择桥接网络?baidu之后,了解到是虚拟机工作原理的不同,也就是说有好几种工作模式. bridged(桥接模式) ...

  7. USACO1.4.1 Packing Rectangles

    //毕竟我不是dd牛,USACO的题解也不可能一句话带过的…… 题目链接:http://cerberus.delos.com:790/usacoprob2?a=pWvHFwGsTb2&S=pa ...

  8. Darwin Streaming Server 安裝操作備忘

    Darwin Streaming Server 安裝操作 Darwin Streaming Server是蘋果公司推出的開放源碼.跨平台多媒體串流伺服器, 提供音樂 (mp3) 與影音 (3gp.mp ...

  9. 一天一个Java基础——数组

    一天一个变成了几天一个,最近接受的新东西太多.太快,有好多需要blog的但没有时间,这些基础知识应该是要深挖并好好研究的,不应该每次都草草了事,只看个皮毛. 数组: JVM将数组存储在一个称为堆(he ...

  10. 常用UI布局

    1.LinearLayout(线性布局):将布局所包含的控件在线性方向上依次排列. <1>android:orientation 指定了排列方向(垂直方向(vertical).水平方向(h ...