S-Nim

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7262    Accepted Submission(s): 3074

Problem Description
Arthur and his sister Caroll have been playing a game called Nim for some time now. Nim is played as follows:

The starting position has a number of heaps, all containing some, not necessarily equal, number of beads.

The players take turns chosing a heap and removing a positive number of beads from it.

The first player not able to make a move, loses.

Arthur and Caroll really enjoyed playing this simple game until they recently learned an easy way to always be able to find the best move:

Xor the number of beads in the heaps in the current position (i.e. if we have 2, 4 and 7 the xor-sum will be 1 as 2 xor 4 xor 7 = 1).

If the xor-sum is 0, too bad, you will lose.

Otherwise, move such that the xor-sum becomes 0. This is always possible.

It is quite easy to convince oneself that this works. Consider these facts:

The player that takes the last bead wins.

After the winning player's last move the xor-sum will be 0.

The xor-sum will change after every move.

Which means that if you make sure that the xor-sum always is 0 when you have made your move, your opponent will never be able to win, and, thus, you will win.

Understandibly it is no fun to play a game when both players know how to play perfectly (ignorance is bliss). Fourtunately, Arthur and Caroll soon came up with a similar game, S-Nim, that seemed to solve this problem. Each player is now only allowed to remove a number of beads in some predefined set S, e.g. if we have S =(2, 5) each player is only allowed to remove 2 or 5 beads. Now it is not always possible to make the xor-sum 0 and, thus, the strategy above is useless. Or is it?

