POJ 2234 Matches Game

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/han_hhh/article/details/82014475

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

问题简述:输入的第一行是堆数n,然后跟着n个数分别为每堆的火柴数。一个玩家可以选择一个堆,并从该堆取任意火柴,如果一个玩家取了火柴后没有火柴留下,那么该玩家胜利。如果先取的玩家胜利输出Yes否则输出No

分析:

1.如果只有一堆,则先取的玩家将这堆火柴全取完获得胜利 Yes

2. 如果有两堆,每堆的数量分别为n1,n2

①:n1=n2第一个玩家取一部分火柴,第二个玩家通过取与第一个玩家相等数量的火柴获得胜利 No

②:n1≠n2第一个玩家先把两堆多出来的火柴取走,再取与第二个玩家相等数量的火柴获得胜利 Yes

3.有n堆,

猜想:玩家1能够在非平衡状态下取胜,玩家2能够在平衡状态下取胜。

火柴数分别为N1、N2、N3、N4、Nn,火柴的堆数都可以用二进制来表示,要求表示出来的火柴堆数的二进制位数相等,不够的前边补零

     N1=a1 a2 a3 ……an

     N2=b1 b2 b3…… bn

     N3=m1 m2 m3…… mn

平衡态:a1+b1+……+m1 为偶数

               a2+b2+……+m2 为偶数

               an+bn+……+mn 为偶数

巧妙之处:从平衡态到异或运算的转化。n堆火柴对应n个二进制数,连续进行n-1次异或运算,若结果非零则说明存在非平衡态,玩家一获胜;若为0说明所有位平衡,玩家二获胜

#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
int main(){
   int n,val;
   while(~scanf("%d",&n)){
        int a=0;
        for(int i=0;i<n;i++){
            scanf("%d",&val);
             a^=val;
        }
        if(a!=0){                //异或后不等于0说明存在非平衡态
             printf("Yes\n");}
        else
             printf("No\n");

   }




    return 0;
}

猜你喜欢

转载自blog.csdn.net/han_hhh/article/details/82014475