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 ;
}

最新文章

  1. ubuntu使用doxygen
  2. Daily Scrum Meeting ——ThirdDay
  3. Java maven安装GDAL
  4. 设置IE兼容模式的几种方法
  5. HashCheck
  6. 部署解决方案包 (SharePoint Server 2010)
  7. c++ 中的8种智能指针[转]
  8. java.lang.ClassNotFoundException错误原因汇总
  9. [转]Laravel 4之Eloquent ORM
  10. respondsToSelector的相关使用
  11. poj 2054 Color a Tree(贪婪)
  12. 超级好用的excel第三方组件
  13. Gitbucket—快速建立自己的Github
  14. codeforces 809E Surprise me!
  15. Http 压测工具 wrk 基本使用
  16. Task: Indoor Positioning with WiFi Signals
  17. 6.7 使用show profile 进行sql分析
  18. 美团点评MySQL数据库高可用架构从MMM到MHA+Zebra以及MHA+Proxy的演进
  19. C# MD5 32位加密 UTF-8编码
  20. thinkPHP 快速上手

热门文章

  1. 写在SDOI2016Round1前的To Do List
  2. RegexBuddy正则表达式工具
  3. TCP/IP Four Layer Protocol Format Learning
  4. Mysql数据库的工作原理
  5. JavaScript的apply和call方法及其区别
  6. 大数据分析与机器学习领域Python兵器谱
  7. --hdu 1231 最大连续子序列(动态规划)
  8. mysql zip 版本配置方法
  9. nginx 服务器重启命令,关闭 (转)
  10. 【Swoole应用教程】一、Swoole扩展的编译安装部署