hdu1536 S-Nim
2016-01-24 11:49
323 查看
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5856 Accepted Submission(s): 2507
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
题意:给你一个集合S,再给你几个局面,每次操作某一堆只能减去S中的一个数,问当前局面是必胜局面还是必败局面。
思路:这题是sg模板题,因为终止条件是最后不能走的人输,所以当各个游戏的sg值异或和为0时为必败局面,不为0为必胜局面,我们只要处理每个游戏的sg值就行了。处理sg值的时候有两种方法,一种是每次每个局面单独求一遍,递归出它的后继游戏的sg的值,然后它的sg值就是不包含后继状态sg值的最小非负整数,还有一种是对于一个S集合求所有状态的sg值一次求出来,然后打表,那么要求的时候就可以直接用了。
代码一:递归求sg的值
代码二:先打表求出所有局面的sg函数值。
Total Submission(s): 5856 Accepted Submission(s): 2507
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
题意:给你一个集合S,再给你几个局面,每次操作某一堆只能减去S中的一个数,问当前局面是必胜局面还是必败局面。
思路:这题是sg模板题,因为终止条件是最后不能走的人输,所以当各个游戏的sg值异或和为0时为必败局面,不为0为必胜局面,我们只要处理每个游戏的sg值就行了。处理sg值的时候有两种方法,一种是每次每个局面单独求一遍,递归出它的后继游戏的sg的值,然后它的sg值就是不包含后继状态sg值的最小非负整数,还有一种是对于一个S集合求所有状态的sg值一次求出来,然后打表,那么要求的时候就可以直接用了。
代码一:递归求sg的值
#include<iostream> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<string> #include<algorithm> using namespace std; typedef long long ll; #define inf 99999999 #define pi acos(-1.0) int mex[110],sg[10010],s[110],n,m; int get_sg(int num) { int i,j; if(sg[num]!=-1)return sg[num]; bool mex[110]; //mex的大小和S集合的大小差不多 memset(mex,false,sizeof(mex)); for(i=1;i<=n;i++){ if(s[i]<=num){ mex[get_sg(num-s[i])]=true; } else break; } for(i=0;;i++){ if(!mex[i]){ sg[num]=i;return i; } } } int main() { int i,j,l,c,num; while(scanf("%d",&n)!=EOF && n!=0) { for(i=1;i<=n;i++){ scanf("%d",&s[i]); } sort(s+1,s+1+n); //这里要排序,因为get_sg函数中进行到s[i]>num就break了。 memset(sg,-1,sizeof(sg)); scanf("%d",&m); while(m--) { scanf("%d",&l); num=0; for(i=1;i<=l;i++){ scanf("%d",&c); num=num^get_sg(c); } if(num==0)printf("L"); else printf("W"); } printf("\n"); } return 0; }
代码二:先打表求出所有局面的sg函数值。
#include<iostream> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<string> #include<algorithm> using namespace std; typedef long long ll; #define inf 99999999 #define pi acos(-1.0) int mex[110],sg[10010],s[110],n,m; void get_sg( ) { int i,j; sg[0]=0; for(i=1;i<=10000;i++){ memset(mex,0,sizeof(mex)); for(j=1;j<=n;j++){ if(s[j]<=i){ mex[sg[i-s[j] ] ]=1; } else break; } for(j=0;;j++){ if(!mex[j]){ sg[i]=j;break; } } } } int main() { int i,j,l,c,num; while(scanf("%d",&n)!=EOF && n!=0) { for(i=1;i<=n;i++){ scanf("%d",&s[i]); } sort(s+1,s+1+n); get_sg(); scanf("%d",&m); while(m--) { scanf("%d",&l); num=0; for(i=1;i<=l;i++){ scanf("%d",&c); num=num^sg[c]; } if(num==0)printf("L"); else printf("W"); } printf("\n"); } return 0; }
相关文章推荐
- android之碎片(fragment)二(动态添加)
- User SCOTT executes the following command on the EMP table but has not issued COMMIT, ROLLBACK, or a
- LuaTest
- java基础2
- 通过打印学习Linux内核之sysfs(0)
- 按花生酱,赞不绝口——敏捷12准则的敏捷解释
- kafka
- 基于JavaScript短信验证码如何实现
- Uable to run mksdcard sdk tool in Ubuntu 15.10
- Android 支付宝支付
- Flume(4)实用环境搭建:source(spooldir)+channel(file)+sink(hdfs)方式
- 人脑理解递归与递推
- Android Activity的singleTask加载模式和onActivityResult方法之间的冲突
- 两台win7电脑,双网卡主机共享网络(局域网和Internet)给从机
- sqlhelper封装
- Android实现仿网易新闻主界面设计
- java基础
- java并发编程中CountDownLatch和CyclicBarrier的使用
- QtCharts笔记之QLineSeries
- Spring Boot 集成 批处理框架Spring batch