hdu 1536

S-Nim

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

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

记忆化求sg,直接打表会tle

 1 #include<iostream>
 2 #include<string>
 3 #include<cstdio>
 4 #include<vector>
 5 #include<queue>
 6 #include<stack>
 7 #include<set>
 8 #include<algorithm>
 9 #include<cstring>
10 #include<stdlib.h>
11 #include<math.h>
12 using namespace std;
13 #define ll __int64
14 int sg[11000],ss[110],k;
15 int getsg(int n){
16         int vit[110];
17         memset(vit,0,sizeof(vit));
18         for(int j=0;j<k&&ss[j]<=n;j++){
19              if(sg[n-ss[j]]==-1) sg[n-ss[j]]=getsg(n-ss[j]);
20              vit[sg[n-ss[j]]]=1;
21         }
22         for(int j=0;;j++)
23         if(!vit[j]) return j;
24 }
25 int main(){
26     while(scanf("%d",&k)&&k){
27         for(int i=0;i<k;i++) scanf("%d",&ss[i]);
28         sort(ss,ss+k);
29         int m;scanf("%d",&m);
30         memset(sg,-1,sizeof(sg));
31         sg[0]=0;
32         while(m--){
33             int ans=0,t;scanf("%d",&t);
34             while(t--){
35                 int tt;scanf("%d",&tt);
36                 if(sg[tt]==-1) sg[tt]=getsg(tt);
37                 ans^=sg[tt];
38             }
39             if(ans==0) printf("L");
40             else printf("W");
41         }
42         printf("\n");
43     }
44 }
时间: 2024-10-04 19:14:31

hdu 1536的相关文章

hdu 1536 S-Nim 博弈论,,求出SG&#39;函数就可以解决

S-Nim Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4975    Accepted Submission(s): 2141 Problem Description Arthur and his sister Caroll have been playing a game called Nim for some time now

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1944 http://acm.hdu.edu.cn/showproblem.php?pid=1536 给定每一次可以取的石头数,给定很多种情况,每一种情况有若干堆石头,判断先手胜负. SG函数打表,然后直接抑或,判断结果是否为0,第一次写SG函数,贴个代码,慢慢理解. 代码: /* *********************************************** Author :rabb

HDU 1536 S-Nim 求SG函数

题意:给你n个数Nnum[ i ],表示每次只能取Nnum[ i ]个数. m个问题:每次给你 l 堆石子,每堆有num个石子,问先手是否会赢. 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 经典Nim游戏,找出SG就可以了. 至于如何找SG,这里有详细的 点我 #include<cstdio> #include<

hdu 1536 S-Nim|| poj 2960 S-Nim (sg函数)

#include <stdio.h> #include <string.h> int s[110]; int sg[10010],hash[110]; int n, m; int getsg(int x) //sg模板 { int i; if(sg[x] != -1) return sg[x]; memset(hash,0,sizeof(hash)); for(i = 0; i < n; i++) { if(x >= s[i]) { sg[x - s[i]] = get

HDU 1536 &amp; 1944

http://acm.hdu.edu.cn/showproblem.php?pid=1536 http://acm.hdu.edu.cn/showproblem.php?pid=1944 一样的题 题意:先给一个集合,代表可能发生的转移.然后m个询问,可以理解为每次给l堆石子,每堆有hi个,问博弈策略 直接用sg定理,非常简单,转移给的清清楚楚,照着写就行,递推或者记忆化搜索都行. 这题的时间卡的紧,开始死活过不了,看别人代码把vis数组开成100就过了(原来开的1w),深感此题有问题,sg的值

HDU 1536——S-nim博弈

题目: 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 t

hdu 1536 NIM博弈 (模板)

推荐文章 博弈论初步:http://www.cnblogs.com/Knuth/archive/2009/09/05/1561002.html 博弈解决思想:http://www.cnblogs.com/Knuth/archive/2009/09/05/1561005.html NIM游戏:http://www.cnblogs.com/Knuth/archive/2009/09/05/1561008.html 关于SG函数:http://www.cnblogs.com/Knuth/archive

HDU 1536 sg-NIM博弈类

题意:每次可以选择n种操作,玩m次,问谁必胜.c堆,每堆数量告诉. 题意:sg—NIM系列博弈模板题 把每堆看成一个点,求该点的sg值,异或每堆sg值. 将多维转化成一维,性质与原始NIM博弈一样. 1 // #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #

hdu 1536 S-Nim (简单sg函数)

题意:首先输入K 表示一个集合的大小  之后输入集合 表示对于这对石子只能去这个集合中的元素的个数 之后输入 一个m 表示接下来对于这个集合要进行m次询问 之后m行 每行输入一个n 表示有n个堆  每堆有n1个石子  问这一行所表示的状态是赢还是输 如果赢输入W否则L 思路:sg打表一下 #include <iostream> #include <cstring> using namespace std; const int maxn=10008; int f[maxn],n;//

HDU 1536 sg函数

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.