Matches Game
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5114 Accepted: 2874
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
题意:
给出n堆火柴棒,第i堆有a[i]根火柴棒.两个人轮流取.每人每次只能从某一堆中取若干根火柴棒,问先手能否有必胜的策略.
分析:
首先显而易见的是,由于玩家都不是2...所以若先手无必胜策略,则必输.
考虑n个数的异或和,即sum=a[1] xor a[2] xor a[3] xor ... xor a[n] ;
显然,当sum大于0时,先手可以取走某一堆中的若干根火柴使得sum=0;此后每次后手取火柴后,sum又会大于0,先手又能使得sum>0;
如此反复,后手总面对sum=0的情况,所以后手也会面对火柴总数为0的情况.
反之,当sum等于0时,先手必输.