HDU 1536 sg函数

Posted 半根毛线code

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
题意: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 }

 

以上是关于HDU 1536 sg函数的主要内容,如果未能解决你的问题,请参考以下文章

hdu-1536 S-Nim SG函数

HDU1536:S-Nim(sg函数)

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

HDU1536&&POJ2960 S-Nim(SG函数博弈)

hdu1848(sg函数打表)

博弈总结