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.
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.
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
【题意】:有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;
}

本文介绍了一个名为StoneGameII的问题,这是一个涉及堆石子博弈的游戏。两名玩家轮流从石堆中取石子,并遵循特定的规则。文章分析了获胜策略,并提供了一段C++代码实现。

871

被折叠的 条评论
为什么被折叠?



