2. Fibonacci Game

Source: Internet
Author: User

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

This time we are the first to see the topic, yes, this water problem without any technical content drops, using the Fibonacci game.

Here's another game. Ha, Fibonacci game, the little friends are puzzled, not the Fibonacci sequence, how come another Fibonacci game, yes! Hey, they are the same drop, is the use of Fibonacci sequence of knowledge, but before the code, we first look at the principle.

Reproduced in:

n = 2 o'clock output second;
n = 3 o'clock is also the output second;
n = 4 o'clock, the first person wants to win must first Take 1, then the remaining stone number is 3, at this time no matter how to take the second person, the first person can win, output firstly;
n = 5 o'clock, first can not win, because he takes 2 o'clock, second directly take out the remaining 3 will win, when he takes 1 o'clock, this becomes the case of N 4, so the output is second;
n = 6 o'clock, first only remove 1, you can let the situation into the case of N 5, so the output is first;
n = 7 o'clock, first takes out 2, the situation becomes N 5, so first wins, so the output is first;
n = 8 o'clock, when first takes 1, the situation becomes 7 of the situation, the second person can win, at the first take 2, the situation becomes N 6 get the situation, is also the second person to win, take 3, second directly take out the remaining 5, so n = 8 o'clock, the output is second;
............
From the above analysis can be seen, N is 2, 3, 5, 8, these are output second, that is, must be defeated, careful people will find these meet Fibonacci number law, you can infer that 13 is also a must defeat point.
With the aid of the "Zeckendorf theorem" (Ziekendorf theorem): Any positive integer can be expressed as a sum of several discontinuous Fibonacci. N=12, as long as who can make the stone left 8 and this time take the son not more than 3 can win. Therefore, 12 can be regarded as 8+4, 8 as a station, equivalent to 4 for the "Asthma operation." Another example is that the 13,13=8+5,5 is going to fail, and that 13 is also a must-fail state. In other words, as long as the Fibonacci number, is a must lose points.
So we can use the Fibonacci number formula: Fib[i] = fib[i-1] + fib[i-2], as long as n is the Fibonacci number of the output second.

1#include <stdio.h>2#include <iostream>3#include <algorithm>4#include <string.h>5 using namespaceStdintfi[ $];6 voidFe () {7fi[0]=2;8fi[1]=3;9  for(intI=2;i< $; i++)Tenfi[i]=fi[i-1]+fi[i-2]; One } A intMain () - {Fe (); -     intN; the      while(cin>>n&&N) { -             intflag=0; -          for(intI=0;i< $; i++){ -             if(fi[i]==N) +{cout<<"Second Win"<<endl;flag=1; Break;} -  +         } A        if(flag==0) cout<<"First win"<<Endl; at     } -}
View Code

2. Fibonacci 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.