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;
}
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
杭州电子科技大学在线评测系统(杭电OJ)中的题目1000-1100是一系列编程题,我将分别进行回答。 1000题是一个简单的入门题,要求计算两个整数的和。我们可以使用一个简单的算法,读取输入的两个整数,然后将它们相加,最后输出结果即可。 1001题是一个稍微复杂一些的题目,要求实现字符串的逆序输出。我们可以使用一个循环来逐个读取输入的字符,然后将这些字符存储在一个数组中。最后,我们可以倒序遍历数组并将字符依次输出,实现字符串的逆序输出。 1002题是一个求最大公约数的问题。我们可以使用辗转相除法来解决,即先求出两个数的余数,然后将被除数更新为除数,将除数更新为余数,直至两个数的余数为0。最后的被除数就是最大公约数。 1003题是一个比较简单的排序问题。我们可以使用冒泡排序算法来解决,即每次比较相邻的两个元素,如果它们的顺序错误就交换它们的位置。重复这个过程直至整个数组有序。 1100题是一个动态规划问题,要求计算给定序列中的最长上升子序列的长度。我们可以使用一个数组dp来保存到达每个位置的最长上升子序列的长度。每当遍历到一个位置时,我们可以将其和之前的位置比较,如果比之前位置的值大,则将其更新为之前位置的值加1,最后返回dp数组的最大值即可。 以上是对杭电OJ1000-1100题目的简要回答,涉及了一些基本的编程知识和算法思想。希望对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值