A Simple Nim

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

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\le T\le 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\le n\le 10^6,1\le s[i]\le 10^9)$
 
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.
 
题意:n堆石子,两个人轮流拿,每次可以选择任意一堆取任意个(不能不拿)或者将一个堆分成3个小堆,问先手胜还是后手胜。
题解:SG定理的应用,可以看做是Nim博弈,打表找规律,我也不会证
  i=8*k+7时SG[i]=8*k+8,i=8*k+8时SG[i]=8*k+7其他情况SG[i]=i。
#include<bits/stdc++.h>
#define N 45000
#define mes(x) memset(x, 0, sizeof(x));
#define ll __int64
const long long mod = 1e9+;
const int MAX = 0x7ffffff;
using namespace std;
int dir[], SG[];
int getSG(int n){
if(n == ) return ;
if(n% == ) return n-;
if(n% == ) return n+;//注意这里是n%8==7,不是n%7==0....
return n;
}
int main()
{
int T,t, i,n, ans;
scanf("%d", &T);
while(T--){
scanf("%d", &n);
for(ans=i=;i<n;i++){
scanf("%d", &t);
ans ^= getSG(t);
}
if(ans==) printf("Second player wins.\n");
else printf("First player wins.\n");
}
return ;
}

HDU5795A Simple Nim SG定理的更多相关文章

  1. 多校6 1003 HDU5795 A Simple Nim (sg函数)

    思路:直接打表找sg函数的值,找规律,没有什么技巧 还想了很久的,把数当二进制看,再类讨二进制中1的个数是必胜或者必败状态.... 打表: // #pragma comment(linker, &qu ...

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

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

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

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

  4. hdu 5795 A Simple Nim 博弈sg函数

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

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

    /** 题目:A Simple Nim 链接:http://acm.hdu.edu.cn/showproblem.php?pid=5795 题意:给定n堆石子,每堆有若干石子,两个人轮流操作,每次操作 ...

  6. HDU 1851 (巴什博奕 SG定理) A Simple Game

    这是由n个巴什博奕的游戏合成的组合游戏. 对于一个有m个石子,每次至多取l个的巴什博奕,这个状态的SG函数值为m % (l + 1). 然后根据SG定理,合成游戏的SG函数就是各个子游戏SG函数值的异 ...

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

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

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

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

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

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

随机推荐

  1. Redis性能测试

    Redis 性能测试 Redis 性能测试是通过同时执行多个命令实现的.Redis性能测试主要是通过src文件夹下的redis-benchmark来实现(Linux系统下) 语法 redis 性能测试 ...

  2. golang 环境一键式配置

    在window下,通过以下bat,自动设置环境变量,启动终端,并cd到gopath目录 set goroot=c:\go set gopath=d:\go @start "start gol ...

  3. Mssql迁移至Oracle 查询优化

    针对Oracle的查询优化 a.避免使用nclob类型字段,可以通过排除此类型的字段,优化查询b.避免对字段进行NULL值判断,如:SELECT * FROM  TABLE WHERE COL IS ...

  4. stream数据流

    首先必须先要了解Buffer,Buffer是js和c++的结合体,类数组,通常与Stream一起用: 1. (1).Buffer是个类,因此可以构建成对象 (2).buf具有数组的性质 字符串转换成二 ...

  5. PythonNote01_HTML标签

    >头标签<head> >>位置 头标签要放在头部之间 >>种类 <title> : 指定整个网页的标题,在浏览器最上方显示. <meta&g ...

  6. 深入理解javascript原型和闭包(1)---一切都是对象

    深入理解javascript原型和闭包(1)---一切都是对象 type函数输出的类型,在此列出: function show (x){ console.log(typeof(x));//undefi ...

  7. c/c++ qsort 函数 结构体简单使用(1)

    #include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct student ...

  8. Displaying Data in a Chart with ASP.NET Web Pages (Razor)

    This article explains how to use a chart to display data in an ASP.NET Web Pages (Razor) website by ...

  9. Redis与Memcache的区别

    Redis与Memcache的区别 数据类型:  redis数据类型丰富,支持set liset等类型          memcache支持简单数据类型,需要客户端自己处理复杂对象 持久性: red ...

  10. 转Windows Phone8.1 获取手机唯一识别码

    转:http://www.dotblogs.com.tw/martinlau17/archive/2014/07/21/146020.aspx 因小弟比較懶,上次不小心 清空了所有文章 現在重寫了XD ...