A Simple Nim

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

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
 
Recommend
wange2014
题意:给定n堆石子,每次有两种操作,1是从任意一堆中取任意个,2是把一堆分为三堆且这三堆都不为空,问谁能赢。
题解:首先看题目的数据范围是1e9,那么算出每个sg肯定会超时,所以我们想到打表找规律。首先我们暴力打表算出200以内的sg函数值,对于一堆石子来说处理出他的所有后继情况。
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. using namespace std;
  5. int sg[]= {};
  6. bool vis[];
  7. int t,cas=;
  8. void get()
  9. {
  10. memset(sg,,sizeof(sg));
  11. sg[]=;
  12. for(int i=; i<=; i++)
  13. {
  14. memset(vis,,sizeof(vis));
  15. for(int j=;j<=i;j++)
  16. {
  17. for(int k=;j+k<=i;k++)
  18. {
  19. if(j!=&&k!=&&((i-j-k)!=))
  20. vis[sg[j]^sg[k]^sg[i-j-k]]=;//操作
  21. }
  22. }
  23. for(int j=;j<i;j++)//操作
  24. vis[sg[j]]=;
  25. for(int x=; ; x++)
  26. if(!vis[x])
  27. {
  28. sg[i]=x;
  29. break;
  30. }
  31. }
  32. }
  33. int main()
  34. {
  35. get();
  36. for(int i=;i<=;i++)
  37. printf("i: %d %d\n",i,sg[i]);
  38. return ;
  39. }

运行该代码,我们会得到如下结果:

注意在这个表当中

观察结果我们可以得知,当n为8的倍数时,sg(n)=n+1; 当(n+1)为8的倍数时,sg(n)=n-1; 否则sg(n)=n;

所以我们把sg值异或起来就能判断出谁赢了。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <cstring>
  5. using namespace std;
  6. int main()
  7. {
  8. int t;
  9. scanf("%d",&t);
  10. while(t--)
  11. {
  12. int n;
  13. long long x,ans=;
  14. scanf("%d",&n);
  15. for(int i=;i<n;i++)
  16. {
  17. scanf("%lld",&x);
  18. if(x%==)
  19. ans=ans^(x-);
  20. else if((x+)%==)
  21. ans=ans^(x+);
  22. else
  23. ans=ans^x;
  24. }
  25. if(ans)
  26. puts("First player wins.");
  27. else
  28. puts("Second player wins.");
  29. }
  30. return ;
  31. }

HDU 5795 A Simple Nim (博弈) ---2016杭电多校联合第六场的更多相关文章

  1. HDU 5754 Life Winner Bo (各种博弈) 2016杭电多校联合第三场

    题目:传送门 题意:一个国际象棋棋盘,有四种棋子,从(n,m)走到(1,1),走到(1,1)的人赢,先手赢输出B,后手赢输出G,平局输出D. 题解:先把从(n,m)走到(1,1)看做是从(1,1)走到 ...

  2. HDU 5793 A Boring Question (逆元+快速幂+费马小定理) ---2016杭电多校联合第六场

    A Boring Question Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  3. HDU 5762 Teacher Bo (鸽笼原理) 2016杭电多校联合第三场

    题目:传送门. 题意:平面上有n个点,问是否存在四个点 (A,B,C,D)(A<B,C<D,A≠CorB≠D)使得AB的横纵坐标差的绝对值的和等于CD的横纵坐标差的绝对值的和,n<1 ...

  4. HDU 5752 Sqrt Bo (思维题) 2016杭电多校联合第三场

    题目:传送门. 题意:一个很大的数n,最多开5次根号,问开几次根号可以得到1,如果5次还不能得到1就输出TAT. 题解:打表题,x1=1,x2=(x1+1)*(x1+1)-1,以此类推.x5是不超过l ...

  5. HDU 5724 Chess (状态压缩sg函数博弈) 2016杭电多校联合第一场

    题目:传送门. 题意:有n行,每行最多20个棋子,对于一个棋子来说,如果他右面没有棋子,可以移动到他右面:如果有棋子,就跳过这些棋子移动到后面的空格,不能移动的人输. 题解:状态压缩博弈,对于一行2^ ...

  6. HDU 5821 Ball (贪心排序) -2016杭电多校联合第8场

    题目:传送门. 题意:T组数据,每组给定一个n一个m,在给定两个长度为n的数组a和b,再给定m次操作,每次给定l和r,每次可以把[l,r]的数进行任意调换位置,问能否在转换后使得a数组变成b数组. 题 ...

  7. HDU 5831 Rikka with Parenthesis II (贪心) -2016杭电多校联合第8场

    题目:传送门. 题意:T组数据,每组给定一个长度n,随后给定一个长度为n的字符串,字符串只包含'('或')',随后交换其中两个位置,必须交换一次也只能交换一次,问能否构成一个合法的括号匹配,就是()( ...

  8. HDU 5744 Keep On Movin (贪心) 2016杭电多校联合第二场

    题目:传送门. 如果每个字符出现次数都是偶数, 那么答案显然就是所有数的和. 对于奇数部分, 显然需要把其他字符均匀分配给这写奇数字符. 随便计算下就好了. #include <iostream ...

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

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

随机推荐

  1. 状态压缩dp问题

    问题:Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Ev ...

  2. android studio中the logging tag can be most 23 characters

    转:http://blog.csdn.net/voiceofnet/article/details/49866047 今天写代码的时候,突然发现平时用的好好的Log竟然报错,提示信息为:the log ...

  3. UVALive 6523 Languages

    传送门 The Enterprise has encountered a planet that at one point had been inhabited. The only remnant f ...

  4. hdu 1202 The calculation of GPA

    感觉本题没有什么好解释的,已知公式,直接编程即可. 1.统计所有 科目的学分 得到总学分 2.统计所有 成绩对应的绩点*对应的学分即可(如果成绩==-1直接continue,不进行统计),得到总绩点. ...

  5. 设置button不同状态下的背景色,即把这个颜色变成图片设置成,背景图片

    - (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state { [self setBack ...

  6. asp.net修改web.config文件

    private void UpdateConfigFile() { var cfg = System.Web.Configuration.WebConfigurationManager.OpenWeb ...

  7. zabbix: failed to accept an incoming connection

    错误描述 查日志发现: failed to accept an incoming connection: connection from "192.168.186.132" rej ...

  8. PHP定界符 heredoc

    <?php echo <<<EOT //如果这个后面有空格,报错... haha EOT; //如果这个后面有空格,报错[如果没有空格,就这样文件直接结束,同样报错,请在EOT ...

  9. object-c(oc)内存管理机制详解

    1.内存的创建和释放 让我们以Object-c世界中最最简单的申请内存方式展开,谈谈关于一个对象的生命周期.首先创建一个对象: 1 2 3 //“ClassName”是任何你想写的类名,比如NSStr ...

  10. css的一些小技巧!页面视觉差!

    相当长的一段时间,现在网站与所谓的“视差”效果一直很受欢迎. 万一你没有听说过这种效果,不同的图像,在不同的方向移动或层主要包括.这导致了一个很好的光学效应,使参观者的注意. 在网页设计中,为了实现这 ...