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。
  1. #include<bits/stdc++.h>
  2. #define N 45000
  3. #define mes(x) memset(x, 0, sizeof(x));
  4. #define ll __int64
  5. const long long mod = 1e9+;
  6. const int MAX = 0x7ffffff;
  7. using namespace std;
  8. int dir[], SG[];
  9. int getSG(int n){
  10. if(n == ) return ;
  11. if(n% == ) return n-;
  12. if(n% == ) return n+;//注意这里是n%8==7,不是n%7==0....
  13. return n;
  14. }
  15. int main()
  16. {
  17. int T,t, i,n, ans;
  18. scanf("%d", &T);
  19. while(T--){
  20. scanf("%d", &n);
  21. for(ans=i=;i<n;i++){
  22. scanf("%d", &t);
  23. ans ^= getSG(t);
  24. }
  25. if(ans==) printf("Second player wins.\n");
  26. else printf("First player wins.\n");
  27. }
  28. return ;
  29. }

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. iOS基于MVC的项目重构总结

    关于MVC的争论 关于MVC的争论已经有很多,对此我的观点是:对于iOS开发中的绝大部分场景来说,MVC本身是没有问题的,你认为的MVC的问题,一定是你自己理解的问题(资深架构师请自动忽略本文). 行 ...

  2. 使用CLion编辑C工程

    最近正在研究Linux C代码编辑器,确实也不太喜欢SI(Windows看代码还行,编辑一般,同步麻烦), 尝试使用CLion,但对makefile工程支持不好,怎么编译还没搞懂, 阅读.编辑还不错, ...

  3. kafka集群搭建和使用Java写kafka生产者消费者

    1 kafka集群搭建 1.zookeeper集群  搭建在110, 111,112 2.kafka使用3个节点110, 111,112 修改配置文件config/server.properties ...

  4. Web 服务器 low bandth DOS attack

    https://www.owasp.org/images/0/04/Roberto_Suggi_Liverani_OWASPNZDAY2010-Defending_against_applicatio ...

  5. 设计一个程序,程序中有三个类,Triangle,Lader,Circle。

    //此程序写出三个类,triangle,lader,circle:其中triangle类具有类型为double的a,b,c边以及周长,面积属性, //具有周长,面积以及修改三边的功能,还有判断能否构成 ...

  6. python-study1 in hubei

    1.安装好python后要配置环境变量(C:\Python27\Scripts---能找到pip.exe和easy_install.exe和C:\Python27---能找到python.exe) 2 ...

  7. String转json

    一.下载json 具体到http://www.json.org/上找Java-json下载,并把其放到项目源代码中,这样就可以引用其类对象了 转载地址:http://blog.csdn.net/tax ...

  8. C语言pow函数编写

    C语言pow函数编写 #include<stdio.h> double chaoba(double f,double q); //声明自定义函数 void main(void) { dou ...

  9. Buge's Fibonacci Number Problem

    Buge's Fibonacci Number Problem Description snowingsea is having Buge’s discrete mathematics lesson, ...

  10. 【unity3d游戏开发脚本笔记之一:坐标系选择对物体运动的影响】

    时间:2016年9月24日17:38:21   作者:yexiaopeng 博客园     在unity3d的世界中,其坐标系可分为四种,世界坐标系-WorldSpace   本地坐标系-LocalS ...