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. 最新jquery+easyui_api培训文档

    目  录 1 Accordion(可折叠标签) 2 1.1 实例 2 1.2 参数 3 2 DateBox(日期框) 4 2.1 实例 4 2.2 参数 6 2.3 事件 6 2.4 方法 6 3 C ...

  2. python 小白学习(1)

    自定义错误类型 class XxxError(Exception): def __init__(self , message): self = Exception("xxxxx") ...

  3. ID基本操作(创建主页,复制主页,把主页应用到多个页面)5.11

    主页上的对象将会显示在应用在这个主页上的所有页面. 一.创建主页的方法: 1.页面面板,右上方点击,可以新建主页..前缀:用来识别页面面板中的各个页面所应用的主页.最多可输入四个字符.名称:输入主页跨 ...

  4. VS中常用快捷键

    常用的快捷键     这里仅列出一些个人觉得好用的快捷键: 调用智能提示:使用组合键“Ctrl+J” 注释/取消注释: 注释用组合键“Ctrl+K+C” 取消注释用组合键“Ctrl+K+U” 大小写转 ...

  5. Java的Properties类使用

    一.Java Properties类 Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置 ...

  6. [转载] JAVA面试题和项目面试核心要点精华总结(想进大公司必看)

    JAVA面试题和项目面试核心要点精华总结(想进大公司必看) JAVA面试题和项目面试核心要点精华总结(想进大公司必看)

  7. nginx+tomcat集群

    参考: 简单:http://blog.csdn.net/wang379275614/article/details/47778201 详细:http://www.jb51.net/article/77 ...

  8. js 判断某个元素是否隐藏或显示

    //判断某个元素是否显示 true:是 false:不是 var isVisible = $('#myDiv').is(':visible'); //判断某个元素是否隐藏 true:是 false:不 ...

  9. 异步IO(协程,消息循环队列)

    同步是CPU自己主动查看IO操作是否完成,异步是IO操作完成后发出信号通知CPU(CPU是被通知的) 阻塞与非阻塞的区别在于发起IO操作之后,CPU是等待IO操作完成再进行下一步操作,还是不等待去做其 ...

  10. 1.5 socket服务器传输文件

    socket服务器代码 # -*- coding: utf-8 -*-import sys,os,time,_thread from socket import * host = 'localhost ...