hdu Hakase and Nano (博弈)

Problem C. Hakase and Nano

Hakase and Nano are playing an ancient pebble game (pebble is a kind of rock). There are n packs of pebbles, and the i-th pack contains ai pebbles. They take turns to pick up pebbles. In each turn, they can choose a pack arbitrarily and pick up at least one pebble in this pack. The person who takes the last pebble wins.

This time, Hakase cheats. In each turn, she must pick pebbles following the rules twice continuously. Suppose both players play optimally, can you tell whether Hakase will win?

Input

The first line contains an integer T (1 ≤ T ≤ 20) representing the number of test cases.

For each test case, the first line of description contains two integers n(1 ≤ n ≤ 106) and d (d = 1 or d = 2). If d = 1, Hakase takes first and if d = 2, Nano takes first. n represents the number of pebble packs.

The second line contains n integers, the i-th integer ai (1 ≤ ai ≤ 109) represents the number of pebbles in the i-th pebble pack.

Output

For each test case, print “Yes” or “No” in one line. If Hakase can win, print “Yes”, otherwise, print “No”.

Example

standard input

 

standard output

2

3 1

1 1 2

3 2

1 1 2

Yes

No

 

题意:有n堆石子,每一堆有ai个石子,Hakase 和 Nano轮流从石堆里取石子,谁取到最后一个石子,谁就获胜。Hakase每次必须从两堆石子内取任意石子,每堆至少取一颗石子,Nano每次只能从一堆内取任意石子,至少取一颗石子。现在告诉你谁先手,双方均采取最佳的策略,如果Hakese能赢输出Yes,否则输出No。

思路:和NIM博弈不太一样,因为双方的取法不一样,所以我们可以从例子中找寻规律。

发现:如果Hakese先手,他失败的情况只有一种:石子的个数是3的倍数,且每堆石子的个数必须都是1.

现在考虑Nano先手,Nano想赢,那就必须将当前的局面构造成Hakese失败的情况。即当前局面石头堆的个数为3的倍数加1且

石子数为1的堆数>=总的石子数-1,或者是当前局面石头堆的个数为3的倍数且石子数为1的堆数=总的石子数-1

AC代码:

#include <cstdio>

using namespace std;
const int MAXN = 1e6+10; 
int a[MAXN];
int main(){
	int T;scanf("%d",&T);
	while(T--){
		int n,d;scanf("%d%d",&n,&d);
		int cnt=0;
		for(int i=0;i<n;i++){
			scanf("%d",&a[i]);
			if(a[i]==1)
				cnt++;
		}
		if(d==1){
			if(n%3==0 && cnt==n){
				printf("No\n");
			}else{
				printf("Yes\n");
			}
		}else{
			if((n%3==1 && cnt>=n-1)||(n%3==0 &&cnt==n-1)){
				printf("No\n");
			}else {
				printf("Yes\n");
			}
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Rainbow_storm/article/details/83269842