F - Again Stone Game

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Submit Status

Description

Alice and Bob are playing a stone game. Initially there are n piles of stones and each pile contains some stone. Alice stars the game and they alternate moves. In each move, a player has to select any pile and should remove at least one and no more than half stones from that pile. So, for example if a pile contains 10 stones, then a player can take at least 1 and at most 5 stones from that pile. If a pile contains 7 stones; at most 3 stones from that pile can be removed.

Both Alice and Bob play perfectly. The player who cannot make a valid move loses. Now you are given the information of the piles and the number of stones in all the piles, you have to find the player who will win if both play optimally.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case starts with a line containing an integer n (1 ≤ n ≤ 1000). The next line contains n space separated integers ranging in [1, 109]. The ith integer in this line denotes the number of stones in the ith pile.

Output

For each case, print the case number and the name of the player who will win the game.

Sample Input

5

1

1

3

10 11 12

5

1 2 3 4 5

2

4 9

3

1 3 9

Sample Output

Case 1: Bob

Case 2: Alice

Case 3: Alice

Case 4: Bob

Case 5: Alice

题意:有n堆石子,分别有a1,a2,......,an个。两个游戏者轮流操作,每次可以选一堆,

   拿走至少一个石子,但不能拿走超过一半的石子。比如,若有3堆石子,每堆分别有

   5,1,2个,则在下一轮中,游戏者可以从第一堆中拿1个或2个,第二堆中不能拿,第三堆

   只能拿一个。谁不能拿石子,就算输。

题解:刘汝佳白皮书《算法竞赛入门经典训练指南》 p137原题。首先递推出sg函数找规律。

打印sg函数找规律:

#include <iostream>
#include <cstring>
using namespace std;
const int maxn=;
int sg[maxn];
int vis[maxn];
int main()
{
sg[]=;
for(int i=; i<=; i++)
{
memset(vis,,sizeof(vis));
for(int j=; j*<=i; j++)
vis[sg[i-j]]=;
for(int j=;; j++)
{
if(!vis[j])
{
sg[i]=j;
break;
} }
cout<<sg[i]<<" ";
}
return ;
}
/*
运行结果:
1 0 2 1 3 0 4 2 5 1 6 3 7 0 8 4 9 2 10 5 11 1 12 6 13 3 14 7 15
Process returned 0 (0x0) execution time : 0.062 s
Press any key to continue.
*/

发现n为偶数时,sg(n)=n/2。

把n为偶数的值全部删除后得到的数列是0 1 0 2 1 3 0 4 2 5 1 6 3 7 ...

把n为偶数的值全部删除,发现得到的数列和原数列一样。

则当n为奇数时,sg(n)=sg(n/2),这里的n是向下取整的。

以下为AC代码:

#include <iostream>
#include <stdio.h>
using namespace std;
int sg(int n)
{
while(n&) n>>=; //当n为奇数时,递归到为偶数为止。
return n>>;
}
int main()
{
int i,n,t,cas=;
cin>>t;
while(t--)
{
int ans=,data;
cin>>n;
for(i=;i<n;i++)
{
cin>>data;
ans^=sg(data);
}
if(ans)
printf("Case %d: Alice\n",cas++);
else
printf("Case %d: Bob\n",cas++);
}
return ;
}

