hdu 1536/ hdu 1944 S-Nim(sg函数)
S-Nim
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7751 Accepted Submission(s): 3266
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.
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
WWL
题意:首先输入K 表示一个集合的大小 之后输入集合 表示对于这对石子只能去这个集合中的元素的个数
之后输入 一个m 表示接下来对于这个集合要进行m次询问
之后m行 每行输入一个n 表示有n个堆 每堆有n1个石子 问这一行所表示的状态是赢还是输 如果赢输入W否则L
#include <bits/stdc++.h>
using namespace std; const int MAXN = + ;
const int MAXM = + ; int f[MAXN];//f[0]存合法移动个数
int sg[MAXM];
bool exist[MAXN];//hash, sg不会超过合法移动个数MAXN void getSg(int n)
{
int i, j;
sg[] = ;
for (i = ; i <= n; ++i) {
memset(exist, false, sizeof(exist));
for (j = ; j <= f[] && f[j] <= i; ++j) {
exist[sg[i - f[j]]] = true;
}
for (j = ; j < MAXN; ++j) {
if (!exist[j]) {
sg[i] = j;
break;
}
}
}
} int main()
{
int k;//, s;
int m;
int l, hi;
int i, j;
int sum; while (~scanf("%d", &k)) {
if (k == ) {
break;
}
f[] = k;
for (i = ; i <= k; ++i) {
scanf("%d", &f[i]);
}
sort(f + , f + + k);
getSg(); scanf("%d", &m);
for (i = ; i < m; ++i) {
scanf("%d", &l);
sum = ;
for (j = ; j < l; ++j) {
scanf("%d", &hi);
sum ^= sg[hi];
}
if (sum != ) {
printf("W");
} else {
printf("L");
}
}
printf("\n"); }
return ;
}
#include <bits/stdc++.h>
using namespace std; const int MAXN = + ;
const int MAXM = + ; int s[MAXN];
int sg[MAXM];
int n;//s中的个数 int dfsSg(int x)
{
if (sg[x] != -) {
return sg[x];
}
int i;
bool vis[MAXN];//sg范围
memset(vis, false, sizeof(vis));
for (i = ; i < n && s[i] <= x; ++i) {
dfsSg(x - s[i]);
vis[sg[x - s[i]]] = true;
}
for (i = ; i <= x; ++i) {
if (!vis[i]) {
sg[x] = i;
break;
}
}
return sg[x];
} int main()
{
int k;//, s;
int m;
int l, hi;
int i, j;
int sum; while (~scanf("%d", &k)) {
if (k == ) {
break;
}
n = k;
for (i = ; i < k; ++i) {
scanf("%d", &s[i]);
}
sort(s, s + k);
memset(sg, -, sizeof(sg));
scanf("%d", &m);
for (i = ; i < m; ++i) {
scanf("%d", &l);
sum = ;
for (j = ; j < l; ++j) {
scanf("%d", &hi);
sum ^= dfsSg(hi);
}
if (sum != ) {
printf("W");
} else {
printf("L");
}
}
printf("\n"); }
return ;
}
hdu 1536/ hdu 1944 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 1848 Fibonacci again and again(SG函数)
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- hdu 4559 涂色游戏(对SG函数的深入理解,推导打SG表)
提议分析: 1 <= N <= 4747 很明显应该不会有规律的,打表发现真没有 按题意应该分成两种情况考虑,然后求其异或(SG函数性质) (1)找出单独的一个(一列中只有一个) (2)找 ...
- hdu 3980 Paint Chain 组合游戏 SG函数
题目链接 题意 有一个\(n\)个珠子的环,两人轮流给环上的珠子涂色.规定每次涂色必须涂连续的\(m\)颗珠子,无法继续操作的人输.问先手能否赢. 思路 参考 转化 第一个人取完之后就变成了一条链,现 ...
- HDU 1848 Fibonacci again and again SG函数做博弈
传送门 题意: 有三堆石子,双方轮流从某堆石子中去f个石子,直到不能取,问先手是否必胜,其中f为斐波那契数. 思路: 利用SG函数求解即可. /* * @Author: chenkexing * @D ...
- 多校6 1003 HDU5795 A Simple Nim (sg函数)
思路:直接打表找sg函数的值,找规律,没有什么技巧 还想了很久的,把数当二进制看,再类讨二进制中1的个数是必胜或者必败状态.... 打表: // #pragma comment(linker, &qu ...
- hdu 3032 Nim or not Nim? (SG函数博弈+打表找规律)
Nim or not Nim? Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Sub ...
- 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 ...
随机推荐
- Server responded "Algorithm negotiation failed" SSH Secure链接服务器错误
Ubuntu 16.04安装openssh-server后,使用ssh客户端连接时可能报此错误,情况如下图所示: server responded "algorithm negotiatio ...
- tfboys——tensorflow模块学习(二)
tf.contrib模块 tf.contrib 模块是一个比较复杂的模块. contrib细节: tf.contrib.bayesflow.entropy 香农信息论 tf.contrib.baye ...
- 剑指offer 面试65题
题目65题:不用加减乘除做加法. 解法一:Python特性 # -*- coding:utf-8 -*- class Solution: def Add(self, num1, num2): # wr ...
- Objective-C 继承和多态
学习Java我们知道类有三大特征,封装,继承,多态.而在Objective-C中也有继承的概念,今天就来看看Objective-C中的继承和多态. 首先,我们来看看示例代码: //Animal.h # ...
- SpringMVC:学习笔记(6)——转换器和格式化
转换器和格式化 说明 SpringMVC的数据绑定并非没有限制,有案例表明,在SpringMVC如何正确绑定数据方面是杂乱无章的,比如在处理日期映射到Date对象上. 为了能够让SpringMVC进行 ...
- Python 5 面对对象编程
面向对象编程: 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装,让开发“更快更好更强...” 面向过程编程 ...
- iOS imageNamed VS imageWithContentsOfFile
今天 又学习了 一个 提高应用交互效率 降低内存的 小知识 结论: (1)mageNamed加载图片,并且把image缓存到内存里面, (2)imageWithContentsOfFile是只显示图片 ...
- Java开发者或许应该经常去看看的网站?...
Java开发者或许应该经常去看看的网站?...Google top3 1.Oracle Technology Network for Java Developers | Oracle Technolo ...
- 20145239杜文超《网络对抗》- Web安全基础实践
20145239杜文超<网络对抗>- Web安全基础实践 基础问题回答 (1)SQL注入攻击原理,如何防御? SQL注入攻击就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查 ...
- cocos2dx打飞机项目笔记五:CCSpriteBatchNode 的使用
在上一节里,在头文件看到 定义了一个 CCSpriteBatchNode* batchNode;,在addEnemy方法里看到 batchNode->addChild(enemy); 新建的敌机 ...