n silver coin + 1 gold coin problem __ Algorithm

Source: Internet
Author: User

The original question: (Classmate interview face problem), if there are 20 pieces of silver, and 1 gold coins, there are A and B in turn according to the following rules to take: a first B, each time can only take 1~4 pieces, the silver coins can be taken after the gold coins, and finally take the gold coins, ask a how much can be guaranteed to win the first time.

The answer is: A for the first time to win 4 pieces.

The Ph. D. In the lab tried to explain it to me by group theory, but in the next few years on the ocean side of Mathematics & theory, he had not wet his knees and was too shallow to understand. After thinking for myself, I thought of a discriminant/general solution to this problem:

A + (Min + Max) * n + 1 = total. A for the first number of a, Min,max for the range of desirable values, 1~4 is Min~max, Min = 1, Max = 4,n is an unrelated constant, total is 20 here.

So here: A + 5*n + 1 = 20 of the solution A is the first time a value is taken.

discriminant meaning: For the above, if there is a a∈[min, Max, then the first person to take/action will be able to win, otherwise, if not, that is a = 0, then take/action the person will win, that is, the first person will lose.

Find the solution to the problem: that is, in the case of a valid a, the value of a is solved.


Why.

The transformation of the original >>> problem is: How to get B to the last silver coin. So the first 20 silver coin's take/action process abstraction is a B a b .... A B. The focus of the problem is a (B a ...). b A) b. b Take the last 1, a take a, then become a + ... + 1 = 20. What about the middle value ... If you don't want to, then don't ask, let the middle of these values become and the final problem has nothing to do. That is, how to let the middle of the N multiple B a pairs of values and with the final result is irrelevant. If the value of the N multiple b pairs in the middle is constant, it has nothing to do with the result. n the value of multiple B-A pairs is constant, the value of the b+a should be constant. How to do it. B's value 1~4,a cannot control the value of B, but a allows b+a to be constant. B,a range of values are 1~4, and C is the range of 2~8, so that a+b = constant C of the value of only one, that is, c=5. So if a can not control B value, a as long as the middle of the process, the value is 5-b, you can make the entire intermediate process into a fixed constant. Then the process of taking/acting becomes a + constant + 1 = a + 5*n + 1 = 20. So the above discriminant/general solution is set up.

Other:

>>> on the formula can be proved, for n silver coins + 1 gold coins, when n=6,11,16,21 ..., a will lose, of course B to have the same as a will to win.


For other similar problems, the above equation can be deformed properly.

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.