HDU

We know that Daizhenyang is chasing a girlfriend. As we all know, whenever you chase a beautiful girl, there'll always be an opponent, or a rival. In order to take one step ahead in this chasing process, Daizhenyang decided to prove to the girl that he's better and more intelligent than any other chaser. So he arranged a simple game: Coin Flip Game. He invited the girl to be the judge.
In this game, n coins are set in a row, where n is smaller than 10^8. They took turns to flip coins, to flip one coin from head-up to tail-up or the other way around. Each turn, one can choose 1, 2 or 3 coins to flip, but the rightmost selected must be head-up before flipping operation. If one cannot make such a flip, he lost.
As we all know, Daizhenyang is a very smart guy (He's famous for his 26 problems and Graph Theory Unified Theory-Network Flow does it all ). So he will always choose the optimal strategy to win the game. And it's a very very bad news for all the competitors.
But the girl did not want to see that happen so easily, because she's not sure about her feelings towards him. So she wants to make Daizhenyang lose this game. She knows Daizhenyang will be the first to play the game. Your task is to help her determine whether her arrangement is a losable situation for Daizhenyang.
For simplicity, you are only told the position of head-up coins. And due to the girl's complicated emotions, the same coin may be described twice or more times. The other coins are tail-up, of course.
Coins are numbered from left to right, beginning with 0.

Input

Multiple test cases, for each test case, the first line contains only one integer n (0<=n<=100), representing the number of head-up coins. The second line has n integers a1, a2 … an (0<=ak<10^8) indicating the An-th coin is head up.

Output

Output a line for each test case, if it's a losable situation for Daizhenyang can, print "Yes", otherwise output "No" instead.

Sample Input

0
1
0
4
0 1 2 3

Sample Output

Yes
No
Yes

题解:

博弈论题经典做法:SG打表+找SG值规律。

打表代码:

#include <bits/stdc++.h>

using namespace std;

const int MAXN = 10005;

int SG[MAXN];
bool SGhash[MAXN];
 
inline int getSG(int x){
	memset(SGhash,false,sizeof SGhash);	
	SGhash[0] = true;//翻一个硬币 
	for(int i=0 ; i<x ; ++i)SGhash[SG[i]] = true;//翻两个硬币 
	for(int i=0 ; i<x ; ++i)
		for(int j=0 ; j<i ; ++j)SGhash[SG[i]^SG[j]] = true;//翻三个硬币 
	for(int i=0 ; ; ++i)if(!SGhash[i])return i;
}
 
inline void BuildSG(int x){
	memset(SG,0,sizeof SG);
	SG[0] = 1;//当head-up硬币位置在0时先手必胜 
	for(int i=1 ; i<=x ; ++i){
		SG[i] = getSG(i);
	}
}

int main(){
	
	BuildSG(100);
	for(int i=0 ; i<11 ; ++i)printf("%d:%d ",i,SG[i]);
	printf("
");
	for(int i=11 ; i<20 ; ++i)printf("%d:%d ",i,SG[i]);
	
	return 0;
}

因为ak很大,明显只能找规律。观察可以很明显的发现 SG[x]= 2*x 或者 2*x+1。从这出发,又发现所有SG函数值二进制表示的数中1的个数为奇数个(别问我咋发现的,这是神仙告诉我的_(:з」∠)_)。根据这一发现,对于输入的a,判断下2*a的二进制表示中1的个数奇偶性,如果为偶就加1。

代码:

#include <cstdio>
#include <algorithm>

using namespace std;

const int MAXN = 105;

bool Judge(int x){
	int num = 0;
	while(x){
		if(x&1)++num;
		x /= 2;
	}
	if(num % 2 == 0)return true;
	return false;
}

int getSG(int x){
	x *= 2;
	if(Judge(x))++x;
	return x;
}

int board[MAXN];

int main(){
	int N;
	while(scanf("%d",&N) == 1){
		if(N == 0){
			printf("YES
");
			continue;
		}
		for(int i=0 ; i<N ; ++i)scanf("%d",&board[i]);
		sort(board,board+N);
		int re = getSG(board[0]);
		for(int i=1 ; i<N ; ++i){
			if(board[i] == board[i-1])continue;
			re ^= getSG(board[i]);
		}
		if(re)printf("No
");
		else printf("Yes
");
	}
	return 0;
}
原文地址:https://www.cnblogs.com/vocaloid01/p/9513989.html