POJ 2234 Matches Game

烂漫一生 提交于 2019-11-26 20:59:07

Description

Here is a simple game. In this game, there are several piles of matches and two players. The two player play in turn. In each turn, one can choose a pile and take away arbitrary number of matches from the pile (Of course the number of matches, which is taken away, cannot be zero and cannot be larger than the number of matches in the chosen pile). If after a player’s turn, there is no match left, the player is the winner. Suppose that the two players are all very clear. Your job is to tell whether the player who plays first can win the game or not.

Input

The input consists of several lines, and in each line there is a test case. At the beginning of a line, there is an integer M (1 <= M <=20), which is the number of piles. Then comes M positive integers, which are not larger than 10000000. These M integers represent the number of matches in each pile.

Output

For each test case, output "Yes" in a single line, if the player who play first will win, otherwise output "No".

Sample Input

2 45 45
3 3 6 9

Sample Output

No
Yes

Source

POJ Monthly,readchild
 
思路在Bash博弈中,如果 n%(m+1)≠0 先手必胜
证明:
首先,如果n<=m,先手可以一次取完,先手必胜。如果n=m+1,那么由于一次最多只能取m个,所以,无论先手拿走多少个,后手都能够一次拿走剩余的物品,后手取胜取胜的方法:如果n = (m+1)*r+s,(r为任意自然数,s≤m),那么先手要拿走s个物品,如果后手拿走k(k≤m)个,那么先手再拿走m+1-k个,结果剩下(m+1)*(r-1)个,以后保持这样的取法,那么先手肯定获胜。总之,要保持给对手留下(m+1)的倍数,就能最后获
 
代码:
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

int x,y,n;

int main () {
	while(~scanf("%d",&n)) {
		y=0;
		for(int i=1; i<=n; i++) {
			scanf("%d",&x);
			y=y^x;
		}
		if(y==0)
			printf("No\n");
		else
			printf("Yes\n");
	}
	return 0;
}

 

 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!