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

S-Nim

Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

Submit Status

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

经典的Nim游戏题目中已经给出了——每一堆选取的数量没有限制。

S-Nim游戏仅仅是限制了每一次从每一堆中选取的个数,依旧用sg函数计算即可。

经典的Nim游戏中sg(x) = x,所以结果就是每一堆的状态直接xor即可,S-Nim游戏先计算每一堆的sg函数值,然后判断方法依旧是用xor。

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 102
int n,m,l,s[N],sg[10005],ans,a;
bool vis[10005];
void c()
{
    sg[0]=0;
    for(int i=1;i<=10000;i++)
    {
        memset(vis,0,sizeof(vis));
        for(int j=0;j<n&&s[j]<=i;j++)
        vis[sg[i-s[j]]]=1;
        for(int x=0;x<=10000;x++)
            if(!vis[x])
        {
            sg[i]=x;
            break;
        }
    }
}
int main()
{
    while(cin>>n&&n)
    {
        for(int i=0;i<n;i++)
            cin>>s[i];
        sort(s,s+n);
        c();
        cin>>m;
        while(m--)
        {
            ans=0;
            cin>>l;
            while(l--)
            {
                cin>>a;
                ans^=sg[a];
            }
            if(ans)
                cout<<"W";
            else
                cout<<"L";
        }
        cout<<endl;
    }
    return 0;
}

对sg函数的理解:

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 102
int n,m,l,s[N],sg[10005],ans,a;
bool vis[10005];
void c()
{
    sg[0]=0;
    for(int i=1;i<=10000;i++)
    {
        memset(vis,0,sizeof(vis));
        for(int j=0;j<n&&s[j]<=i;j++)
        vis[sg[i-s[j]]]=1; //里面的sg[]是sg[i]的后继 vis[sg[]] 指的是求mex
        for(int x=0;x<=10000;x++)
            if(!vis[x])
        {
            sg[i]=x;
            break;
        }
    }
}
int main()
{
    while(cin>>n&&n)
    {
        for(int i=0;i<n;i++)
            cin>>s[i];
        sort(s,s+n);
        c();
        for(int i=0;i<30;i++)
        cout<<"i: "<<i<<"  sg[i]: "<<sg[i]<<endl;
        /*cin>>m;
        while(m--)
        {
            ans=0;
            cin>>l;
            while(l--)
            {
                cin>>a;
                ans^=sg[a];
            }
            if(ans)
                cout<<"W";
            else
                cout<<"L";
        }
        cout<<endl;*/
    }
    return 0;
}

让我们再来考虑一下顶点的SG值的意义。当g(x)=k时,表明对于任意一个0<=i<k,都存在x的一个后继y满足g(y)=i。

也就是说,当某枚棋子的SG值是k时,我们可以把它变成0、变成1、……、变成k-1,但绝对不能保持k不变。

不知道你能不能根据这个联想到Nim游戏,Nim 游戏的规则就是:每次选择一堆数量为k的石子,

可以把它变成0、变成1、……、变成k-1,但绝对不能保持k不变。这表明,如果将n枚棋子所在的顶点的 SG值看作n

堆相应数量的石子,那么这个Nim游戏的每个必胜策略都对应于原来这n枚棋子的必胜策略!

时间: 2024-11-05 22:04:05

HDU1536&&POJ2960 S-Nim(SG函数博弈)的相关文章

【POJ2960】S-Nim SG函数 博弈 裸题模板题

转载请注明出处:http://blog.csdn.net/vmurder/article/details/42653601 其实我就是觉得原创的访问量比未授权盗版多有点不爽233... 题意: 两人轮流从若干堆石子中某堆取k个石子, k∈集合S, 就是每次取的数量被限定成某几个数的意思! 然后跟正常Nim一样谁不能操作就输. 题解: SG函数裸题. SG函数: 首先需要是有向无环图(拓扑图) 首先确定边界状态,SG值为0,然后暴力拓扑得出其它点的SG值. SG值为所有子集的SG值中未出现的最小自

HDU 3032 Nim or not Nim?(sg函数博弈)

题目地址:HDU 3032 这题是很好用来练习sg函数打表的一题. 下面是sg函数值打表代码: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queue> #include &

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

题目链接 暴力出来,竟然眼花了以为sg(i) = i啊....看表要认真啊!!! #include <cstdio> #include <cstring> #include <iostream> using namespace std; #define LL __int64 int dp[10001]; int sg(int x) { int flag[10001],temp,i; if(dp[x] >= 0) return dp[x]; memset(flag,

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)Total Submission(s): 1056    Accepted Submission(s): 523 Problem Description Nim is a two-player mathematic game of strategy in which players take turn

HDU1536:S-Nim(sg函数)

S-Nim Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10368    Accepted Submission(s): 4262 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1536 Descripion: Arthur and his sister Caroll have been

hdu1848 Fibonacci again and again(SG函数博弈)

现在换是看不明白SG函数的求法什么的 暂时先当模板题吧 函数mex1就是求g(x) 然后异或 #include<cstdio> #include<iostream> #include<algorithm> #include<cstring> using namespace std; int k,fibo[100],f[10001]; int mex1(int p){ int i,t; bool g[101]={0}; for(i=0;i<k;i++){

HDU 1729 Stone Game 石头游戏 (Nim, sg函数)

题意:有n个盒子,每个盒子可以放一定量的石头,盒子中可能已经有了部分石头.假设石头无限,每次可以往任意一个盒子中放石头,可以加的数量不得超过该盒中已有石头数量的平方k^2,即至少放1个,至多放k^2个. 思路:跟常规nim的区别就是加了个限制“每次加的量不超平方”.盒子容量上限是100万,那么就不能直接计算SG了,会超时.sg打表后找规律.根据剩下多少个空位来决定sg值.都是0123456这样子递增的,碰到不能一次加满就变为0,然后继续递增,一直这样. 我的方案是,对于每个盒子大小,找到除了自己

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) Total Submission(s): 1099    Accepted Submission(s): 547 Problem Description Nim is a two-player mathematic game of strategy in which players take tur

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