Starting with Nim

Source: Internet
Author: User

Bashgame ):A pile of N items, two people take things from the pile of items in turn, requires at least one each time, a maximum of M, and finally win the light;
Obviously, if n = m + 1, a maximum of m items can be taken at a time. Therefore, no matter how many items are taken by the first accessor, the latter can take the remaining items at a time, the latter wins. Therefore, we discovered the rule of How to Win: If n = (m + 1) R + S, (R is an arbitrary natural number, S ≤ m ), the first accessors must take s items. If the second accessors take K (≤ m) items, the first accessors take m + 1-K items, and the result is (m + 1) (r-1), and later to maintain such a method, then the first accessors certainly win. In short, we need to leave a multiple (m + 1) to the opponent to win.

Wythoffgame ):There are several items in two stacks. Two people take the same number of items from one or both stacks in turn. It is required that at least one item be obtained at a time. There are no restrictions on the number of items, and the final winner wins. This is the NIM problem mentioned in this book;

Conclusion: The number of items in the two stacks is <a, B>. The N groups are insecure. <a (n), B (n)> the following conditions are met: a (n) + n = B (n) and a (n) = min (N-A) and B (n-1); where N: all positive integers, a (n) = {A1 ,..... A (n)}, B (n) = {B1 ..... B (n )};

Then, we can directly solve the problem based on the above conclusions. The initial values are a1 = 1, b1 = 2. for a given number of two items, we can determine whether the two items meet the unsafe conditions. If the two items meet the conditions, the attacker will be defeated first, otherwise, the first accessor will have a chance to win; algorithm complexity O (N );

Of course, the O (1) formula can solve the problem: a (n) = [(1 + SQRT (5)/2 * n], B (n) = [(3 + SQRT (5)/2 * n]; we can use a general formula to calculate all insecure situations;

The nature of the mandatory State in the problem: for mandatory number pairs (a, B), assume that a <B has two properties: 1. any natural number appears only once in several pairs, and the difference between any B-A only once. At the same time, we know that the difference between the nth point is N;

For more information about proof and subsequent processing, see this blog;

Wikipedia Nim reference;

There is another similar problem:There are n stacks of coins on the table. The number of coins is A1, A2,... an. Each time two players can take any coin from any pile (> 0 ), players who win the coins;

Conclusion: It is mandatory for beginners and only when A1 ^ A2 ^... ^ An = 0, where ^ represents an exclusive or (XOR) operation;

For more information about the proof, see this blog;

Exclusive or operation:

A = a ^ B ^ B

This bitwise operation can be used for exchange operations;

 1 struct RESULT 2 { 3     int a,b; 4 }; 5  6 void swap(RESULT &num) 7 { 8     num.a=num.a^num.b; 9     num.b=num.a^num.b;10     num.a=num.a^num.b;11 }12 13 int main( )14 {15     RESULT tmp;16     tmp.a=1;17     tmp.b=2;18     swap(tmp);19     cout<<tmp.a<<ends<<tmp.b<<endl;20     return 0;21 }
View code

Starting with Nim

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.