Alice and Bob are playing a strange game. The rules of the game are:

1.      Initially there are n piles.

2.      A pile is formed by some cells.

3.      Alice starts the game and they alternate turns.

4.      In each tern a player can pick any pile and divide it into two unequal piles.

5.      If a player cannot do so, he/she loses the game.

Now you are given the number of cells in each of the piles, you have to find the winner of the game if both of them play optimally.

Input

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

Each case starts with a line containing an integer n (1 ≤ n ≤ 100). The next line contains n integers, where the ith integer denotes the number of cells in the ith pile. You can assume that the number of cells in each pile is between 1 and 10000.

Output

For each case, print the case number and 'Alice' or 'Bob' depending on the winner of the game.

Sample Input

3

1

4

3

1 2 3

1

7

Sample Output

Case 1: Bob

Case 2: Alice

Case 3: Bob

题意

有n堆石子,每一堆分别有ai个石子

一次操作可以使一堆石子变成两堆数目不相等的石子

最后不能操作的算输,问先手胜还是后手胜

思路

n堆石子相互独立,所以可以应用SG定理,只需要算出一堆石子的SG函数。

一堆石子(假设有x个)的后继状态可以枚举出来,分别是{1,x-1},{2,x-2},...,{(x-1)/2,x-(x-1)/2},

一堆石子分成的两堆石子又相互独立,再次应用SG定理。

所以SG(x) = mex{ SG(1)^SG(x-1), SG(2)^SG(x-2),..., SG((x-1)/2)^SG(x-(x-1)/2) },

最后的答案是SG(x1)^SG(x2)^...^SG(xn)

AC代码

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ull unsigned long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
#define lson o<<1
#define rson o<<1|1
const double E=exp(1);
const int maxn=1e4+10;
const int mod=1e9+7;
using namespace std;
int sg[maxn];
int vis[maxn];
int a[maxn];
inline void getSG()
{
ms(sg);
for(register int i=1;i<=maxn;i++)
{
ms(vis);
for(int j=1;2*j<i;j++)
vis[sg[i-j]^sg[j]]=1;
for(register int j=0;j<=maxn;j++)
{
if(!vis[j])
{
sg[i]=j;
break;
}
}
}
}
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
register int t;
getSG();
register int n;
cin>>t;
register int _=0;
while(t--)
{
int sum=0;
ms(a);
cin>>n;
for(register int i=1;i<=n;i++)
{
cin>>a[i];
sum^=sg[a[i]];
}
cout<<"Case "<<++_<<": ";
if(sum)
cout<<"Alice"<<endl;
else
cout<<"Bob"<<endl;
}
return 0;
}

参考博客:https://www.cnblogs.com/Ritchie/p/5396893.html

Light OJ 1199:Partitioning Game(SG函数模板)的更多相关文章

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

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

  2. LIGHT OJ 1199 - Partitioning Game

    传送门 1199 - Partitioning Game    PDF (English) problem=1199" style="color:rgb(79,107,114)&q ...

  3. hdu1536&&hdu3023 SG函数模板及其运用

    S-Nim Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status ...

  4. SG函数模板(转)

    ps:sg[i]为0表示i节点先手必败. 首先定义mex(minimal excludant)运算,这是施加于一个集合的运算,表示最小的不属于这个集合的非负整数.例如mex{0,1,2,4}=3.me ...

  5. hdu 1536 SG函数模板题

    S-Nim Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  6. 【非原创】sg函数模板

    学习博客:戳这里 解题模型: 1.把原游戏分解成多个独立的子游戏,则原游戏的SG函数值是它的所有子游戏的SG函数值的异或.        即sg(G)=sg(G1)^sg(G2)^...^sg(Gn) ...

  7. HDU 1847-Good Luck in CET-4 Everybody!-博弈SG函数模板

    Problem Description 大学英语四级考试就要来临了,你是不是在紧张的复习?也许紧张得连短学期的ACM都没工夫练习了,反正我知道的Kiki和Cici都是如此.当然,作为在考场浸润了十几载 ...

  8. SG函数模板

    这篇虽然是转载的,但代码和原文还是有出入,我认为我的代码更好些. 转载自:http://www.cnblogs.com/frog112111/p/3199780.html 最新sg模板: //MAXN ...

  9. hdu 1536 S-Nim(sg函数模板)

    转载自:http://blog.csdn.net/sr_19930829/article/details/23446173 解题思路: 这个题折腾了两三天,参考了两个模板,在这之间折腾过来折腾过去,终 ...

随机推荐

  1. 微信小程序 HMACSHA256 哈希加密

    下载CryptoJS, 增加红色的这句 module.exports = CryptoJS /* CryptoJS code.google.com/p/crypto-js (c) 2009-2012 ...

  2. Eclipse Mars-Ant无法使用jre1.6的问题

    https://www.jianshu.com/p/24b9517d7f43 Eclipse 升级到Mars 4.5.1版本后,老项目使用jdk1.6,通过ant编译jar时,会遇到如下问题: 使用j ...

  3. 基本git指令

    --git包含命令行界面和图形化界面 1.Git安装之后需要进行一些基本信息设置 a.设置用户名:git  config -- global  user.name  '你再github上注册的用户名' ...

  4. js之querySelector方法

    querySelector()接受一个CSS选择符,返回匹配的第一个元素,反之则NULL. 如: var body = document.querySelector('body'); var mydi ...

  5. mac CodeIgniter和EasyWeChat 开发微信公众号

    mac 安装 Composer //composer安装成功 curl -sS https://getcomposer.org/installer | php //将composer.phar移动到 ...

  6. hdu 2065 "红色病毒"问题(快速幂求模)

    n=1  --> ans = 2 = 1*2 = 2^0(2^0+1) n=2  -->  ans = 6 = 2*3 = 2^1(2^1+1) n=3  -->  ans = 20 ...

  7. http协商缓存VS强缓存

    之前一直对浏览器缓存只能描述一个大概,深层次的原理不能描述上来:终于在前端的两次面试过程中被问倒下,为了泄恨,查阅一些资料最终对其有了一个更深入的理解,废话不多说,赶紧来看看浏览器缓存的那些事吧,有不 ...

  8. datatabale 服务器分页

    转载:http://blog.csdn.net/angelvyvyan/article/details/51783272$(document).ready( function() { $('#tabl ...

  9. SQL-5查找所有员工的last_name和first_name以及对应部门编号dept_no,也包括展示没有分配具体部门的员工

    输出描述: 题目描述 查找所有员工的last_name和first_name以及对应部门编号dept_no,也包括展示没有分配具体部门的员工CREATE TABLE `dept_emp` (`emp_ ...

  10. EffecJava Method

    坚持使用Overrider注解,可以预防我们并没有重载的情况出现. 除非使用者绝对安全,不然使用保护性拷贝,可以使程序安全. public class Period {//没有拷贝安全 private ...