#博弈論的問題基本上是同一種想法——找規律。往往是通過很特殊的情況往一般情況衍生,或者說分析出特殊情況的必勝狀態,一般情況往特殊情況靠攏。
#還有特別使用到的是二進制,許多問題的分類都藉助了二進制的優勢,不論怎樣表述的物品,比如格子、石頭堆,二進制可以自動分類形成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
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.
You can assume that all the number of stones in each pile will not exceed 100,000.
#AC碼
#include<iostream>
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 "<<i<<": "<<"Yes"<<endl;
else
cout<<"Case "<<i<<": "<<"No"<<endl;
}
}