HDU4388 Stone Game II (找规律博弈)

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
2012 Multi-University Training Contest 9
 
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4388

【题意】:有n堆石子,每堆石子有ai个,两个选手轮流取石子,取石子有以下两步,第一步:假设当对第i堆石子进行取石子操作后,第i堆石子还剩k个,k应该满足的条件为:0

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

【代码如下】

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




你可能感兴趣的:(ACM,博弈,博弈论)