Light OJ 1296 - Again Stone Game (博弈sg函数递推)

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函数找规律,发现

   n为偶数时,sg(n)=n/2。把n为偶数的值全部删除,发现得到的数列和原数列一样。则当n

   为奇数时,sg(n)=sg(n/2),这里的n是向下取整的。

#include <iostream>
#include <stdio.h>
using namespace std;
int sg(int n)
{
    while(n&1) n>>=1; //当n为奇数时,递归到为偶数为止。
    return n>>1;
}
int main()
{
    int i,n,t,cas=1;
    cin>>t;
    while(t--)
    {
        int ans=0,data;
        cin>>n;
        for(i=0;i<n;i++)
        {
            cin>>data;
            ans^=sg(data);
        }
        if(ans)
        printf("Case %d: Alice\n",cas++);
        else
        printf("Case %d: Bob\n",cas++);
    }
    return 0;
}
时间: 2024-12-17 17:11:35

Light OJ 1296 - Again Stone Game (博弈sg函数递推)的相关文章

Light OJ 1296 - Again Stone Game

传送门 1296 - Again Stone Game    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB 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 a

UVa 10561 (SG函数 递推) Treblecross

如果已经有三个相邻的X,则先手已经输了. 如果有两个相邻的X或者两个X相隔一个.,那么先手一定胜. 除去上面两种情况,每个X周围两个格子不能再放X了,因为放完之后,队手下一轮再放一个就输了. 最后当“禁区”布满整行,不能再放X了,那个人就输了. 每放一个X,禁区会把它所在的线段“分割”开来,这若干个片段就可以看做若干个游戏的和. 设g(x)表示x个连续格子对应的SG函数值,递推来求g(x): g(x) = mex{ g(x-3), g(x-4), g(x-5), g(x-6) xor g(1),

UVA 10561 - Treblecross(博弈SG函数)

UVA 10561 - Treblecross 题目链接 题意:给定一个串,上面有'X'和'.',可以在'.'的位置放X,谁先放出3个'X'就赢了,求先手必胜的策略 思路:SG函数,每个串要是上面有一个X,周围的4个位置就是禁区了(放下去必败),所以可以以X分为几个子游戏去求SG函数的异或和进行判断,至于求策略,就是枚举每个位置就可以了 代码: #include <stdio.h> #include <string.h> #include <algorithm> usi

hdu 3032(博弈sg函数)

题意:与原来基本的尼姆博弈不同的是,可以将一堆石子分成两堆石子也算一步操作,其它的都是一样的. 分析:由于石子的堆数和每一堆石子的数量都很大,所以肯定不能用搜索去求sg函数,现在我们只能通过找规律的办法求得sg的规律. 通过打表找规律可以得到如下规律:if(x%4==0) sg[x]=x-1; if(x%4==1||x%4==2) sg[x]=x; if(x%4==3) sg[x] = x+1. 打表代码: #include<iostream> #include<cstdio> #

UVA 11534 - Say Goodbye to Tic-Tac-Toe(博弈sg函数)

UVA 11534 - Say Goodbye to Tic-Tac-Toe 题目链接 题意:给定一个序列,轮流放XO,要求不能有连续的XX或OO,最后一个放的人赢,问谁赢 思路:sg函数,每一段...看成一个子游戏,利用记忆化求sg值,记忆化的状态要记录下左边和右边是X还是O即可 代码: #include <stdio.h> #include <string.h> const int N = 105; int t, sg[3][3][N]; char str[N]; int ge

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

HDOJ 5724 博弈SG函数

链接: http://blog.csdn.net/tc_to_top/article/details/51958964 题意: n行20列的棋盘,对于每行,如果当前棋子右边没棋子,那可以直接放到右边,如果有就跳过放到其后面的第一个空位子,A先操作,最后谁无法操作则输,给定每行棋子状态,问先手是否必胜 题解: 组合博弈问题,直接sg函数,因为列只有20,可以状压搞,枚举每个状态,找到该状态下可行的操作然后标记 代码: 31 int sg[1 << 21]; 32 int vis[21]; 33

(转)博弈 SG函数

此文为以下博客做的摘要: https://blog.csdn.net/strangedbly/article/details/51137432 ---------------------------------------------------------------------------------------- 1.定义P-position和N-positon P表示Previous,N表示Next. 即上一个移动的人有必胜策略的局面是P-position,"先手必败"或&qu

Marbles(博弈SG函数)

Marbles Gym - 101908B Using marbles as a currency didn't go so well in Cubic?nia. In an attempt to make it up to his friends after stealing their marbles, the Emperor decided to invite them to a game night in his palace. Of course, the game uses marb