HDU4388 Stone Game II (找规律博弈)

33 篇文章 1 订阅
24 篇文章 0 订阅

Stone Game II

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 401    Accepted Submission(s): 230


Problem Description
  Stone Game II comes. It needs two players to play this game. There are some piles of stones on the desk at the beginning. Two players move the stones in turn. At each step of the game the player should do the following operations.
  First, choose a pile of stones. (We assume that the number of stones in this pile is n)
  Second, take some stones from this pile. Assume the number of stones left in this pile is k. The player must ensure that 0 < k < n and (k XOR n) < n, otherwise he loses.
  At last, add a new pile of size (k XOR n). Now the player can add a pile of size ((2*k) XOR n) instead of (k XOR n) (However, there is only one opportunity for each player in each game).
The first player who can't do these operations loses. Suppose two players will do their best in the game, you are asked to write a program to determine who will win the game.
 

Input
  The first line contains the number T of test cases (T<=150). The first line of each test cases contains an integer number n (n<=50), denoting the number of piles. The following n integers describe the number of stones in each pile at the beginning of the game. 
You can assume that all the number of stones in each pile will not exceed 100,000.
 

Output
  For each test case, print the case number and the answer. if the first player will win the game print "Yes"(quotes for clarity) in a single line, otherwise print "No"(quotes for clarity).
 

Sample Input
  
  
3 2 1 2 3 1 2 3 4 1 2 3 3
 

Sample Output
  
  
Case 1: No Case 2: Yes Case 3: No
 

Author
WHU
 

Source
 
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4388

【题意】:有n堆石子,每堆石子有ai个,两个选手轮流取石子,取石子有以下两步,第一步:假设当对第i堆石子进行取石子操作后,第i堆石子还剩k个,k应该满足的条件为:0<k<ai,且 k ^ ai < ai;第二步:当对第i堆进行操作后,会增加一堆数量为k^ai的石子,每个选手都有一个技能可以把该堆石子变成2*k^ai,该技能当且仅当只能使用一次。

【分析】假设当前石子只有一堆,有m个石子,先不考虑k^m这个条件,可以得到,如果m的二进制表示有奇数个1则后手必胜,否则先手必胜。再来考虑k^m这个条件,可以得出该条件中二进制1的个数等价m-k,故该条件没有影响。对于n堆石子来说,只需要判断这n堆石子每堆ai二进制个数的奇偶即可。

【代码如下】

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int n,t,cas;

int main(){
    scanf("%d",&t);
    while(t --){
        scanf("%d",&n);
        int x,ans=0;
        for(int i = 0; i < n; i ++){
            scanf("%d",&x);
            int cnt = 0;
            while(x){
                if(x % 2) cnt ++;
                x /= 2;
            }
            if(cnt) cnt --;
            ans += cnt;
        }
        printf("Case %d: ",++ cas);
        if(ans % 2) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值