Step2 for new Acmer learning the Fibonacci sequence

Source: Internet
Author: User

2015-3-19 time limit 2 days

The Fibonacci sequence refers to a sequence of numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... This sequence starts with the third item and each item is equal to the sum of the first two items. There's a lot of problems with the Fibonacci sequence. The prototype of the Fibonacci sequence is derived from the rabbit reproduction problem, before we take a look at the jumping steps of the problem of jumping stairs problem description

A step has a total of n levels, if you can jump 1 levels at a time, you can jump 2 levels.

How many total hops are in total, and the time complexity of the algorithm is analyzed.

Analysis and Solution

Consider the simplest case first. If there are only 1 steps, there is obviously only one way to jump. If there are 2 steps, there are two ways to jump: One is divided into two jumps, each jumping 1 levels, the other is to jump 2 levels at a time.

Now let's talk about the general situation. We consider the Hop method of N-Steps as the function of N, which is recorded as f (n).

    • When N>2, there are two different options when jumping for the first time:
      • First, only jump 1 level, at this time the number of hops is equal to the rest of the n-1 steps of the number of jumps, that is, f (n-1);
      • Another option is to jump 2 levels for the first time, at which point the number of hops equals the number of jumps of the n-2 steps left behind, i.e. F (n-2).

Thus the total number of different hops for N-Step steps f (n) =f (n-1) +f (n-2).

We summarize the above analysis with a formula as follows:

        /  1                             n = 1f(n)=      2                             n = 2        \  f(n-1) + f(n-2)               n > 2

The above problem is the Fibonacci series problem which we are familiar with normally. You can write code such as the following:

Long Long int N) {    int result[3] = {012};     if 2 )        return  result[n];     return 1 2 );}

So, what if a person can go up the stairs 1, 2, or 3 at a time? This time, the formula is written like this:

        / 1                                      n = 1f(n)=     2                                      n = 2          4                                      n = 3       //111, 12, 21, 3        \ f(n-1)+f(n-2)+f(n-3)                   n > 3
Rabbit Reproductive Problems

If there is a pair of rabbits, every one months give birth to a pair of rabbits, and the birth of each pair of rabbits in the third month after birth also gave birth to a pair of rabbits. So, starting with a pair of new rabbits, how many pairs of rabbits can be bred in a full year?

F1) =1(there is a pair of rabbits in the 1th month) F (2) =1(2nd month or a pair of rabbits) F (3) =2(there was a pair of rabbits, starting with the 3rd, a pair of rabbits each month) F (4) =3(there are two pairs of rabbits, there is a pair can be fertile) f (5) =5(There are 3 pairs of rabbits, the 3rd-month-born rabbit can also have children, so now there are two rabbits can have children) F (6) =8(There are 5 pairs of rabbits, the 4th-month birth of the rabbit can also have children, so now there are 3 of rabbits can give birth) ... As can be seen above, the logarithm of the nth-month rabbit is f (n)= f (N-1) + F (N-2); F (n-1) was the number of rabbits last month, which was the original. F (n-2) is the number of rabbits that can be born, that is, the number of extra. The 3rd month after the beginning of the first n-2 month is the nth month, at which time the rabbits of the first n-2 months are fertile.
think about it and look at it.

Think 2: If there is a pair of rabbits, every one months to give birth to a pair of rabbits, and the birth of each pair of rabbits in the birth of the first k months also gave birth to a pair of rabbits. So, starting with a pair of rabbits, how many pairs of rabbits can be bred in a year?

How to write the derivation formula in this case?

F (n) =f (1) +f (n-k+1) The number of nthmonths is exactlythe nth-1 month n-k+1 months thenumber of nth-k+1 months can be considered as the first n-k+ The 1-month-old rabbits that have the ability to reproduce and the total amount of rabbits that are not yet fertile will breed in the nth months.
think about it and look at it.

Practice:

http://acm.zcmu.edu.cn/JudgeOnline/problem.php?id=1214

http://acm.zcmu.edu.cn/JudgeOnline/problem.php?id=1347

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2290

Step2 for new Acmer learning the Fibonacci sequence

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.