Stone Game II (HDU-4388)
题面
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.
输入
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.
输出
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).
样例输入
13
22
31 2
43
51 2 3
64
71 2 3 3
样例输出
1Case 1: No
2Case 2: Yes
3Case 3: No
提示
无
思路
对于每一堆,分解到2的幂就无法再次分解了,所以我们统计一下二进制下1的数量,cnt-1为奇数时先手胜,反之。
代码
1using namespace std;
2
3int bitcount(int x){
4 int ans=0;
5 while(x){
6 x &= x-1;
7 ans++;
8 }
9 return ans;
10}
11
12int main()
13{
14 int T; scanf("%d", &T);
15 for(int cs=1; cs<=T; cs++)
16 {
17 int n; scanf("%d", &n);
18
19 int nim = 0;
20 for(int i=0; i<n; i++){
21 int x; scanf("%d", &x);
22 nim ^= (bitcount(x)-1) & 1;
23 }
24 printf("Case %d: ", cs);
25 if(nim){
26 printf("Yes\n");
27 }else{
28 printf("No\n");
29 }
30 }
31 return 0;
32}