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. 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

Norgesmesterskapet 2004

题意: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=1000000007;
25 const int MOD1=1000000007;
26 const int MOD2=1000000009;
27 const double EPS=0.00000001;
28 typedef __int64 ll;
29 const ll MOD=1000000007;
30 const int INF=1000000010;
31 const ll MAX=1ll<<55;
32 const double eps=1e-8;
33 const double inf=~0u>>1;
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[10005];
40 int a[105];
41 int flag[105];
42 int q,m,exm;
43 void init()
44 {
45     sg[0]=0;
46     for(int i=1;i<=10000;i++)
47     {
48         memset(flag,0,sizeof(flag));
49         for(int j=1;j<=k;j++)
50         {
51             if(i-a[j]>=0)
52             {
53               flag[sg[i-a[j]]]=1;
54             }
55         }
56         for(int j=0;;j++)
57         {
58             if(flag[j]==0){
59                 sg[i]=j;
60             break;
61                 }
62         }
63     }
64 }
65 int main()
66 {
67   while(scanf("%d",&k)!=EOF)
68   {
69       if(k==0)
70         break;
71       for(int i=1;i<=k;i++)
72         scanf("%d",&a[i]);
73        init();
74        scanf("%d",&q);
75        for(int i=1;i<=q;i++)
76        {
77            scanf("%d",&m);
78            int ans=0;
79            for(int j=1;j<=m;j++)
80            {
81                scanf("%d",&exm);
82                ans^=sg[exm];
83            }
84            if(ans==0)
85             printf("L");
86            else
87             printf("W");
88        }
89        printf("\n");
90   }
91  return 0;
92 }
时间: 2024-11-05 06:10:54

HDU 1536 sg函数的相关文章

hdu 2999 sg函数(简单博弈)

Stone Game, Why are you always there? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 393    Accepted Submission(s): 132 Problem Description "Alice and Bob are playing stone game...""E

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

Good Luck in CET-4 Everybody! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8634    Accepted Submission(s): 5587 Problem Description 大 学英语四级考试就要来临了,你是不是在紧张的复习?也许紧张得连短学期的ACM都没工夫练习了,反正我知道的Kiki和C

hdu 1847 sg函数

Good Luck in CET-4 Everybody! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5332    Accepted Submission(s): 3426 Problem Description 大学英语四级考试就要来临了,你是不是在紧张的复习?也许紧张得连短学期的ACM都没工夫练习了,反正我知道的Kiki和Ci

hdu 1848(SG函数)

Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7663    Accepted Submission(s): 3205 Problem Description 任何一个大学生对菲波那契数列(Fibonacci numbers)应该都不会陌生,它是这样定义的:F(1)=1;F(2)=2;

HDU 1848 SG函数博弈

Fibonacci again and again Problem Description 任何一个大学生对菲波那契数列(Fibonacci numbers)应该都不会陌生,它是这样定义的:F(1)=1;F(2)=2;F(n)=F(n-1)+F(n-2)(n>=3);所以,1,2,3,5,8,13……就是菲波那契数列.在HDOJ上有不少相关的题目,比如1005 Fibonacci again就是曾经的浙江省赛题.今天,又一个关于Fibonacci的题目出现了,它是一个小游戏,定义如下:1.  这

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