hdu 3032 Nim or not Nim? (SG函数博弈+打表找规律)
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
Nim is usually played as a misere game, in which the player to take the last object loses. Nim can also be played as a normal play game, which means that the person who makes the last move (i.e., who takes the last object) wins. This is called normal play because most games follow this convention, even though Nim usually does not.
Alice and Bob is tired of playing Nim under the standard rule, so they make a difference by also allowing the player to separate one of the heaps into two smaller ones. That is, each turn the player may either remove any number of objects from a heap or separate a heap into two smaller ones, and the one who takes the last object wins.
Input
Output
Sample Input
3
2 2 3
2
3 3
Sample Output
Bob
题意:给定n堆石子,两人轮流操作,每次选一堆石子,取任意石子或则将石子分成两个更小的堆(非0),取得最后一个石子的为胜。
题解:比较裸的SG定理,用sg定理打表,得到表1,2,4,3,5,6,8,7,9,10,12,11...可以发现当x%4==0时sg[x]=x-1;当x%4==3时sg[x]=x+1;其余sg[x]=x。然后异或下就出来结果了。
SG定理打表+找规律:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=1e4+;
int sg[maxn],vis[maxn];
void init()
{
int i,j,k;
sg[]=,sg[]=;
for(i=;i<=;i++)
{
memset(vis,,sizeof(vis));
for(j=;j<i;j++)
vis[sg[j]^sg[i-j]]=; //拆分
for(j=;j<i;j++)
vis[sg[j]]=; //取石子
for(j=;;j++)
if(!vis[j])break;
sg[i]=j;
}
for(i=;i<=;i++)
cout<<sg[i]<<endl;
}
int main()
{
init();
}
AC代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=1e6+;
int find(int x)
{
if(x%==)return x-;
else if(x%==)return x+;
return x;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int a,n,i,j,ans=;
scanf("%d",&n);
for(i=;i<n;i++)
{
scanf("%d",&a);
ans=ans^find(a);
}
if(ans==)printf("Bob\n");
else printf("Alice\n");
}
return ;
}
hdu 3032 Nim or not Nim? (SG函数博弈+打表找规律)的更多相关文章
- HDU 5795 A Simple Nim (博弈 打表找规律)
A Simple Nim 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5795 Description Two players take turns ...
- hdu 3032 Nim or not Nim? (sg函数打表找规律)
题意:有N堆石子,每堆有s[i]个,Alice和Bob两人轮流取石子,可以从一堆中取任意多的石子,也可以把一堆石子分成两小堆 Alice先取,问谁能获胜 思路:首先观察这道题的数据范围 1 ≤ N ...
- HDU 3032 Nim or not Nim?(Multi_SG,打表找规律)
Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- HDU 5795 A Simple Nim(SG打表找规律)
SG打表找规律 HDU 5795 题目连接 #include<iostream> #include<cstdio> #include<cmath> #include ...
- HDU 3032 (SG打表找规律)
题意: 有n堆石子,alice先取,每次可以选择拿走一堆石子中的1~x(该堆石子总数) ,也可以选择将这堆石子分成任意的两堆.alice与bob轮流取,取走最后一个石子的人胜利. 思路: 因为数的范围 ...
- hdu_5795_A Simple Nim(打表找规律的博弈)
题目链接:hdu_5795_A Simple Nim 题意: 有N堆石子,你可以取每堆的1-m个,也可以将这堆石子分成3堆,问你先手输还是赢 题解: 打表找规律可得: sg[0]=0 当x=8k+7时 ...
- HDU2147 kiki's game (SG表找规律)
Recently kiki has nothing to do. While she is bored, an idea appears in his mind, she just playes th ...
- HDU 5753 Permutation Bo (推导 or 打表找规律)
Permutation Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5753 Description There are two sequen ...
- HDU 4731 Minimum palindrome 打表找规律
http://acm.hdu.edu.cn/showproblem.php?pid=4731 就做了两道...也就这题还能发博客了...虽然也是水题 先暴力DFS打表找规律...发现4个一组循环节.. ...
随机推荐
- 37.Activity之间的转换以及数据的传递(Intent)学习
Intent简介: 在一个Androi ...
- BZOJ-1305 dance跳舞 建图+最大流+二分判定
跟随YveH的脚步又做了道网络流...%%% 1305: [CQOI2009]dance跳舞 Time Limit: 5 Sec Memory Limit: 162 MB Submit: 2119 S ...
- OI历程日常
之前的一直没来的及记录,表示从今往后连载 10.29 蒟蒻正在紧张的备战NOIP 整改了一下faebdc学长的模拟题,T1直接可以暴力破解,T2二分,O(nlog^2n)开始二分写残了,调了半天唉,现 ...
- C#获取本机IP且过滤非真实网卡(如虚拟机网卡)
参考了网上的文章,具体地址不记得了. 下面的方法可以过滤掉虚拟机的网卡等无效网卡,进而只留下真实的网卡. using System; using System.Collections.Generic; ...
- OMNET++工具的使用(2)
http://blog.csdn.net/codingkid/article/details/7085214 首先解决一些概念上的问题: 1. 在omnetpp.org中提到的仿真模型和框架与OMNe ...
- js中按钮控制显示隐藏以及下拉功能
<script> function show() { var a2=document.getElementById("div2"); if(a2.style.displ ...
- javaweb学习总结(三十)——EL函数库
一.EL函数库介绍 由于在JSP页面中显示数据时,经常需要对显示的字符串进行处理,SUN公司针对于一些常见处理定义了一套EL函数库供开发者使用. 这些EL函数在JSTL开发包中进行描述,因此在JSP页 ...
- 将对象转为数组方法:延伸array_map函数在PHP类中调用内部方法
public static function objectToArray($d) { if (is_object($d)) { $d = get_object_vars($d); } if (is_a ...
- [Socket网络编程]由于套接字没有连接并且(当使用一个 sendto 调用发送数据报套接字时)没有提供地址,发送或接收数据的请求没有被接受。
原文地址:http://blog.sina.com.cn/s/blog_70bf579801017ylu.html,记录在此方便查看 解决办法: MSDN的说明: Close 方法可关闭远程主机连接, ...
- 反射中 GetCustomAttributes
public abstract object[] GetCustomAttributes(bool inherit); 这是GetCustomAttributes方法的一个重载,参数为bool类型返回 ...