your job is to write a program that determines if a position of S-Nim is a losing or a winning position. A position is a winning position if there is at least one move to a losing position. A position is a losing position if there are no moves to a losing position. This means, as expected, that a position with no legal moves is a losing position.

 
Input
Input consists of a number of test cases. For each test case: The first line contains a number k (0 < k ≤ 100 describing the size of S, followed by k numbers si (0 < si ≤ 10000) describing S. The second line contains a number m (0 < m ≤ 100) describing the number of positions to evaluate. The next m lines each contain a number l (0 < l ≤ 100) describing the number of heaps and l numbers hi (0 ≤ hi ≤ 10000) describing the number of beads in the heaps. The last test case is followed by a 0 on a line of its own.
 
Output
For each position: If the described position is a winning position print a 'W'.If the described position is a losing position print an 'L'. Print a newline after each test case.
 
Sample Input
2 2 5
3
2 5 12
3 2 4 7
4 2 3 7 12
5 1 2 3 4 5
3
2 5 12
3 2 4 7
4 2 3 7 12
0
 
Sample Output
LWW
WWL
 
Source
题意:m堆石子  玩家每次可以从某一堆中取出si个石子 不能取则输
题解:初步学习sg函数 sg[i]为 i的后继状 的sg值中 没有出现过的非负最小值。
sg异或值为0则后手胜
  1. /******************************
  2. code by drizzle
  3. blog: www.cnblogs.com/hsd-/
  4. ^ ^ ^ ^
  5. O O
  6. ******************************/
  7. #include<bits/stdc++.h>
  8. #include<map>
  9. #include<set>
  10. #include<cmath>
  11. #include<queue>
  12. #include<bitset>
  13. #include<math.h>
  14. #include<vector>
  15. #include<string>
  16. #include<stdio.h>
  17. #include<cstring>
  18. #include<iostream>
  19. #include<algorithm>
  20. #pragma comment(linker, "/STACK:102400000,102400000")
  21. using namespace std;
  22. #define A first
  23. #define B second
  24. const int mod=;
  25. const int MOD1=;
  26. const int MOD2=;
  27. const double EPS=0.00000001;
  28. typedef __int64 ll;
  29. const ll MOD=;
  30. const int INF=;
  31. const ll MAX=1ll<<;
  32. const double eps=1e-;
  33. const double inf=~0u>>;
  34. const double pi=acos(-1.0);
  35. typedef double db;
  36. typedef unsigned int uint;
  37. typedef unsigned long long ull;
  38. int k;
  39. int sg[];
  40. int a[];
  41. int flag[];
  42. int q,m,exm;
  43. void init()
  44. {
  45. sg[]=;
  46. for(int i=;i<=;i++)
  47. {
  48. memset(flag,,sizeof(flag));
  49. for(int j=;j<=k;j++)
  50. {
  51. if(i-a[j]>=)
  52. {
  53. flag[sg[i-a[j]]]=;
  54. }
  55. }
  56. for(int j=;;j++)
  57. {
  58. if(flag[j]==){
  59. sg[i]=j;
  60. break;
  61. }
  62. }
  63. }
  64. }
  65. int main()
  66. {
  67. while(scanf("%d",&k)!=EOF)
  68. {
  69. if(k==)
  70. break;
  71. for(int i=;i<=k;i++)
  72. scanf("%d",&a[i]);
  73. init();
  74. scanf("%d",&q);
  75. for(int i=;i<=q;i++)
  76. {
  77. scanf("%d",&m);
  78. int ans=;
  79. for(int j=;j<=m;j++)
  80. {
  81. scanf("%d",&exm);
  82. ans^=sg[exm];
  83. }
  84. if(ans==)
  85. printf("L");
  86. else
  87. printf("W");
  88. }
  89. printf("\n");
  90. }
  91. return ;
  92. }

HDU 1536 sg函数的更多相关文章

  1. hdu 1536 SG函数模板题

    S-Nim Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  2. hdu 2147 SG函数打表(手写也可以) 找规律

    kiki's game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 40000/1000 K (Java/Others) Total ...

  3. hdu 1848(SG函数)

    Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  4. hdu 1847(SG函数,巴什博弈)

    Good Luck in CET-4 Everybody! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  5. HDU 1848 SG函数博弈

    Fibonacci again and again Problem Description   任何一个大学生对菲波那契数列(Fibonacci numbers)应该都不会陌生,它是这样定义的:F(1 ...

  6. hdu 2999 sg函数(简单博弈)

    Stone Game, Why are you always there? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/ ...

  7. hdu 1536 sg (dfs实现)

    S-Nim Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  8. SG 函数初步 HDU 1536 &amp;&amp; HDU 1944

    题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=1944 pid=1536"> http://acm.hdu.edu.cn/showpr ...

  9. S-Nim HDU 1536 博弈 sg函数

    S-Nim HDU 1536 博弈 sg函数 题意 首先输入K,表示一个集合的大小,之后输入集合,表示对于这对石子只能去这个集合中的元素的个数,之后输入 一个m表示接下来对于这个集合要进行m次询问,之 ...

随机推荐

  1. UITableView优化的方法

    使用不透明视图. 不透明的视图可以极大地提高渲染的速度.因此如非必要,可以将table cell及其子视图的opaque属性设为YES(默认值). 其中的特例包括背景色,它的alpha值应该为1(例如 ...

  2. Python学习笔记(0)

    Python 是什么类型的语言 Python是脚本语言 Python下载地址:https://www.python.org/downloads/ Python版本:Python 3.4.2 - 64b ...

  3. Java使用BigDecimal精确计算的简单公式计算器

    由于工作需要,写了一个使用BigDecimal运算的精确计算的计算器(然后发现其实比不用BigDecimal的并好不到哪里去) 只能做加减乘除 double类型的数字在千万级别的时候会转成科学计数法, ...

  4. 《BI那点儿事》数据流转换——透视

    这个和T-SQL中的PIVOT和UNPIVOT的作用是一样的.数据透视转换可以将数据规范或使它在报表中更具可读性. 通过透视列值的输入数据,透视转换将规范的数据集转变成规范程度稍低.但更为简洁的版本. ...

  5. iOS - Xcode 常用快捷键

    Xcode 常用快捷键 1)文件: command + shift + n 新建项目 command + n 新建文件 command + control + n 新建空文件 command + o ...

  6. 16 SQL Tuning Overview

    16.1 Introduction to SQL Tuning Identifying high load or top SQL statements that are responsible for ...

  7. 【UFLDL】Exercise: Convolutional Neural Network

    这个exercise需要完成cnn中的forward pass,cost,error和gradient的计算.需要弄清楚每一层的以上四个步骤的原理,并且要充分利用matlab的矩阵运算.大概把过程总结 ...

  8. Evolutionary Computing: 3. Genetic Algorithm(2)

    承接上一章,接着写Genetic Algorithm. 本章主要写排列表达(permutation representations) 开始先引一个具体的例子来进行表述 Outline 问题描述 排列表 ...

  9. 基于讯为4412开发板的Android开发流程

    讯为4412开发板  使用三星2410芯片,基于arm9架构,由于自己电脑硬件的局限,只能跑Android4.0.3系统. 1.Uboot这个直接使用官方镜像烧写就可以了,一般情况不用去重复烧写. 略 ...

  10. cookie预:

    什么是cookie? cookie 是存储于访问者的计算机中的变量.每当同一台计算机通过浏览器请求某个页面时,就会发送这个 cookie.你可以使用 JavaScript 来创建和取回 cookie ...