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个一组循环节.. ...
随机推荐
- BZOJ-4010 菜肴制作 贪心+堆+(拓扑图拓扑序)
无意做到...char哥还中途强势插入干我...然后据他所言,看了一会题,一转头,我爆了正解....可怕 4010: [HNOI2015]菜肴制作 Time Limit: 5 Sec Memory L ...
- MySQL安装最后一步apply security settings错误
网上查了很久都是说删除各种文件什么的,直接百度apply security settings,说是mysql没卸载干净.不是的. 看日志发现 You must SET PASSWORD before ...
- POJ2288 Islands and Bridges
Description Given a map of islands and bridges that connect these islands, a Hamilton path, as we al ...
- CF Gym 100685E Epic Fail of a Genie
传送门 E. Epic Fail of a Genie time limit per test 0.5 seconds memory limit per test 64 megabytes input ...
- iOS 代理与block 逆向传值 学习
一般在项目中出现逆向传值的时候就需要用到代理.block 或者通知中心了.由于公司的项目底层封装的很好,所以项目做了三四个月就算碰到需要逆传的情况也不用自己处理.但是最近遇到了一个特别的情况就需要自己 ...
- 工作者对象HttpWorkerRequest
在ASP.NET中,用于处理的请求,需要封装为HttpWorkerRequest类型的对象.该类为抽象类,定义在命名空间System.Web下. #region Assembly System.Web ...
- tcpdump wireshark 实用过滤表达式(针对ip、协议、端口、长度和内容) 实例介绍
tcpdump wireshark 实用过滤表达式(针对ip.协议.端口.长度和内容) 实例介绍 标签: 网络tcpdst工具windowslinux 2012-05-15 18:12 3777人阅读 ...
- C++ 中map 中迭代器的简单使用:
public member function <map> std::map::find iterator find (const key_type& k); const_itera ...
- linux命令别名的使用
语 法:alias[别名]=[指令名称] 1,查看该用户下的别名: alias 2,有的系统里没有ll这个命令,原因就是没有定义ll='ls -l --color=tty'这个别名 如果想永久生效,就 ...
- linux远程连接客户端总结
序:刚从阿里ECS买了一个ubuntu14.04_64_20G,但是没有提供页面登陆工具,因此从网上找了几个远程连接工具,特写在这里算是总结. 1 secureCRT SecureCRT是一款支持SS ...