HDU 1535 S-Nim(SG函数)
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
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
Output
Sample Input
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
题意
首先给出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函数)的更多相关文章
- 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 ...
- hdu 3032 Nim or not Nim? (SG函数博弈+打表找规律)
Nim or not Nim? Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Sub ...
- HDU 5724 Chess(SG函数+状态压缩)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5724 题意: 现在有一个n*20的棋盘,上面有一些棋子,双方每次可以选择一个棋子把它移动到其右边第一 ...
- HDU 5724 Chess(SG函数)
Chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- hdu 3032(博弈sg函数)
题意:与原来基本的尼姆博弈不同的是,可以将一堆石子分成两堆石子也算一步操作,其它的都是一样的. 分析:由于石子的堆数和每一堆石子的数量都很大,所以肯定不能用搜索去求sg函数,现在我们只能通过找规律的办 ...
- 多校6 1003 HDU5795 A Simple Nim (sg函数)
思路:直接打表找sg函数的值,找规律,没有什么技巧 还想了很久的,把数当二进制看,再类讨二进制中1的个数是必胜或者必败状态.... 打表: // #pragma comment(linker, &qu ...
- hdu 1079 Calendar Game sg函数 难度:0
Calendar Game Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- HDU 3032 Nim or not Nim (sg函数)
加强版的NIM游戏,多了一个操作,可以将一堆石子分成两堆非空的. 数据范围太大,打出sg表后找规律. # include <cstdio> # include <cstring> ...
- HDU 1729 Stone Game 石头游戏 (Nim, sg函数)
题意: 有n个盒子,每个盒子可以放一定量的石头,盒子中可能已经有了部分石头.假设石头无限,每次可以往任意一个盒子中放石头,可以加的数量不得超过该盒中已有石头数量的平方k^2,即至少放1个,至多放k^2 ...
随机推荐
- 玩转Docker之常用API(四)
原文地址:http://accjiyun.cn/wan-zhuan-dockerzhi-chang-yong-api-si/ 任何一个开发的平台都会向开发者开发API,以供开发者更加自由地使用平台所提 ...
- WPF根据数据项获取条目控件的方法-ItemContainerGenerator
一.方法: ContainerFromIndex:返回 ItemCollection 中指定索引处的项的容器. ContainerFromItem:返回与制定的项对应的容器(ComboxItem等条目 ...
- HDU4352 XHXJ's LIS(LIS 状压)
题意 题目链接 Sol 刚开始的思路是\(f[i][j]\)表示到第\(i\)位,LIS长度为\(j\)的方案. 然而发现根本不能转移,除非知道了之前的状态然后重新dp一遍.. 题解,,,挺暴力的把, ...
- NC57访问报错:java.sql.SQLException: Io 异常: Got minus one from a read call
一.报错信息 1. 前端登录界面 2. 后台应用日志 报错信息一致为: $$callid= $$thread=[Service Monitor and Runtime Enroment] $$ho ...
- 小div在大div里面 垂直居中
方法1: .parent { width:800px; height:500px; border:2px solid #000; position:relative; } .child { width ...
- CSS样式表优化
前几天公司要模仿一家客户的网站模板来为另一客户新建一个模板,说白了就是换个数据源,然后样式表再小修小改一下就行了.但通过浏览器控制台下载素材时,发现这个网站开发的挺专业的,单就样式表而言,代码工整,注 ...
- HDU3973 线段树 + 字符哈希
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3973 , 线段树 + 字符哈希,好题. 又学了一种新的哈希方法,hhhh~ 解法: 想法是用P进制的数 ...
- leetcode--5 Longest Palindromic Substring
1. 题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximu ...
- iOS 提交form表单,上传图片
之前不会用,总感觉很难,用后感觉不是太难,本文只是简单的讲一下怎么使用的, //实例话对象 AFHTTPSessionManager *manager = [AFHTTPSessionManager ...
- python实现链表中倒数第k个结点
题目描述 输入一个链表,输出该链表中倒数第k个结点 第一种实现: # -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # ...