HDU1944 S-NIM(多个NIM博弈)
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.
InputInput consists of a number of test cases.
For each test case: The rst 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.OutputFor 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
题解:可以当成多个NIM博弈,最终答案等于每个NIMA博弈结果的异或;(注意求SG函数时,不要每次都把vis数组清空,用一个t标记即可,每次改变标记,否则会超时)
参考代码:
#include<bits/stdc++.h>
using namespace std;
#define clr(a,val) memset(a,val,sizeof a)
const int maxn=;
int num,f[maxn],ans;
int l,t,cas,SG[maxn],vis[maxn];
void GetSG(int x)
{
clr(SG,);
int t=;
for(int i=;i<=x;++i)
{
for(int j=;f[j]<=i&&j<=num;++j) vis[SG[i-f[j]]]=t;
for(int j=;j<=x;j++) {if(vis[j]!=t) {SG[i]=j;break;}}
++t;
}
} int main()
{
while(~scanf("%d",&num) && num)
{
for(int i=;i<=num;++i)scanf("%d",&f[i]);
sort(f+,f++num);
GetSG(maxn-);
scanf("%d",&cas);
while(cas--)
{
scanf("%d",&l);ans=;
for(int i=;i<=l;++i) scanf("%d",&t),ans^=SG[t];
if(!ans) printf("L");
else printf("W");
}
puts("");
}
return ;
}
最新文章
- Material Design Reveal effect(揭示效果) 你可能见过但是叫不出名字的小效果
- iOS开发 解决UITapGestureRecognizer手势与UITableView的点击事件的冲突
- sql总结(3)---比较全
- EntityFrame Work:No Entity Framework provider found for the ADO.NET provider with invariant name &#39;System.Data.SqlClient&#39;
- 用读写锁三句代码解决多线程并发写入文件 z
- Spring触发器配置Quartz
- C语言 常用的时间函数
- css 盒模型相关样式
- 电脑升级完Xcode8后 注释快捷键无效的问题
- PHP面向对象(OOP):.static和const关键字的使用(self::)
- C++中的string
- UVA 10139 Factovisors(数论)
- Echars 6大公共组件详解
- win7系统下dos界面无法自由调整大小
- Django的POST请求时因为开启防止csrf,报403错误,及四种解决方法
- Prometheus Redis_exporter
- visual studio 2017 installer 安装包制作过程出现的问题---此安装程序需要.NET Framework 版本 3.5,请安装该版本,然后重新运行此安装程序,可以从Web获得.NET Framework 。要立即做此事吗?
- C#模拟HTTP请求Post JSON
- mybaties实体的 Mapper.xml文件中自定义sql时模糊查询的写法
- VirtualBox虚拟机和主机之间的通信
热门文章
- Python 基础 三 反射
- T-SQL, Part I: LIKE Pattern
- C++中对C的扩展学习新增语法——lambda 表达式(匿名函数)
- 万恶之源-python内容的进化
- 18063-圈中的游戏-(第九章第4题)-";数组指针的使用";-数学分析
- PHP提高SESSION响应速度的方法有哪些
- day48天jQuary
- 12、pytest -- 缓存:记录执行的状态
- 【Android - 控件】之V - ViewPager的使用
- 爬虫框架Scrapy入门——爬取acg12某页面