4388 Stone Game II

#博弈论的问题基本上是同一种想法——找规律。往往是通过很特殊的情况往一般情况衍生,或者说分析出特殊情况的必胜状态,一般情况往特殊情况靠拢。

#还有特别使用到的是二进制,许多问题的分类都借助了二进制的优势,不论怎样表述的物品,比如格子、石头堆,二进制可以自动分类形成2^0,2^1等堆,在这种情况下进行异或操作有着天然的优势。

#对于这道题而言,发现规律:所有石头数量的奇偶性与石头堆数的奇偶性异或,如果为1则先手胜。

#问题

Stone Game II

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


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

#AC码

#include
using namespace std;
int main()
{
    int t,i,j,n,te;
    cin>>t;
    for(i=1;i<=t;i++)
    {
        cin>>n;
        int cnt=0;
        for(j=1;j<=n;j++)
        {
            cin>>te;
            while(te)
            {
                if(te&1)
                    cnt++;
                te=te>>1;
            }
        }
        if((n%2)^(cnt%2))
            cout<<"Case "<

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