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. h5的坑

    转自 http://www.mahaixiang.cn 解决各种坑 http://www.mahaixiang.cn/ydseo/1529.html

  2. span 超出部分换行

    span{ word-break: normal; width: auto; display: block; white-space: pre-wrap; word-wrap: break-word; ...

  3. Best Paper Awards in Computer Science 链接

    http://jeffhuang.com/best_paper_awards.html#icml

  4. windbg 定位崩溃问题

    三板斧如下: 使用windbg打开dump文件,设置好对应进程的 pdb 文件(这个很关键.为了避免releas优化导致符号文件错乱,我发布的所有                      relea ...

  5. linux网络操作 ifconfig命令

    ifconfig 查看已经被激活的网卡详细信息 "ifconfig eth0" 查看特定的网卡信息 [root@ssgao ~]# ifconfig eth0 eth0 Link ...

  6. uImage是什么

    vmlinux是内核文件,zImage是一般情况下默认的压缩内核映像文件,压缩vmlinux,加上一段解压启动代码得到.而uImage则是使用工具mkimage对普通的压缩内核映像文件(zImage) ...

  7. 利用VisualVm和JMX远程监控Java进程

    自Java 6开始,Java程序启动时都会在JVM内部启动一个JMX agent,JMX agent会启动一个MBean server组件,把MBeans(Java平台标准的MBean + 你自己创建 ...

  8. node连接myslq

    var mysql = require('mysql'); var connection = mysql.createConnection({ host : 'localhost', user : ' ...

  9. 5.4 C++重载输入与输出操作符

    参考:http://www.weixueyuan.net/view/6382.html 总结: 在C++中,系统已经对左移操作符“<<”和右移操作符“>>”分别进行了重载,使其 ...

  10. Java流对象理解

    马士兵老师,曾在Java的学习过程中,将Java的流比作管道,认为很贴切,在此笔者也建议读者在学习过程中作类似比喻,形象化的学习 Java根据数据流向的不同分为输入流和输出流: Java根据处理数据类 ...