Step jumping Problem | Fibonacci recursion complexity problem | integer division problem

Source: Internet
Author: User
Tags integer division
[Question] There are n steps at a time. You can skip one or two steps at a time. How many hops are there? What if I can skip 1, 2, or 3 at a time? [Analysis] defines F (n) as the hop method for the nth Step,
  1. When n = 1, F (n) = 1;
  2. When n = 2, F (n) = 2, because there are two steps for each hop and two steps for one hop
  3. When N> 2, if the first hop Level 1, then to jump to section N there, you need to jump the remaining N-1 section, F (N-1); if the first hop Level 2, then the rest of the N-2 needs to be completed, there is F (N-2) Jump method, the two start jump method is mutually exclusive, so by F (n) = f (N-1) + f (N-2)
[Code]
long long fibonacci_soluction(int N){if(N <=2)return N;elsereturn fibonacci_soluction(N-1)+fibonacci_soluction(N-2);}

Similarly, if you can skip level 1, level 2, or level 3 at a time

  1. F (n) = 1 n = 1
  2. F (n) = 2 n = 2
  3. F (n) = 4 N = 3
  4. F (n) = f (N-1) + f (N-2) + f (N-3) n> 3
The Code is as follows:
long long fibonacci_soluction2(int N){if(N == 1)return 1;if(N == 2)return 2;if(N == 3)return 4;elsereturn fibonacci_soluction2(N-1)+fibonacci_soluction2(N-2)+fibonacci_soluction2(N-3);}
[Thinking] the complexity of the time space of the Fibonacci series is converted to the recursive solution of the Fibonacci series. It seems to be a pretty solution, but the time O (2 ^ N) and space complexity O (N) I dare not compliment you. Instead, I use recursion. the time complexity is O (n), and the space complexity is O (1)
long long fibonacci_soluction_rec(int N){if(N <= 2)return N;long long a, b, c;a = 1, b = 2;int i;for (int i = 3; i <= N; i++){c = a + b;a = b;b = c;}return c;}

But is this the best way to achieve the Fibonacci series? See f [N] 1 * f [n-1] + 1 * f [N-2] 1 1 f [N-1] | 1 1 | ^ (N-1) f [1]
----- = ------------- = *
F [n-1]
1 * f [n-1] + 0 * f [n-1] 1 0 f [N-2] | 1 0 |
F [0]
Transformation to the form of N-1 power of 1-2 dimensional matrix
1 0
/AN/2 * When an/2 N is an even number
An =
\ A (n-1)/2 * a (n-1)/2 when n is an odd number
In this way, you only need to log in n times.
*/

Struct matrix_2by2 {matrix_2by2 (long a_00 = 0, long a_01 = 0, long a_10 = 0, long a_11 = 0): a00 (a_00), A01 (a_01 ), a10 (a_10), A11 (a_11) // For ease of writing, give struct a constructor {} long a00; long A01; long A10; long A11 ;}; typedef struct matrix_2by2 matrix_2by2; matrix_2by2 matrix_2by2_multiple (const matrix_2by2 A, const matrix_2by2 B) {return matrix_2by2 (. a00 * B. a00 +. a01 * B. a10,. a00 * B. a01 +. a01 * B. a11,. a10 * B. a00 +. a11 * B. a10,. a10 * B. a01 +. a11 * B. a11);} matrix_2by2 matrix_pow (int n) {assert (n> 0); matrix_2by2 matrix; If (n = 1) {return matrix_2by2 (1, 1, 1, 0);} If (N % 2 = 0) matrix = matrix_2by2_multiple (matrix_pow (n/2), matrix_pow (n/2 )); if (N % 2 = 1) {matrix = matrix_2by2_multiple (matrix_pow (N-1)/2), matrix_pow (N-1)/2); matrix = matrix_2by2_multiple (matrix, matrix_2by2 (,);} return matrix;} long matrix_fabonacci (int n) {If (n <= 2) return N; matrix_2by2 res = matrix_pow (N ); return res. a00 ;}

[Test code]

# Include <time. h ># include <iostream> using namespace STD; int main () {time_t begin, end; begin = clock (); fibonacci_soluction_rec (10000000); End = clock (); cout <"when using recursive Fibonacci (100)" <End-begin <Endl; begin = clock (); matrix_fabonacci (10000000); End = clock (); cout <"when using matrix Fibonacci (100)" <End-begin <Endl ;}

Somehow, the matrix method I tested is more than 50 times slower than the recursive method! Have time to consider ~~


[Thinking] when the number of hop steps more than three, this is converted to the problem of integer planning for detailed analysis see other people's blog: http://www.cnblogs.com/dolphin0520/archive/2011/04/04/2005098.html
long long equationCount(int n, int m){assert(m >0 && n>0);if (m==1 || n==1)return 1;if (n <= m)return 1+equationCount(n, n-1);if (n > m)return equationCount(n-m, m)+equationCount(n, m-1);}

Of course, the above Recursive Algorithms need to be optimized. (3 days of writing on and off = _ =)

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.