LIGHT OJ 1199 - Partitioning Game

传送门

1199 - Partitioning Game

   PDF (English) problem=1199" style="color:rgb(79,107,114)">Statistics

problem=1199" style="color:rgb(79,107,114)">Forum

Time Limit: 4 second(s) Memory Limit: 32 MB

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

Output for Sample Input


3

1

4

3

1 2 3

1

7


Case 1: Bob

Case 2: Alice

Case 3: Bob

Explanation

In case 1, Alice has only 1 move, she divides the pile with 4 cells into two unequal piles, where one pile has 1 cell and the other pile has 3 cells. Now it‘s Bob‘s turn. Bob divides the pile with 3 cells into two piles, where one pile has 1 cell and another
pile has 2 cells. So, now there are three piles having cells 1, 1, 2. And Alice loses, since she doesn‘t have any moves now.

题目大意:

有n堆石子(1<=n<=100),每一堆分别有ai个石子(1<=ai<=10000),一次操作能够使一堆石子变成两堆数目不相等(注意是不相等)的石子,最后不能操作就算输,问先手赢还是后手赢。

解题思路:

就是一个SG函数,提到SG函数这个就是求一下 当前状态的下一个状态,又由于 这 n 堆石子是相互独立的,没有影响 所以说 能够开用SG函数,

依据SG定理,如果 当前堆中有 m块石子 那么他的下一状态就可能有 {1,m-1},{2,n-2},...,{(m-1)/2,m-(m-1)/2}(把每一种情况都想到
而且分析出来)。

然后分完的那些 a和b块石子又能够进行分,以此类推。那么SG(x) = mex{ SG(1)^SG(x-1), SG(2)^SG(x-2),...,
SG((x-1)/2)^SG(x-(x-1)/2) },

然后我们要求的就是 SG[a[0]]^SG[a[1]]^...^SG[a[n-1]],假设结果是0就是 后手赢,否则 先手赢

My Code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 10000+5;
int sg[MAXN];
int hash[MAXN];
void Get_sg()///模板
{
    memset(sg, 0, sizeof(sg));
    for(int i=1; i<MAXN; i++)
    {
        memset(hash, 0, sizeof(hash));
        for(int j=1; j*2<i; j++)
        {
            hash[sg[j]^sg[i-j]] = 1;
        }
        int j;
        for(j=0; j<MAXN; j++)
            if(!hash[j])
                break;
        sg[i] = j;
    }
}
int main()
{
    Get_sg();
    int T;
    scanf("%d",&T);
    for(int cas=1; cas<=T; cas++)
    {
        int m, sum = 0;
        scanf("%d",&m);
        for(int i=0; i<m; i++)
        {
            int x;
            scanf("%d",&x);
            sum ^= sg[x];
        }
        if(sum)
            printf("Case %d: Alice\n",cas);
        else
            printf("Case %d: Bob\n",cas);
    }
    return 0;
}
 

时间: 2024-10-27 01:20:35

LIGHT OJ 1199 - Partitioning Game的相关文章

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

D - Partitioning Game Time Limit:4000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Description Alice and Bob are playing a strange game. The rules of the game are: Initially there are n piles. A pile is formed by some cell

light oj 1236 【大数分解】

给定一个大数,分解质因数,每个质因子的个数为e1,e2,e3,--em, 则结果为((1+2*e1)*(1+2*e2)--(1+2*em)+1)/2. //light oj 1236 大数分解素因子 #include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> #include <math.h> #include <ctype.h> #i

[2016-04-21][light]OJ[1234][Harmonic Number]

时间:2016-04-21 22:18:26 星期四 题目编号:[2016-04-21][light]OJ[1234][Harmonic Number] 题目大意:求∑nk=11kn∈(1,108),精确到10?8求∑k=1n1kn∈(1,108),精确到10?8 分析: 想法是打表,然后输出,但是直接打表会爆内存 解决办法,就是每隔100个来打表,节省1100的空间,然后从那个值开始计算到当前值解决办法,就是每隔100个来打表,节省1100的空间,然后从那个值开始计算到当前值 对应的整百就是n

Light OJ 1411 Rip Van Winkle`s Code 线段树成段更新

题目来源:Light OJ 1411 Rip Van Winkle`s Code 题意:3中操作 1种查询 求区间和 其中每次可以把一段区间从左到右加上1,2,3,...或者从右到左加上...3,2,1 或者把某个区间的数都置为v 思路:我是加了6个域 add是这段区间每个数都要加上add  add是这么来的 对与123456...这个等差数列 可能要分为2个区间 那么我就分成123和123 两个右边的等差数列每个数还应该加上3 所以右区间add加3 v是这个区间都要置为v 他的优先级最高 b是

Light OJ 1168 Wishing Snake 强连通缩点+哈密顿通路

题目来源:Light OJ 1168 Wishing Snake 题意:有点难看懂题意 看了一个小时再加别人的代码才懂意思 从0开始 输入的那些每一对u v 都要经过 就是从0到到达那些点 思路:首先缩点 每一个强连通分量里面的点都是可达的 缩点后的图是有向无环图 如果从0这个强连通分量可以出去到2个强连通分量 那么这两个强连通分量是不可能相互可达 所以可行的方案就是所有的强连通分量连成一线 一个一个串起来 除了第一个 出度是1入度是0和最后一个出度是0入度是1 其他都是入度等于出度是1 特判只

Jan&#39;s light oj 01--二分搜索篇

碰到的一般题型:1.准确值二分查找,或者三分查找(类似二次函数的模型). 2.与计算几何相结合答案精度要求比较高的二分查找,有时与圆有关系时需要用到反三角函数利用 角度解题. 3.不好直接求解的一类计数问题,利用二分直接枚举可能的结果,再检查是否符合题目要求. 4.区间求解,即利用两次二分分别查找有序序列左右上下限,再求差算出总个数. 题型知识补充: 1. 三分的一般写法: 1 double thfind(double left,double right) 2 { 3 double midmid

light oj 1422 - Halloween Costumes (区间dp)

1422 - Halloween Costumes PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Gappu has a very busy weekend ahead of him. Because, next weekend is Halloween, and he is planning to attend as many parties as he can. Since it's Ha

Light OJ 1341 Aladdin and the Flying Carpet

It's said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a powerful Genie. Here we are concerned about the first mystery. Aladdin was about to enter to a magical cave, led by the evil sorcerer who disguised hi

Light OJ 1114 Easily Readable 字典树

题目来源:Light OJ 1114 Easily Readable 题意:求一个句子有多少种组成方案 只要满足每个单词的首尾字符一样 中间顺序可以变化 思路:每个单词除了首尾 中间的字符排序 然后插入字典树 记录每个单词的数量 输入一个句子 每个单词也排序之后查找 根据乘法原理 答案就是每个单词的数量之积 #include <iostream> #include <cstring> #include <cstdio> #include <algorithm>