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堆数字,然后每堆数字都代表这一堆有多少个棋子,你每次至少取一个棋子,但是只能选择一堆。好,那我们来讨论把。
当n==1时,先手必胜(全部取光)
当n==2时,需要判断两个堆的数字情况,例如每堆都是1,那么后手赢了(一人一个)
当n==2时,但是一堆是1另外一对是x,则先手赢(先手需要在x那堆取x-1个就赢了)
当n==2时,但是两堆没有一个是1,但是两堆的数量相同,那么先手输了(因为无论先手你做什么操作,后手只需要模仿你,在另外一堆上面进行一样的操作,你就输了)
当n==2时,两堆不相等并且均不等于1时,那么先手必胜(你想一下啊,你是不是可以把两堆取成一样的,然后你就进入到你掌控世界的节奏当中了
当n==3时,三堆都是1,不用说肯定先手赢了。
当n==3时,两堆是1,另外一堆是x,那么先手赢了(把x那堆取光,然后进入1,1的那种情况了)
当n==3时,一堆是1,另外两堆相等并且不等于1的时候,先手赢了(先手把1的那堆取光,就变成了上面的两堆相等的情况)
当n==3时,一堆是1,另外两堆不相等并且不等于1的时候后手赢了(先手把一堆取光,那么剩下两堆就变得相等了,要是先把两堆变得相等,那么自己就会成为被模仿的对象)
当n==3时,三堆均不相等,并且都不等于1的时候,先手赢(先手可以把任意一堆变成1,然后变成所谓的“后手”形成上面说的那种情况)
当然还有很多,我现在就不列举了,开始找规律,我先初始化一个标记等于0,如果标记等于1就是先手赢,然后标记是0,就是后手赢,我需要让标记通过某种操作和正确结果对应。
当n==1时,看不出来,当n==2时,你发现结果总是围绕着1展开,于是二进制出现,你会发现当两堆均相等时候,先手输了,剩下的都是先手赢,然后当n==3时,你会发现只要被后手形成只有两堆并且相等那么先手就输了,所以他其实在判断相等的数字个数。
好了我们发现二进制这个异或的好东西,可以直接判断两个数字是否相等,相等是0值不变,不相等是1。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
int main()
{
int n;
while(~scanf("%d",&n))
{
int flag=0;
long long x;
for(int i=1;i<=n;i++)
{
scanf("%lld",&x);
flag^=x;
}
if(flag)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}