您的位置:首页 > 其它

HDOJ 1536 S-Nim SG函数求法

2014-04-19 22:57 246 查看


S-Nim

Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 4040 Accepted Submission(s): 1744



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

Recommend

LL | We have carefully selected several similar problems for you: 1404 1517 1524 1729 1079

第二道SG函数练手题 ^_^

我首先自己写了个求SG函数的代码,把所有可能的取值都算了。TLE后把一个数组改小(一开始没理解SG函数求法,100就够的数组开到了10000)后AC。
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

int m,n,p;
int k[100],sg[10100];

int main()
{
int i,j;
while (cin>>m && m){
for (i=0;i<m;++i)
cin>>k[i];
sort(k,k+i);
k[m]=0x3f3f3f3f;
memset(sg,0,sizeof(sg));
for (i=1;i<=10100;++i){
int f[101]={0}; //f[10100]={0}; TLE
for (j=0;k[j]<=i;++j)
f[sg[i-k[j]]]=1;
for (j=0;f[j];++j);
sg[i]=j;
}

cin>>m;
while (m--){
cin>>n; j=0;
while (n--){
cin>>i;
j^=sg[i];
}
if (j) cout<<'W';
else cout<<'L';
}
cout<<endl;
}
return 0;
}


第一次TLE以后以为是SG函数有更优化的求法,于是去百度。发现别人的题解。仅仅是用到哪个数求哪个数的SG值,利用递归,代码非常巧妙。我一开始没有想到递归的解法。
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

int m,n,p;
int k[100],sg[10100];

int mex(int n){
int i;
bool g[101]={0};
for (i=0;n-k[i]>=0;++i){
if (sg[n-k[i]]==-1)
sg[n-k[i]]=mex(n-k[i]);
g[sg[n-k[i]]]=1;
}
for (i=0;g[i];++i);
return i;
}

int main()
{
int i,j;
while (cin>>m && m){
for (i=0;i<m;++i)
cin>>k[i];
sort(k,k+i);
k[m]=0x3f3f3f3f;
memset(sg,-1,sizeof(sg));
cin>>m;
while (m--){
cin>>n; j=0;
while (n--){
cin>>i;
if (sg[i]==-1)
sg[i]=mex(i);
j^=sg[i];
}
if (j) cout<<'W';
else cout<<'L';
}
cout<<endl;
}
return 0;
}




效率方面,首先无视最早的那个TLE +_+
即使第二种解法有着递归调用时间的开支,时间上还是比第一种更优。但是差距并不明显(说不定600ms主要是cin耗时? 已证实加优化后变为140ms)

kdwycz的网站: http://kdwycz.com/

kdwyz的刷题空间:http://blog.csdn.net/kdwycz
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: