Fibonacci sequence, stair problem, cow problem

Source: Internet
Author: User

Fibonacci Series: The Fibonacci sequence, also known as the Golden Section, refers to a sequence of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 、...... Mathematically, the Fibonacci sequence is defined as a recursive method: F (0) =0,f (1) =1,f (n) =f (n-1) +f (n-2) (n≥2,n∈n*) [from Baidu Encyclopedia Http://baike.baidu.com/link?url= 8lktktallugdme610zio0dajs3cceaopxicfvh_y47_i_xdrgzygcrzsodd1oho726fjnpwkqzkqc7piugu_i_]

Question 1:

We are all very familiar with the Fibonacci classic question, by recursion formula f (n) = f (n-1) + f (n-2), we can find the nth item f (n)in linear time, now consider the enhanced version of the Fibonacci retracement, We require that the range of the number of n is a nonnegative integer in the range of int, design an efficient algorithm to calculate the nth item f (n). The first Fibonacci number is f (0) = 1. Given a non-negative integer, return the nth item of the Fibonacci sequence, and, to prevent overflow, set the result mod 1000000007. [From Niu Ke net]

Method 1:

The most basic method is the simplest recursion, will produce a lot of repetition, time complexity is too large, not recommended.

Method 2:

It has been deduced that:

When n is even, f (n) = f (N/2) ^2 + f (N/2-1) ^2

When n is odd, f (n) = f (N/2) ^2 + 2*f (N/2) *f (N/2-1)

This can be used:

int Fibonacci (int n) {long I, J;        if (n = = 0 | | n = = 1) return 1;        i = Fibonacci (N/2);        j = Fibonacci (N/2-1);        if (n%2 = = 0) return (i*i + j*j)%1000000007; else return (2*i*j+ i*i)%1000000007;}

This algorithm is similar to binary lookup, if the time complexity of Method 1 is N, the time complexity of Method 2 may be log2 N (indeterminate here )

Although this approach has improved, it is still not ideal and repeated computations are excessive.

Note: The i*i part of this method is a multiplication between large integers, a large number and consumes a lot of time.

Method 3:

Fibonacci Classic problems can also be solved by means of a matrix method (not listed here), there is a method of 2 compared to a space-time tradeoff.

Since the Fibonacci matrix is roughly a[2][2]={1,1,1,0}, the operations between large integers are reduced, which is also an improvement over Method 2.

For problems where n is even or odd:

To avoid multiplication between large integers, it is based on the matrix multiplication rule:

When M is even: arr (M/2, a), and then matrix A*a

When M is odd: arr (M/2, a), and then matrix A*a*{1, 1, 1, 0}


/* Matrix processing function */void arr (int m, long long  (*D) [2])          {            long long a[2][ 2];            long long e[2][2];             long long c[2][2] = {1,  1, 1, 0};            if  (M  == 1)  {                 d[0][0] = 1;                 d[0][1] = 1;                 d[1][0] = 1;                 d[1][1] = 0;                 return;            }             arr (M/2, a);             e[0][0] =  (a[0][0]*a[0][0] + a[0][1]*a[1][ 0])%1000000007;            e[0][1] =  (a[ 0][0]*A[0][1] + A[0][1]*A[1][1])%1000000007;             e[1][0] =  (a[1][0]*a[0][0] + a[1][1]*a[1][0])%1000000007;             e[1][1] =  (A[1][0]*a[0][1] + a [1] [1]*a[1][1])%1000000007;            if  (m%2  != 0) &nbsP {                d[0][0]  =  (e[0][0]*c[0][0] + e[0][1]*c[1][0])%1000000007;                 d[0][1] =  (E[0][0]*c[0][1] + e[0][1] *C[1][1])%1000000007;                 d[1][0] =  (e[1][0]*c[0][0] + e[1][1]*c[1][0])%1000000007;                 d[1][1] =  (e[1][0]*c[0 ][1] + E[1][1]*C[1][1])%1000000007;             }            else {             d[0][0] = e[0][0];             d[0][1] = e[0][1];             d[1][0] = e[1][0];             d[1][1] = e[1][1];             }        }/* Call Function */int fibonacci (int n)  {         // write code here         long long b[2][2] = {1, 1, 1, 0};         if  (n == 0 | |  N == 1)             return 1;         arr (n - 1, b);         return  (b[0][0] + b[0][1])%1000000007;}


Question 2:

Now there is a tall building, but the elevator is out of trouble, but you can only take the stairs upstairs, depending on your leg length, you can walk 1 or 2 stairs at a time, you are known to go to the level of stairs to your destination, please calculate the number of the plan you go to the destination floor, because the building is very high, so the range of n is a positive integer in the

Given the total number of stairs n, return the number of scenarios. To prevent overflow, return the value of the result mod 1000000007. [From Niu Ke net]

Analysis:

One can only walk one step at a time, or two steps. There are a total of two big ways to step N, from N-1 (N-1 > 0), from N-2 (N-2 > 0). Then the way to reach the step n is the way to reach N-1 plus the way to reach N-2, i.e. f (n) = f (n-1) + f (n-2). Then the problem is the Fibonacci sequence, which is exactly the same way.


Question 3:

In the farm, the cow family is a very large family, for the family of cows, from the year it was born, the third year can mature, mature cows can give birth to a small cow every year. That is, a cow can be a mother for the third year since birth. There was only one cow at the start of the farm, and it began to give birth to a heifer the following year. Design an efficient algorithm that returns the total number of cows in the nth year, the range of known n is a positive integer in the range of int.

Given a positive integer n, return the total number of cows in the nth year, and in order to prevent overflow, set the result mod 1000000007. [From Niu Ke net]

Analysis:

Cows are divided into mature and immature two, mature annual production of a son, immature need three years to mature before producing children.

Divide cows into three categories, mature, born one year, born two years

The number of cows in the nth year = three kinds of cows want to add

N-Year mature cow = year N-1 mature cow + N-1 year-old cow born two

N year of birth cows = year N-1 mature cows

N-year Two cows born = N-1 year-old cows = N-2 years of mature cows

So:

Full use of mature cows instead of cows born for one or two years:

N-year ripe cow = First N-1 year mature cow + first N-3 year mature cow

F (n) = f (n-1) + f (n-3)

N year of birth cows = year N-1 mature cows

N Year Two cows born = year N-2 mature cows

Nth year total number of cows = Nth year mature + first N-1 year mature cows + N-2 year mature cows

At this point, the relationship between the total number of cows and the mature cows

Multiply by Matrix.

1 0 1 F (n-1) f (n-1) + f (n-3) f (n)

1 0 0 X f (n-2) = f (n-1) = f (n-1)

0 1 0 F (n-3) F (n-2) f (n-2)



At this point, after matrix multiplication, n in F (n) is a smooth backward increment, the matrix satisfies this algorithm, the last total number of cows is: f (n) + f (n-1) + f (n-2)

This algorithm uses the 3*3 matrix, and the others are similar to question 2:

Void arr (int m, long long  (*D) [3])         {             long long a[3][3];             long long e[3][3];             long long c[3][3] = {1, 0 , 1, 1, 0, 0, 0, 1, 0};             if  (m == 1)  {                 d[0][0] = 1;                 d[0][1] = 0;                 d[0][2] = 1;                 d[1][0] = 1;                 d[1][1] = 0;                 d[1][2] = 0;                 d[2][0] = 0;                 d[2][1] =  1;                D[2][2]  = 0;                 return;            }             arr (M/2, a);             e[0][0] =  (a[0][0]*a[0][0] + a[0][1]*a[1][0] + a[0][2]*a[2][0])%1000000007;             e[0][1] =  (A[0][0]*a[0][1] + a [0] [1]*a[1][1] + a[0][2]*a[2][1])%1000000007;             e[0][2] =  (A[0][0]*a[0][2] + a[0][1]*a[1][2] + a[0][2]*a[2][2] )%1000000007;            e[1][0] =  (A[1] [0]*a[0][0] + a[1][1]*a[1][0] + a[1][2]*a[2][0])%1000000007;                e[1][1] =  (A[1][0]*a[0][1] + a [1] [1]*a[1][1] + a[1][2]*a[2][1])%1000000007;             e[1][2] =  (A[1][0]*a[0][2] + a[1][1]*a[1][2] + a[1][2]*a[2][2] )%1000000007;            e[2][0] =  (A[2][0]*a[0][0]  + a[2][1]*a[1][0] + a[2][2]*a[2][0])%1000000007;             e[2][1] =  (a[2][0]*a[0][1] + a[2][1]*a[1][1] +  A[2][2]*A[2][1])%1000000007;            e[2] [2] =  (a[2][0]*a[0][2] + a[2][1]*a[1][2] + a[2][2]*a[2][2])%1000000007;             if  (m%2 != 0)  {                 d[0][0] =  (E[0][0]*c[0][0] + e[0][1]*c[1][0] + e[0][2]*c[2][0])%1000000007;                 d[0][1] =  (E[0][0]*c[0][1]  + e[0][1]*c[1][1] + E[0][2]*C[2][1])%1000000007;                 d[0][2] =  (e[0][0]*c[0][2] + e[0][1]*c[1][2] + e[0 ][2]*C[2][2])%1000000007;                         d[1][0] =  (e[1][0]*c[0][0] + e [1] [1]*c[1][0] + e[1][2]*c[2][0])%1000000007;             d[1][1] =  (E[1][0]*c[0][1] + e[1][1]*c[1][1] + e[1][2]*c[2][1] )%1000000007;            d[1][2] =  (E[1] [0]*c[0][2] + e[1][1]*c[1][2] + e[1][2]*c[2][2])%1000000007;                          d[2][0] =  (e[2][0]*c[0][0] + e[2][1]*c[1][0] + e[2][2]*c[2][0])%1000000007;                 d[2][1] =  (e[2][ 0]*C[0][1] + E[2][1]*C[1][1] + E[2][2]*C[2][1])%1000000007;                 d[2][2] =  (e[2][0]*c[0][2] +  E[2][1]*C[1][2] + E[2][2]*C[2][2])%1000000007;             }            else {                 d[0][0] =  e[0][0];                 d[0][1] = e[0][1];                 d[0][2] = e[0][2];                         d[1][0] = e[1][0];             d[1][1] = e[1][1];             d[1][2] = e[1][2];                          d[2][0] = e[2][0];                d[2][1] = e[2][1];                 d[2][2] = e[2][2];             }        }    int  countsum (int n)  { &NBsp;      // write code here         long long b[3][3] = {1, 0, 1, 1, 0, 0, 0, 1 , 0};        int e[3] = {1, 1, 1};  F (3)  f (2)  f (1)         long long f[3];         //the first three to meet the conditions of the direct output         if   (n == 1)             return 1;         if  (n == 2)              return 2;        if  (n  == 3 )             return 3;     &Nbsp;   arr (n - 3 , b);         f[0]  = b[0][0]*e[0] + b[0][1]*e[1] + b[0][2]*e[2];         f[1] = b[1][0]*e[0] + b[1][1]*e[1] + b[1][2]*e[2];         F[2] = B[2][0]*E[0] + B[2][1]*E[1] + B[2][2] *e[2];        return  (F[0] + f[1] + f[2])  %1000000007;    }

Summarize:

The problem of dairy cows seems to have little relation with stair problems and can be transformed into matrix solution at last.

The matrix can simplify the redundant operation and realize the space turn time.

Fibonacci sequence, stair problem, cow problem

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.