Game theory of turning |poj2234maches game|

Source: Internet
Author: User

The following goes from the wizard's blog.
time limit:
1000ms
Memory Limit:
65536kB

Description
Here's a simple game. In this game, there is several piles of matches and players. The both 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 match ES, which is taken away, cannot be zero and cannot being larger than the number of matches in the chosen pile). If after a player's turn, there is no match left and the player is the winner. Suppose that the players is all very clear. Your job is-to-tell whether the player, plays first can win the game or not.
input
The input consists of several lines, and in each line there are a test case. At the beginning of a line, there are an integer M (1 <= m <=20), and which is the number of piles. Then comes M positive integers, which is not larger than 10000000. These M integers represent the number of matches in each pile.
Output
For each test case, the output is "Yes" in a single line, and if the player who play first would win, otherwise output "No".
Sample Input

2

3 3 6 9

Sample Output

No

Yes


(a) This is a game theory Nim game topic, we first start from the simple situation analysis, and then gradually in-depth.

(1) Only a bunch, the initiator wins
(2) Two piles, a total of 2 matches, the next win (take a pile can be)
two heaps, altogether 3 matches, the initiator wins (1+2 pattern, takes 1 in 2 piles, translates into the former case)
two piles, a pile of 2, another pile of 2, the side win (take a pile, or take 1 will lose)
two heaps, a pile of 1, another heap of 3, the initiator wins (1+3 pattern, in 3 a heap of 2, converted to 2 root case)
          ...   
two piles, the number of matches in the heap is equal, the win
two stacks, the number of matches in the heap varies, the initiator wins
(3) Three piles, altogether 3 matches, the initiator wins
three piles, a total of 4 matches, the initiator wins
three piles, altogether 5 matches, the 1.1.3 type initiator wins (because can always convert to two stacks equal situation)
three piles, altogether 5 matches, the 2.2.1 type initiator wins (because can always convert to two stacks equal situation)
three piles, altogether 6 matches, the 2.2.2 type wins
three piles, altogether 6 matches, the 1.1.4 type wins
three piles, altogether 6 matches, 1.2.3 type to win

It is not easy to find the law by simple analysis, it is not a simple odd-even relationship (I have mistakenly introduced the parity-win relationship)

(b) Let's go into a little bit more

we found in two piles, we found an interesting rule, that is, if the two stacks of equal, then the side win, if the two stacks of different numbers, the initiator side win (think about why?) )。 But three piles of time seemingly does not work, because as the simplest 1+1+1 pattern, obviously is each heap number equal, but should be the initiator side wins. But we will soon find out that for three piles of cases, if the number of piles is equal, it must be the initiator wins (think clearly why?). )。 Further promotion, we can get the following conclusions:

heap for odd number, the number of matches in each heap is equal, must be the first party win

heap for an even number, the number of matches in each heap is equal, must be the side to win

(iii) We begin to deal with the most difficult part

we can now handle the situation when matches are equal in all the heaps, but the matches are not equal in each heap? Here again we see some incredible links in math. We are now re-examining and defining the game's outcome.

(1) If it is time for someone to take a match and the match is gone, the person loses and is set to P-pattern

(2) If it is time for someone to take a match, he can finish the match, then the person wins, set to N-Pattern

(3) If it is time for someone to take a match, he can turn the current pattern into a p pattern, then the pattern is N-pattern

(4) If it's time for someone to take a match, he can't turn the current pattern into a P-pattern, so this pattern is P-patterns

let's deduce a theorem: A pattern is recorded as (x1,x2,..., xn), and the order is independent, and this pattern is P-pattern when and only if X1^x2^...^xn = 0. Where ^ is bitwise xor or arithmetic

derivation: Corresponding to the above 4 cases (for the sake of brevity of the narrative, not very rigorous proof, but the direct assumption that the conclusion is correct, and then explain the working mechanism of the theorem)

(1) when (x1,x2,..., xn) is all 0 o'clock, the pattern is P-pattern, at this time x1 ^ x2 ^ ... ^ xn = 0 established.

(2) when (x1,x2,..., xn) Only one is not 0, the pattern is n-pattern, at this time x1 ^ x2 ^ ... ^ xn = 0 is not established.

(3) when (x1,x2,..., xn) is a P-pattern, the X1,..., xn is not all 0. (to disprove the law)

Suppose x1 ^ x2 ^ ... ^ xn = p, and p is not 0,

In the binary representation of the p, the leftmost 1 (the highest bit of 1) is in the left-to-right number of K-bits.

since p is the result of an XOR operation, then X1, x2, ..., at least one of the 1 K-bits in the Xn ,

It may be advisable to set the K-digit of Xi to 1, then the XI ^ p K-bit is 0, then the XI > xi^p is clearly established.

In other words, there is a certain way to make the number of matches in the I heap change to xi^p.

Set x1 ^ x2 ^ ... ^ xn = p, then x1 ^ x2 ^ ... ^ xn ^ p = 0.

then the current pattern can be converted to a P-pattern, that is, the current pattern of n-pattern, contradiction

Therefore, there must be p=0.

(4) when x1 ^ x2 ^ ... ^ xn = 0 o'clock, if there is a moving XI change to Xi ', and x1^x2^....^xi ' ^...^xn = 0,

then the elimination of the XOR, Xi = Xi ', that is, a match is not taken, which is not allowed, so

the current pattern can only be P pattern

with this powerful theorem, the problem is well solved:

#include <iostream>using namespacestd; intMain () {intM;  while(cin>>M) {/*input*/              intpile[ -] ={};  for(intI=0; i<m; ++i) {cin>>pile[i];} /*Calculate*/              intresult =0;  for(intI=0; i<m; ++i) {result^=Pile[i]; }                    /*Output*/              if(Result) cout<<"Yes"<<Endl; Elsecout<<"No"<<Endl; }                return 0; }  

Game theory of turning |poj2234maches game|

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.