hdu 1536&&1944 S-Nim sg函数 难度:0
S-Nim
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4770 Accepted Submission(s): 2058
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
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int s[101],k;
int sg[10001];
int n;
int seg(int num){
if(sg[num]!=-1)return sg[num];
bool used[101];
memset(used,0,sizeof(used));
for(int i=0;i<k;i++){
if(num<s[i])break;
used[seg(num-s[i])]=true;
}
for(int i=0;i<101;i++)if(!used[i])return sg[num]=i;
}
int main(){
freopen("C:\\Users\\Administrator\\Desktop\\input.txt","w",stdout);
while(scanf("%d",&k)==1&&k){
for(int i=0;i<k;i++)scanf("%d",s+i);
memset(sg,-1,sizeof(sg));
sort(s,s+k);
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
int sum=0,tmp;
for(int i=0;i<n;i++){scanf("%d",&tmp);sum^=seg(tmp);}
if(sum==0)putchar('L');
else putchar('W');
}
putchar('\n');
}
return 0;
}
hdu 1536&&1944 S-Nim sg函数 难度:0的更多相关文章
- 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 1536 S-Nim (组合游戏+SG函数)
题意:针对Nim博弈,给定上一个集合,然后下面有 m 个询问,每个询问有 x 堆石子 ,问你每次只能从某一个堆中取出 y 个石子,并且这个 y 必须属于给定的集合,问你先手胜还是负. 析:一个很简单的 ...
- hdu 1079 Calendar Game sg函数 难度:0
Calendar Game Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- hdu 1536 S-Nim (简单sg函数)
题意:首先输入K 表示一个集合的大小 之后输入集合 表示对于这对石子只能去这个集合中的元素的个数 之后输入 一个m 表示接下来对于这个集合要进行m次询问 之后m行 每行输入一个n 表示有n个堆 每 ...
- hdu 1536/1944 / POJ 2960 / ZOJ 3084 S-Nim 博弈论
简单的SG函数应用!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #inclu ...
- 多校6 1003 HDU5795 A Simple Nim (sg函数)
思路:直接打表找sg函数的值,找规律,没有什么技巧 还想了很久的,把数当二进制看,再类讨二进制中1的个数是必胜或者必败状态.... 打表: // #pragma comment(linker, &qu ...
- HDU 1517 A Multiplication Game (SG函数找规律)
题意:两个玩家玩一个游戏,从 p = 1,开始,然后依次轮流选择一个2 - 9的数乘以 p,问你谁先凑够 p >= n. 析:找规律,我先打了一下SG函数的表,然后就找到规律了 我找到的是: 1 ...
- hdu 1847 博弈基础题 SG函数 或者规律2种方法
Good Luck in CET-4 Everybody! Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- hdu 1536/ hdu 1944 S-Nim(sg函数)
S-Nim Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
随机推荐
- iOS 关于BTC 一些知识点
1.BTC 用这个网 可以校验 自己的库生成的助记词 地址 是否是合法正常的 https://iancoleman.io/bip39/ 2.知晓 BTC 钱包是否有钱 和交易记录 https://te ...
- QUIC协议
QUIC协议 QUIC协议参考网址 https://www.chromium.org/quic 既生瑜,何生亮? QUIC的特性 提供可靠传输 减少连接建立的时间 改善拥塞控制 多路复用 转发错误连接 ...
- python3 驱动 PyMySQL
Python版本: 3.5.0 MySqlDB官网只支持Python3.4, 使用第三方库PyMysql连接Mysql数据库. https://pypi.python.org/pypi/PyMySQ ...
- java中元注解
java中元注解有四个: @Retention @Target @Document @Inherited: @Retention:注解的保留位置 @Retention(RetentionPolicy ...
- centos7开启网卡功能
centos7安装完成后,网卡默认是关闭的,未分配ip地址 解决办法: 1.cd /etc/sysconfig/network-scripts/ 2.ls查看网卡 3.修改该文件 vi ifcfg-e ...
- 异常信息 Exception
void GetExceptionMsg(Exception ex, StringBuilder sb, string Prefix = "") { sb.AppendLine(P ...
- Web安全学习笔记之Openvas配置,使用,报告
OpenVAS(开放式漏洞评估系统)是一个客户端/服务器架构,它常用来评估目标主机上的漏洞.OpenVAS是Nessus项目的一个分支,它提供的产品是完全地免费.OpenVAS默认安装在标准的Kali ...
- 20145331 《Java程序设计》第6周学习总结
20145331 <Java程序设计>第6周学习总结 教材学习内容总结 第十章 输入/输出 10.1.1串流 •Java将输入/输出抽象化为串流,数据有来源及目的地,衔 ...
- c++ 使用WinHTTP实现文件下载功能
因为要项目中要想要实现一个软件自动更新的功能,之前是使用socket直接下载.但切换下载源的时候很麻烦.所以换用http方式. 网上找了很多资料,基本上就是下面几种: 1.curllib //功能强大 ...
- Linux内核、 TCP/IP、Socket参数调优
/proc/sys/net目录 所有的TCP/IP参数都位于/proc/sys/net目录下(请注意,对/proc/sys/net目录下内容的修改都是临时的,任何修改在系统重启后都会丢失),例如下面这 ...