S-Nim

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 4091    Accepted Submission(s): 1760

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
 


这也是一道经典SG函数的题目。
有关于SG函数的解,能够戳这个,非常具体→http://blog.csdn.net/lttree/article/details/24886205
这道题题意:
我就按着例子格式来说吧:
先输入一个K,表示取数集合的个数。(K为0,则结束)
后面跟k个数,表示取数集合的数(就是每次仅仅能取这几个数量的物品)
然后会跟一个M,表示有M次询问。
然后接下来M行,每行先有一个N,表示有多少堆物品。
N后跟着N个数,表示每堆物品数量。

由于,OJ后台的操作,输入和输出是分开的(事实上就是将你的程序的答案存成一个TXT文件,然后和
标准答案TXT文件进行二进制的比較)
所以,我每一个N都直接输出'L'或者'W‘,
在M行结束时,换行,没实用数组来存答案。
PS:用scanf比cin快80MS


/************************************************
*************************************************
* Author:Tree *
*From :http://blog.csdn.net/lttree *
* Title : S-Nim *
*Source: hdu 1536 *
* Hint : SG *
*************************************************
*************************************************/
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define N 10001
int f[N],sg[N];
bool mex[N];
void get_sg(int t,int n)
{
int i,j;
memset(sg,0,sizeof(sg));
for(i=1;i<=n;i++)
{
memset(mex,0,sizeof(mex));
// 对于属于g(x)后继的数置1
for( j=1 ;j<=t && f[j]<=i ;j++ )
mex[sg[i-f[j]]]=1;
// 找到最小不属于该集合的数
for( j=0 ; j<=n ; j++ )
if(!mex[j])
break;
sg[i] = j;
}
}
int main()
{
int k,m,n,i,t,temp;
while( scanf("%d",&k) && k )
{
for(i=1;i<=k;++i)
scanf("%d",&f[i]);
sort(f+1,f+k+1);
get_sg(k,N);
scanf("%d",&m);
while(m--)
{
temp=0;
scanf("%d",&n);
for(i=0;i<n;++i)
{
scanf("%d",&t);
temp^=sg[t];
}
if( !temp ) printf("L");
else printf("W");
}
printf("\n");
}
return 0;
}

ACM-SG函数之S-Nim——hdu1536 hdu1944 poj2960的更多相关文章

  1. 最浅谈的SG函数

    [更新] Nim游戏的经验: 每次最多取m个——%(m+1) 阶梯nim——奇数位无视,看偶数位互相独立,成一堆一堆的石子 . . . . 既然被征召去汇总算法..那么挑个简单点的SG函数好了.. 介 ...

  2. hdu1536&&hdu3023 SG函数模板及其运用

    S-Nim Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status ...

  3. HDU1536:S-Nim(sg函数)

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

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

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

  5. Nim 博弈和 sg 函数

    sg 函数 参考 通俗易懂 论文 几类经典的博弈问题 阶梯博弈: 只考虑奇数号楼梯Nim,若偶数楼梯只作容器,那么游戏变为Nim.题目 翻转硬币: 局面的SG值为局面中每个正面朝上的棋子单一存在时的S ...

  6. hdu-1536 S-Nim SG函数

    http://acm.hdu.edu.cn/showproblem.php?pid=1536 给出能够取的方法序列,然后求基本石子堆问题. 只要用S序列去做转移即可. 注意has初始化的一些技巧 #i ...

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

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

  8. HDU 3032 Nim or not Nim? (sg函数)

    Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  9. [您有新的未分配科技点]博弈论入门:被博弈论支配的恐惧(Nim游戏,SG函数)

    今天初步学习了一下博弈论……感觉真的是好精妙啊……希望这篇博客可以帮助到和我一样刚学习博弈论的同学们. 博弈论,又被称为对策论,被用于考虑游戏中个体的预测行为和实际行为,并研究他们的应用策略.(其实这 ...

随机推荐

  1. python学习之老男孩python全栈第九期_day028知识点总结——面向对象进阶、hashlib

    一. 面向对象进阶与实例 dic = {'k': 'v' } 对象:存储 属性 和 调用方法 dic['k'] = 'v' class Foo: def __init__(self, name, ag ...

  2. jQuery自适应-3D旋转轮播图

    3D旋转轮播图 本例源于(站长之家实例http://sc.chinaz.com/jiaoben/170215391070.htm) 其他相似示例(https://www.cnblogs.com/inc ...

  3. BZOJ2960:跨平面

    题面 BZOJ Sol 对该平面图的对偶图建图后就是最小树形图,建一个超级点向每个点连 \(inf\) 边即可 怎么转成对偶图,怎么弄出多边形 把边拆成两条有向边,分别挂在两个点上 每个点的出边按角度 ...

  4. 普通平衡树Tyvj1728、luogu P3369 (treap)

    您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删除一个) 查询x数的排名(若有多个相同的数,因输出最小的排名) 查询排名为x的 ...

  5. 03--CSS布局设置

    一 盒模型 盒模型 在CSS中,"box model"这一术语是用来设计和布局时使用,然后在网页中基本上都会显示一些方方正正的盒子.我们称为这种盒子叫盒模型. 盒模型有两种:标准模 ...

  6. Android解析WindowManagerService(二)WMS的重要成员和Window的添加过程

    前言 在本系列的上一篇文章中,我们学习了WMS的诞生,WMS被创建后,它的重要的成员有哪些?Window添加过程的WMS部分做了什么呢?这篇文章会给你解答. 1.WMS的重要成员 所谓WMS的重要成员 ...

  7. EXCHANGE 2013 队列

    每当咱在Exchange里查看队列的时候,我们会看到队列分成好几个组,每个邮箱数据库都有自己的目标队列,DAG.AD站点也是,AD林也是一个队列,最后最多的就是外部SMTP域队列. 当传输服务处理队列 ...

  8. python windows 远程执行bat

    本机环境:Win 10,python3.6 远程机器: Win7.WinServer 因python在windows上执行需要用到 wmi 模块. wmi 模块下载地址:https://sourcef ...

  9. August 30th 2017 Week 35th Wednesday

    A lion does not concern himself with the opinion of sheep. 狮子可不会在意绵羊是怎么想的. As a sheep, you must run ...

  10. 43、ThreadPool 、WaitHandle、原子操作InterLocked

    ThreadPool 创建线程需要时间.如果有不同的小任务要完成,就可以事先创建许多线程/在应完成这些任务时发出请求.不需要自己创建这样一个列表. 该列表由ThreadPool类托管.这个类会在需要时 ...