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. jstl是自动就有的吗,不是的Unknown tag (c:if).

    这个错误的原因就是没有导包 http://www.runoob.com/jsp/jsp-jstl.html 这个网站有方法

  2. 洛谷P1082 同余方程

    题目描述 求关于 x 的同余方程 ax ≡ 1 (mod b)的最小正整数解. 输入输出格式 输入格式: 输入只有一行,包含两个正整数 a, b,用一个空格隔开. 输出格式: 输出只有一行,包含一个正 ...

  3. android中Handle类的用法

    android中Handle类的用法 当我们在处理下载或是其他需要长时间执行的任务时,如果直接把处理函数放Activity的OnCreate或是OnStart中,会导致执行过程中整个Activity无 ...

  4. c++实现快排出现错误

    #include"header_file.h" using namespace std; void swap(int a,int b) { int t; t=a; a=b; b=t ...

  5. udp 内网穿透 互发消息

    还差实现内网终端,向服务器发送请求,要对方的内网连接自己,实现打洞.在同一网段,或者公网运行,可以相互聊天. 没有实现检测客户端下线功能. 1,服务器代码 package router; import ...

  6. structs2标签简单实用,及自定义转换器示例代码

    一.在structs.xml中配置 <structs> <package name="tagp" namespace="/test" exte ...

  7. iOS 刚刚,几分钟前,几小时前,几天前,几月前,几年前

    - (NSString *)compareCurrentTime:(NSDate*) compareDate { NSTimeInterval timeInterval = [compareDate ...

  8. 使用log4net连接Mysql数据库配置

    log4net配置: //Author:GaoBingBing [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net ...

  9. Tomcat的目录结构及用途

    目录结构及用途 目录 用途 bin 包含启动/关闭脚本 conf 包含不同的配置文件,包括 server.xml(Tomcat的主要配置文件)和为不同的Tomcat配置的web应用设置缺省值的文件we ...

  10. EDdb 是ED数据

    eddb  是ED数据统计汇总软件的简称,用于统计汇总企事业单位的各类信息数据. 采用Excel界面,操作简单. 对各类信息数据,均可以自定义数据格式,通过internet联网,收集各类信息数据,并通 ...