您的位置:首页 > 其它

poj2960【Nim博弈,SG】

2012-05-05 00:25 281 查看
今天是5.4青年节,给自己放假。



S-Nim

Time Limit: 2000MSMemory Limit: 65536K
Total Submissions: 1939Accepted: 1092
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

Svenskt M?sterskap i Programmering/Norgesmesterskapet 2004

/*

题意:给出n堆石子,各堆石子数ni,每次能取其中一堆的ki个,最后没法取的为输,问先取的人赢还是输。(例如轮到你取时,只剩下2个,但每次只能取3个或5个,那你就输了)。
==========================1==========================
1、只考虑一堆:
有sg函数(i>=0):
1)sg[i]=0;(i=0);
2)sg[i]=min{a|a not in{sg[i-restrict[j]],j=1..k},a belongs to 0..+infinite}(i>0)(也就是如果z<sg[i],那么有合法的取法可以剩下i`个,使得sg[i`]=z)。
例如:每次只能取3个或5个,则sg[0]=0;sg[1]=0;sg[2]=0;sg[3]=1;sg[4]=1;sg[5]=1;sg[6]=2;

现在

看sg[7],因为sg[7-3]=1,sg[7-5]=0,所以sg[7]=2;(min{a|a not in {0,1},a belongs to 0..+inf},即2)

看sg[8],因为sg[8-3]=1,sg[8-5]=1,所以sg[8]=0;(min {a|a not in {1},a belongs to 0..+inf},即0)

看sg[9],因为sg[9-3]=2,sg[9-5]=1,所以sg[9]=0;(min {a|a not in {1,2},a belongs to 0..+infinite},即0)

=============================2=============================
2、The Sprague-Grundy Theorem:SG(x1,x2,..xn)=sg[x1]^sg[x2]^...^sg[xn];
赢<==>SG(x1,x2...xn)>0;
输<==>SG(x1,x2...xn)=0;

证明:let b=SG(x1,x2,...xn);
先看2个命题:
1)对于任意a(0<=a<b),存在取法把SG(x1,x2..xi,..xn)变成SG(x1,x2,..,xi`,..xn)=a
2) 不存在合法取法把SG(x1,x2..xi,..xn)变成SG(x1,x2,..,xi`,..xn)=b
对于2)SG(x1,x2,..,xi,...xn)=sg[x1]^sg[x2]^...sg[xi]...^sg[xn]和SG(x1,x2,..,xi`,...xn)=sg[x1]^sg[x2]^...sg[xi`]...^sg[xn];
且两个SG值相等,有异或的消去率(即a^b=c^b==>a=c),得xi=xi`
对于1):
因为a<b,所以从高到底找b和a的二进制表示数中第一位在b中为1而a中为0的位,就当是第k位(例如b=10011,a=10000,那么k就是红色那位),由异或运算得知第k位那个1肯定可以在sg[x1],sg[x2]...sg[xn]中找到,就当是在sg[x1]中的第k位也是1。现在要在x1个中拿掉一些变成x1`个,问x1`存在不存在(问题已经变为只有一堆的情形)。
记a=z^sg[x2]^...^sg[xn]),如果可以证明z<sg[x1](即有合法取法把那堆取成剩下x1`个)那就表明存在such x1`(由1的2)的红色部分得知)。
写一下:
b=sg[x1]^sg[x2]^...^sg[xn]
a=z^sg[x2]^...^sg[xn]
有a^b=sg[x1]^z;所以z=(b^a)^sg[x1]<sg[x1](因为b^a包含上面说的第k位,例如:b=10011,a=10000,b^a=00011(第k位左边的都是0),而sg[x1]的第k位为1,所以(b^a)^sg[x1]就是把sg[x1]第k位变为0,而第k位左边的不变,不论右边的怎么变都<原来的sg[x1],即(b^a)^sg[x1]<sg[x1],z<sg[x1]).


=======================================结论====================================
也就是
由1)当SG(x1,x2...xn)>0是总有办法通过合法的取法变成比SG(x1,x2...xn)小的状态,当然可以变为0;
而由2)SG(x1,x2...xn)=0却没有办法变成值为0的状态,所以不论怎么取只能变成比SG(x1,x2...xn)更大的状态。
于是就有了:

赢<==>SG(x1,x2...xn)>0;
输<==>SG(x1,x2...xn)=0;

*/

/*
有一大块是调试用的....input1()就是。代码不长的。
*/

#include<iostream>
#include<fstream>
using namespace std;

int k;
int sg[20001];
int restrict[201];//sg[i]=0;(i=0);sg[i]=min{a|a not in{sg[i-restrict[j]],j=1..k},a belongs to 0..+infinte}(i>0)

int SG(int kk)//return sg[kk]
{
if(kk==0) return 0;
if(sg[kk]!=-1) return sg[kk];

bool visit[20001];
memset(visit,0,sizeof(visit));//memorize

int i;
for(i=0;i<k;i++)//exit
if(kk-restrict[i]>=0) visit[SG(kk-restrict[i])]=1;

for(i=0;;i++) if(!visit[i]) return sg[kk]=i;//find sg[kk]
}

void input1(ifstream &in)
{
int i,j,set,k,m,n;
while(in>>k,k)
{
cout<<k;
for(i=0;i<k;i++)
{
in>>m;
cout<<m;
}
cout<<endl;

in>>set;
cout<<set<<endl;
for(i=0;i<set;i++)
{
in>>n;
cout<<n;
for(j=0;j<n;j++)
{
in>>m;
cout<<m;
}
cout<<endl;
}
}
cout<<0<<endl;
}

int main()
{
int i,j,set,m,n,ans;

ifstream input("C:\\Users\\Administrator\\Desktop\\output.txt");

//input1(input);
//return 0;
//cout<<sizeof(sg)<<endl;
while(cin>>k,k!=0)
{
memset(sg,-1,sizeof(sg));
for(i=0;i<k;i++) scanf("%d",&restrict[i]);
/*
for(i=0;i<k;i++) {
//getchar();
cout<<restrict[i]<<' ';
}
cout<<endl;
*/
scanf("%d",&set);
for(i=0;i<set;i++)
{
scanf("%d",&n);
ans=0;
for(j=0;j<n;j++)
{
scanf("%d",&m);
ans^=SG(m);
//for(int p=0;p<=m;p++) cout<<"SG("<<p<<")="<<SG(p)<<endl;
}
ans>0?putchar('W'):putchar('L');

}
puts("");
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: