S-Nim

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8729    Accepted Submission(s): 3660

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
 

题意

首先给出k,表示有几种每次取石子个数的集合,即给出123,每次可以取1,2,3个。然后给出询问次数m。每次询问给出n堆石子,然后询问当前状态是P还是N。

分析

SG函数。两种求法,都写了一遍。耗时间差不多

code

预处理

 #include<cstdio>
#include<cstring>
#include<algorithm> using namespace std; const int N = ;
int sg[],f[];
int k,n,m;
bool Hash[]; void get_SG() {
memset(sg,,sizeof(sg));
for (int i=; i<=N; ++i) {
memset(Hash,false,sizeof(Hash));
for (int j=; j<=k&&f[j]<=i; ++j)
Hash[sg[i-f[j]]] = true;
for (int j=; j<=N; ++j)
if (!Hash[j]) {sg[i] = j;break;}
}
} int main () {
while (~scanf("%d",&k) && k) {
for (int i=; i<=k; ++i)
scanf("%d",&f[i]);
sort(f+,f+k+);
get_SG();
scanf("%d",&m);
for (int i=; i<=m; ++i) {
scanf("%d",&n);
int ans = ;
for (int t,j=; j<=n; ++j) {
scanf("%d",&t);
ans ^= sg[t];
}
if (ans == ) printf("L");
else printf("W");
}
puts("");
}
return ;
}

dfs记忆化搜索

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; int sg[],f[];
int k,n,m; int get_SG(int x) {
if (sg[x] != -) return sg[x];
bool Hash[]; //不能再外面开
memset(Hash,false,sizeof(Hash));
for (int i=; i<=k; ++i) {
if (f[i] > x) break;
get_SG(x-f[i]);
Hash[sg[x-f[i]]] = true;
}
for (int i=; ; ++i)
if (!Hash[i]) {sg[x] = i;break;}
return sg[x];
}
int main () {
while (~scanf("%d",&k) && k) {
for (int i=; i<=k; ++i)
scanf("%d",&f[i]);
sort(f+,f+k+);
memset(sg,-,sizeof(sg));
sg[] = ;
scanf("%d",&m);
for (int i=; i<=m; ++i) {
scanf("%d",&n);
int ans = ;
for (int t,j=; j<=n; ++j) {
scanf("%d",&t);
ans ^= get_SG(t);
}
if (ans == ) printf("L");
else printf("W");
}
puts("");
}
return ;
}

HDU 1535 S-Nim(SG函数)的更多相关文章

  1. hdu 3032 Nim or not Nim? sg函数 难度:0

    Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  2. hdu 3032 Nim or not Nim? (SG函数博弈+打表找规律)

    Nim or not Nim? Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Sub ...

  3. HDU 5724 Chess(SG函数+状态压缩)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5724 题意: 现在有一个n*20的棋盘,上面有一些棋子,双方每次可以选择一个棋子把它移动到其右边第一 ...

  4. HDU 5724 Chess(SG函数)

    Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  5. hdu 3032(博弈sg函数)

    题意:与原来基本的尼姆博弈不同的是,可以将一堆石子分成两堆石子也算一步操作,其它的都是一样的. 分析:由于石子的堆数和每一堆石子的数量都很大,所以肯定不能用搜索去求sg函数,现在我们只能通过找规律的办 ...

  6. 多校6 1003 HDU5795 A Simple Nim (sg函数)

    思路:直接打表找sg函数的值,找规律,没有什么技巧 还想了很久的,把数当二进制看,再类讨二进制中1的个数是必胜或者必败状态.... 打表: // #pragma comment(linker, &qu ...

  7. hdu 1079 Calendar Game sg函数 难度:0

    Calendar Game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  8. HDU 3032 Nim or not Nim (sg函数)

    加强版的NIM游戏,多了一个操作,可以将一堆石子分成两堆非空的. 数据范围太大,打出sg表后找规律. # include <cstdio> # include <cstring> ...

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

    题意: 有n个盒子,每个盒子可以放一定量的石头,盒子中可能已经有了部分石头.假设石头无限,每次可以往任意一个盒子中放石头,可以加的数量不得超过该盒中已有石头数量的平方k^2,即至少放1个,至多放k^2 ...

随机推荐

  1. LVS 集群工作原理

    1. 集群:集群(cluster )就是一组计算机,它们作为一个整体向用户提供一组网络资源,单个计算机系统就是一个集群节点(node). 2. 集群种类: <1>. 负载均衡集群(Load ...

  2. 从零开始的全栈工程师——js篇2.7(JS数据类型具体分析)

    JS数据类型具体分析与数据的三大存储格式 1. 字符串 string2. 数字 number3. 布尔 boolean4. null 空5. undefined 未定义↑↑↑叫基本数据类型 基本数据类 ...

  3. cf1040E. Network Safety(并查集)

    题意 题目链接 一张图,n个点,m条边,每个点有个权值x,x<=1e18.如果一条边的两个端点不一样,那么这条边是安全的,开始时所有边都是安全的. 现在有一个病毒y,病毒可以入侵任意的点,入侵一 ...

  4. This is your path and you will pursue it with excellence.

    This is your path and you will pursue it with excellence.自己选的路就要走出精彩.

  5. JNI教程

    一.什么是JNI JNI(Java Native Interface ),它是Java SDK的一部分,主要用于实现Java对其他语言编写的代码和库的调用,比如C和C++.JNI提供的API也能让JV ...

  6. restesay部署学习过程中遇到的问题

    Exception starting filter org.jboss.resteasy.plugins.serer.servlet.Filter30Dispatcherjava.lang.Class ...

  7. Team Foundation 版本控制

    与 Visual Studio 的一流集成. 使用富文件和文件夹差异工具突出显示代码更改. 借助强大的可视化跨分支跟踪代码更改. 集成的代码评审工具有助于在签入代码之前获得反馈. 使用托管版本或本地版 ...

  8. COGS 1043. [Clover S2] Freda的迷宫

    ★   输入文件:mazea.in   输出文件:mazea.out   简单对比时间限制:1 s   内存限制:128 MB Freda 的迷宫 (mazea.pas/.c/.cpp) 题目叙述 F ...

  9. 详细讲解:yii 添加外置参数 高级版本

    在YII中,添加状态参数的形式 首先,我们在advanced\common\config\params.php文件中,添加我们要设置的参数: 要在控制器中进行使用的话,形式为:\Yii::$app-& ...

  10. Coursera 算法二 week2 Seam Carving

    这周作业设计到的算法是有向无环图的最短路径算法,只需要按照顶点的拓扑顺序去放松顶点即可.而在这个题目中拓扑顺序就是按照行的顺序或列的顺序. 用到的数据结构为一个二维数组picture同来存储每个像素的 ...