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. 一、 Spring Cloud Eureka ,咱们先跑起来

    一.Spring Cloud 简介 Spring Cloud 是一个基于Spring Boot 实现的微服务架构开发工具.是一个涉及到服务治理.分布式配置管理.负载均衡.服务容错.API网关.消息总线 ...

  2. 自定义HashMap的键

    用自定义的类型作为HashMap的key,必须同时重载hashCode()和equals(),才可以实现在HashMap中的查找自定义键. 例如自定义Point类: public class Poin ...

  3. 3D向2D投影

    http://blog.sina.com.cn/s/blog_536e0eaa0100jn7j.html

  4. 利用expect实现自动化操作

    管理机上需要安装expect包 yum -y install expect 1.定义主机ip [root@localhost ~]# cat ip.txt 192.168.1.12 192.168.1 ...

  5. cms-帖子静态化

    实现帖子静态化和实现友情链接静态化一致, 1.首先建立帖子类别的实体类: package com.open1111.entity; /** * 帖子类别实体 * @author user * */pu ...

  6. NYOJ-22-素数求和问题

    原题地址 素数求和问题 描述 现在给你N个数(0<N<1000),现在要求你写出一个程序,找出这N个数中的所有素数,并求和. 输入 第一行给出整数M(0<M<10)代表多少组测 ...

  7. IOS 截屏(保存到相册中)

    @interface NJViewController () /** * 点击截屏按钮 */ - (IBAction)captureView:(UIButton *)sender; /** * 白色v ...

  8. 使用selenium的方式获取网页中图片的链接和网页的链接,来判断是否是死链(二)

    上一篇使用Java正则表达式来判断和获取图片的链接以及跳转的网址,这篇使用selenium的自带的API(getAttribute)来获取网页中指定的内容 实现内容:获取下面所有图片的链接地址以及跳转 ...

  9. 【BZOJ1064】[NOI2008] 假面舞会(图上DFS)

    点此看题面 大致题意:有\(k\)种面具(\(k\)是一个未知数且\(k≥3\),每种面具可能有多个),已知戴第\(i\)种面具的人能看到第\(i+1\)种面具上的编号,特殊的,戴第\(k\)种面具的 ...

  10. 进入Windows之前发出警告

    实现效果: 知识运用: 通过注册表中HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\子键下的LegalNoticeCaption ...