Light OJ 1296 - Again Stone Game (博弈sg函数递推)的更多相关文章

  1. Light OJ 1199:Partitioning Game(SG函数模板)

    Alice and Bob are playing a strange game. The rules of the game are: 1.      Initially there are n p ...

  2. LightOJ 1296 Again Stone Game(sg函数)题解

    题意:每次必须拿且只能拿不超过一半的石头,不能拿为败 思路:显然算出每个的sg函数,但是范围1e9显然不能直接打表.所以先打表找规律,发现偶数一直是自己的一半,奇数好像没规律.偶数x的sg函数值是x/ ...

  3. 一类SG函数递推性质的深入分析——2018ACM陕西邀请赛H题

    题目描述 定义一种有根二叉树\(T(n)\)如下: (1)\(T(1)\)是一条长度为\(p\)的链: (2)\(T(2)\)是一条长度为\(q\)的链: (3)\(T(i)\)是一棵二叉树,它的左子 ...

  4. UVa 10561 (SG函数 递推) Treblecross

    如果已经有三个相邻的X,则先手已经输了. 如果有两个相邻的X或者两个X相隔一个.,那么先手一定胜. 除去上面两种情况,每个X周围两个格子不能再放X了,因为放完之后,对手下一轮再放一个就输了. 最后当“ ...

  5. 【主席树维护mex】 【SG函数递推】 Problem H. Cups and Beans 2017.8.11

    Problem H. Cups and Beans 2017.8.11 原题: There are N cups numbered 0 through N − 1. For each i(1 ≤ i ...

  6. S-Nim HDU 1536 博弈 sg函数

    S-Nim HDU 1536 博弈 sg函数 题意 首先输入K,表示一个集合的大小,之后输入集合,表示对于这对石子只能去这个集合中的元素的个数,之后输入 一个m表示接下来对于这个集合要进行m次询问,之 ...

  7. Light OJ 1199 - Partitioning Game (博弈sg函数)

    D - Partitioning Game Time Limit:4000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  8. hdu 3032(博弈sg函数)

    题意:与原来基本的尼姆博弈不同的是,可以将一堆石子分成两堆石子也算一步操作,其它的都是一样的. 分析:由于石子的堆数和每一堆石子的数量都很大,所以肯定不能用搜索去求sg函数,现在我们只能通过找规律的办 ...

  9. HDU-4678 Mine 博弈SG函数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4678 题意就不说了,太长了... 这个应该算简单博弈吧.先求联通分量,把空白区域边上的数字个数全部求出 ...

随机推荐

  1. IIS7部署项目时提示:"错误消息 401.2。: 未经授权: 服务器配置导致登录失败。"的解决办法

    这个错误的定位:你的站点使用了Forms验证,而且在部署在生产环境的时候,设置错误,或者注释了. 解决方法如下: 1.检查Forms配置是否屏蔽. 2.有权限访问的资源是否已经开发. 基本就围绕以上两 ...

  2. mq安装参考

    CentOS 6.2 64bit 安装erlang及RabbitMQ Server 1.操作系统环境(CentOS 6.2 64bit) [root@leekwen ~]# cat /etc/issu ...

  3. 利用Arraylist输入学生的成绩,求出平均分和总分。

    Console.WriteLine("请输入学生人数:"); int n=int.Parse(Console.ReadLine()); ArrayList arr= new Arr ...

  4. hdu 2111 Saving HDU

    解题思路: 首先做本题,要清楚题意的要求. 1.读取数据到结构体数组中,然后按其价值降序(价值最大的放在最上面). 2.比较给定的M (包裹容量),如果大于当前宝物的体积,则计算总价值+= 宝物的总价 ...

  5. pthread 学习系列 case2-- pthread_mutex_t

    许多互斥对象 如果放置了过多的互斥对象,代码就没有什么并发性可言,运行起来也比单线程解决方案慢.如果放置了过少的互斥对象,代码将出现奇怪和令人尴尬的错误.幸运的是,有一个中间立场.首先,互斥对象是用于 ...

  6. IIS6.0文件解析漏洞小结

    今天搞站,本来这个站是aspx的,webserver是IIS6.0的,进入后台之后,发现有一个上传图片的地方,于是,我就上传了一张asp/aspx的一句话图片木马,但是用菜刀连接的时候,没有成功get ...

  7. EL表达式从request和session中取值

    在Action中保存登录的基本信息:request.getSession().setAttribute("adminid", str); 在JSP页面中:${sessionScop ...

  8. 如何在word里面插入目录

    点击“引用”->插入目录

  9. BC.36.Gunner(hash)

    Gunner  Accepts: 391  Submissions: 1397  Time Limit: 8000/4000 MS (Java/Others)  Memory Limit: 65536 ...

  10. 实战 -- Redis2.4.2集成spring3.2.2

    redis.host=... redis.port= redis.pass= redis.timeout= #最大能够保持idel状态的对象数 redis.maxIdle= #最大分配的对象数 red ...