Hdu2516-Take the Stone game (Fibonacci game) "Game Two-point search"

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=2516

Take a stone game

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3248 Accepted Submission (s): 1897


problem Description1 Heap of stones There are N, two people take turns. The 1th time the first person can take any number, but not all of them. The number of stones to be taken at a later time cannot exceed twice times the number of previous fetching. The winner wins. The first to take the negative output "Second win". The first winner wins the output. InputThe input has more than one group. The 1th row of each group is 2<=n<2^31. N=0 exit. OutputFirst take the negative output "Second win". The first winner wins the output.
See sample Output. Sample Input213100000 Sample OutputSecond Winsecond Winfirst win

The puzzle: Find the law. After analysis, we can judge the 2,3,5,8,13,21 ... It can be found that the must-fail state is a Fibonacci sequence. Then, if n is the Fibonacci sequence element, it will fail;

Here's a look at the mathematical proof:

Just as "Wythoff game" requires "Beatty theorem" to help, it is necessary to use the "Zeckendorf theorem" (Ziekendorf theorem): Any positive integer can be expressed as a sum of several discontinuous Fibonacci numbers.

First look at the proof of the FIB sequence:

1, when i=2, the initiator can only take 1, obviously will be defeated, the conclusion is established.

2, assuming when i<=k, the conclusion is established.

When i=k+1, f[i] = f[k]+f[k-1].

Then we can think of this heap of stones as two piles, referred to as K-Heap and K-1 heap.

(Must be seen as two piles, because if the first time to take the number of stones greater than or equal to f[k-1], the hand can be directly completed f[k], because F[k] < 2*f[k-1])

For the K-1 heap, it is assumed that no matter how the initiator is taken, the last one is always taken. Let's look at the last stone number x in the bottom of the paper.

If the first time to take the number of stones Y>=F[K-1]/3, then this small heap of stones remaining less than 2y, that the hand can be directly finished, at this time x=f[k-1]-y, then x<=2/3*f[k-1].

Let's compare the size of 2/3*f[k-1] with 1/2*f[k]. That is 4*f[k-1] and 3*f[k] size, by the mathematical induction method is not difficult to draw, the latter large.

So we get, x<1/2*f[k].

That is, after taking the k-1 heap, the initiator can not take the K heap, so the game rules have not changed, then by the assumption that, for the K heap, the hand can still take the last one, so the hand to win.

That is i=k+1, the conclusion is still established.

For not the FIB number, the first decomposition.

When decomposing, take as large a number of Fibonacci as possible.

such as decomposition 85:85 between 55 and 89, so can be written 85=55+30, and then continue to decompose 30,30 between 21 and 34, so can be written 30=21+9,

And so on, finally decomposed into 85=55+21+8+1.

Then we can write N as n = f[a1]+f[a2]+......+f[ap]. (A1>A2>......>AP)

We make the initiator first take F[AP], that is, the smallest of the heap. A (P-1) > AP + 1, there is F[a (p-1)] > 2*f[ap] because the individual f is discontinuous. That is, you can only take F[a (p-1)] This heap, and can not be taken out at once.

At this time, the equivalent of the f[a to face this sub-game (only p-1) This heap of stones, and the first to take the next step, that the initiator must be able to take this heap of the last stone.

In the same vein, for each subsequent heap, the initiator can take this heap of the last stone, so as to win the game.

Code:

1#include <iostream>2#include <cstdio>3 4 using namespacestd;5 6 intf[ -],n;7 8 BOOLfind ();9 Ten intMain () { Onef[0]=2; Af[1]=3; -      for(intI=2;i< -; i++) {//44 is test-tested. -f[i]=f[i-1]+f[i-2]; the     } -      while(SCANF ("%d",&N), N) { -         if(Find ()) puts ("Second Win"); -         ElsePuts"First win"); +     } -     return 0; + } A BOOLfind () { at     intL=0, r= +, mid; -      while(l<=R) { -Mid= (l+r) >>1; -         if(N==f[mid])return true; -         Else if(N<f[mid]) r=mid-1; -         ElseL=mid+1; in     } -     return false; to}

Hdu2516-Take the Stone game (Fibonacci game) "Game Two-point search"

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.