S-Nim

Time Limit : 5000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 2   Accepted Submission(s) : 1
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
 
 /*
题意:第二次做题,题意完全忘记。
前面都是背景,告诉你Nim是赢和输的规则。
后面改成了:选的数字是规定的。 数字是改变的,用打表划不来!
数字大小到10000,所以Hash只要到100就可以了。 SG的求法有两种,
1.是打表的。
参考http://www.cnblogs.com/tom987690183/archive/2013/05/30/3108564.html
2.是单点求取的。和记忆化搜索很相似。
这一题是单点的。
*/ #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std; int SG[];
int arry[]; int make_GetSG(int n)//求单点的。
{
int i,tmp,Hash[]={};//后继的大小开sqrt(N);
for(i=;i<=arry[];i++)
{
if(arry[i]>n)
break;
tmp=n-arry[i];
if(SG[tmp]==-)
SG[tmp]=make_GetSG(tmp);
Hash[SG[tmp]]=;
}
for(i=;;i++)
if(Hash[i]==)
return i;
} void make_ini(int m)
{
int i,j,k,n,x;
memset(SG,-,sizeof(SG));
while(m--)
{
scanf("%d",&n);
k=;
for(i=;i<=n;i++)
{
scanf("%d",&x);
k=k^make_GetSG(x);
}
if(k==)printf("L");
else printf("W");
}
printf("\n");
} int main()
{
int k,m,i;
while(scanf("%d",&k)>)
{
if(k==)break;
for(i=;i<=k;i++)
scanf("%d",&arry[i]);
arry[]=k;
sort(arry+,arry++k);
scanf("%d",&m);
make_ini(m);
}
return ;
}

HDU 1016 S-Nim ----SG求值的更多相关文章

  1. hdu 1237 简单计算器 (表达式求值)【stack】

    <题目链接> 题目大意: 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值.  Input测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符, ...

  2. hdu 5124(区间更新+单点求值+离散化)

    lines Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  3. HDU 1729 类NIM 求SG

    每次有n个盒子,每个盒子有容量上限,每次操作可以放入石头,数量为不超过当前盒子中数量的平方,不能操作者输. 一个盒子算一个子游戏. 对于一个盒子其容量为s,当前石子数为x,那么如果有a满足 $a \t ...

  4. hdu 3032 Nim or not Nim? (SG函数博弈+打表找规律)

    Nim or not Nim? Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Sub ...

  5. hdu 4192 (表达式求值)

    <题目链接> <转载于 >>>  > 题目大意: 给你n个数,和一个最终的结果,再给你一个含有n个不同变量的式子,问你这个式子最终能否得到指定的答案. 解题分 ...

  6. 随手练——HDU 1237 表达式求值(输入格式典型)

    坑了老子半天,结果是 float 范围不够!!! 基本思想: 开一个符号栈,一个数字栈: 碰到数字就入栈,碰到符号就与栈顶符号进行对比,如果当前符号优先级小于栈顶符号,数字栈弹出两个数进行栈顶符号运算 ...

  7. HDU 2176 基础NIM 输出方案

    普通的NIM,然后问先手必胜第一次操作后的所有局面. 对于一个必胜局面只要转变局面SG值为必败(SG=0)留给后手就行了. /** @Date : 2017-10-13 21:39:13 * @Fil ...

  8. Matrix Chain Multiplication(表达式求值用栈操作)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1082 Matrix Chain Multiplication Time Limit: 2000/100 ...

  9. 洛谷 P1981 表达式求值

    P1981 表达式求值 题目描述 给定一个只包含加法和乘法的算术表达式,请你编程计算表达式的值. 输入输出格式 输入格式: 输入文件为 expr.in. 输入仅有一行,为需要你计算的表达式,表达式中只 ...

随机推荐

  1. 通过网站统计或系统监视器查看IIS并发连接数

    如果要查看IIS连接数,最简单方便的方法是通过“网站统计”来查看,“网站统计”的当前在线人数可以认为是当前IIS连接数;如果要想知道确切的当前网站IIS连接数的话,最有效的方法是通过windows自带 ...

  2. Mysql错误处理: /usr/bin/mysqld_safe: line xxx: xxxx Killed ... (mysql自动停止 Plugin FEDERATED is disabled 的完美解决方法)

    哈哈哈,问题总算解决,内心抑不住的开心 centos mysql 问题:Plugin 'FEDERATED' is disabled. /usr/sbin/mysqld: Table 'mysql.p ...

  3. $_ENV和$GLOBALS预定义变量

    1,$_ENV该全局数组可以获取环境变量,在php5.3中默认禁用,但是可以在php.ini中启用. variables_order="EGPCS" 2,$GLOBALS一个包含了 ...

  4. Flask从入门到精通之flask扩展

    Flask被设计成可扩展形式,因此并没有提供一些重要的功能,比如数据库和用户认证,所以开发者可以自由选择最适合程序的包,或者按需求自行开发.社区成员开发了大量不同用途的扩展,如果这还不能满足需求,你还 ...

  5. vue中axios访问Java后端跨域问题解决

    问题背景: 前后端分离,前端选用Vue,后端选用Java,vue编译出的静态页面采用ngix发布,在前端访问后端时出现跨域问题. 解决方法: 跨域的问题解决方法有好多种,这里是通过服务端解决,以下是代 ...

  6. 【BZOJ4916】神犇和蒟蒻 杜教筛

    题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4916 第一个询问即求出$\sum_{i=1}^{n} { \mu (i^2)} $,考虑 ...

  7. django 高级

    1.使用form: django的form提供了统一的结构化的后台验证机制,错误信息,也容易展现在前台界面上.由于python的面向对象,使得编写html也能够代码复用. a.多个field 综合验证 ...

  8. QuantLib 金融计算

    我的微信:xuruilong100 <Implementing QuantLib>译后记 QuantLib 金融计算 QuantLib 入门 基本组件之 Date 类 基本组件之 Cale ...

  9. SpringMVC3.2+Spring3.2+Mybatis3.1(SSM~Demo)

    SpringMVC+Spring+Mybatis 框架搭建 整个Demo的视图结构: JAR: 下载地址:http://download.csdn.net/detail/li1669852599/85 ...

  10. 【数组】Remove Duplicates from Sorted Array II

    题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...