Matches game
Time Limit: 1000 ms |
|
Memory limit: 65536 K |
Total submissions: 3934 |
|
Accepted: 2215 |
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
This is a random game question. A random game refers to such a game. Currently, there are any piles of stones, and the number of each pile of stones is also arbitrary. The two sides take turns to take the stones out of the game. The rules are as follows:
1> each step should take at least one stone; each step can only take part or all stones from a pile;
2> whoever gets the last stone wins
That is, the final state of the nimm game is: there is a pile of stones left at the end, and the current person taking the stones won the rest of the stones at a time. When there are two piles of stones left, each Heap has one, the current person is obviously defeated. Let's discuss another situation. When there are two piles of stones left, there is only one heap left, when there are more than one stone remaining in the other pile, the current number of people only needs to pull the pile of more than one stone to the remaining one, and the situation becomes the defeat situation just mentioned. In this process, if the winner of the current course has a winning strategy, it will force the situation to change from a winning situation to a defeated situation. That is to say, if the current situation is a defeated situation, then after two attempts, the situation returns to the defeat situation. The infinite descent method is different from the reverse proof method. In the process of "descent", the State remains unchanged (in the case that ferma conjecture n = 4, the state is that the triple is the solution of the equation). In the random game, the State is the situation, and the number of stones is decreased. Because the total number of stones is decreasing, so it will eventually "Drop" to the ultimate defeat: The last stone has been taken away by the winner, and there is no stone left. The problem is:
1> determine a method (or a function that maps from a certain situation to a set of {winning, losing}) to quickly determine whether the current situation is a winning (losing) situation;
2> is there a conversion state method that satisfies the rules (or a function that maps from a winning situation to a defeated situation) to meet the requirement that, as long as the current situation is not defeated, after a successful retrieval, it can be converted to a mandatory defeat.
If there are only two piles of stones, the above two problems are well solved:
1> when the number of stones in the two piles is equal, the current situation will be defeated; otherwise, it will be a winning situation. Obviously, the method is satisfied when both piles are 0;
2> if the current situation is a winning situation, the two piles of stones will be taken from the pile with more stones to make the number of stones equal. This will turn to the defeat situation.
However, for more than two piles of stones, 1> can be the same, but 2> is far from that simple, because it is unlikely that the number of all piles will be the same (unless the number of stones is the same except the maximum number of stones ). Therefore, we need to find a more effective method. A person named Zhang Yifei did this research. He thought of this method:
1> the number of stones in all heaps is represented by the binary number. When all these stones are in the same position or the result is 0, the current situation will be defeated; otherwise, it will win;
2> (theorem 0) a group of natural numbers must have a number, which is greater than or equal to the result of all other numbers by bit XOR. Therefore, in a win-win situation, because all numbers have an exclusive bitwise OR result greater than zero, this (if all other numbers have an exclusive bitwise result) is obtained at a time) when the number falls to the result of all others, the situation becomes defeated.
With the above theory, this question is very simple.
Source code
Problem:2234 |
User:Nathan96 |
Memory:188 K |
Time:0 ms |
Language:C ++ |
Result:Accepted |
Source: http://hi.baidu.com/nathan_96/blog/item/bf3465fbfd34259a59ee9094.html
I would like to waste a few words here. I am afraid I will forget about the characteristics of the exclusive or:
0 ^ anything = anything
1 ^ anything = non-Anything = 1-anything
A ^ A = 0
So, for example, there is a question: delete one of the 1000 numbers and ask which one is deleted. Method: obtain a for all the original values of the deleted values, B for all values of the deleted values, and a ^ B for the Deleted Values. The principle is a = a ^ x ^ X.
You can do this for similar cases.
Similar to Hamming distance
There is also an application that exchanges the location A-B of two variables without occupying additional space (3 exclusive or ):
A: 1010 B: 1100
A = a ^ B (B: 0110 B: 1100)
B = a ^ B (A: 0110 B: 1010 (formerly ))
A = a ^ B (A: 1100 (original B) B: 1010 (original ))
The obtained a B has been exchanged