19th questions (array, recursion ):

Question: Define the series of Fibonacci:

/0 n = 0

F (n) = 1 n = 1

/F (n-1) + f (n-2) n = 2

Enter N and use the fastest method to obtain the nth item of the series.

Idea: there is a problem with the code below for recursion and non-recursion, and the large numbers are not considered to cross-border. The returned value should be set to long type

The recursion speed is very slow.

/* 19th question (array, recursion): Question: Define the Fibonacci series as follows:/0 n = 0f (n) = 1 n = 1/F (n-1) + f (n-2) N = 2 input N, and calculate the nth item of the series in the fastest way. Start time 13: 05end time 13:16 */# include <stdio. h> // recursive int maid (int n) {Switch (n) {Case 0: Return 0; Case 1: return 1; default: Return maid (n-1) + maid (n-2);} // non-recursive int nonrecursionmaid (int n) {int A = 0, B = 1; Switch (n) {Case 0: return 0; Case 1: return 1; default: {for (INT I = 0; I <n-1; I ++) {int c = A + B; A = B; B = C;} return B ;}} int main () {// int F = Fibonacci (10000); int FF = nonrecursionfibonacci (10000 ); return 0 ;}

There is an O (logn) solution on the Internet

Http://leowzy.iteye.com/blog/787947

This is not the fastest way. The following describes a method in which the time complexity is O (logn. Before introducing this method, we first introduce a mathematical formula:

{F (N), F (n-1), F (n-1), F (n-2) }={ 1, 1,} n-1

(Note: {f (n + 1), F (N), F (N), F (n-1)} represents a matrix. In the matrix, the first column in the first row is F (n + 1), the second column in the first row is F (n), and the first column in the second row is F (n ), the second row and the second column are f (n-1 ).)

With this formula, F (n) is required. We only need to obtain the n-1 power of the matrix {1, 1,}, because the matrix is {1, 1, F (n) is the first column in the first row of the result given by the n-1 power of 1, 0 ). This mathematical formula is easily proved by mathematical induction. If you are interested, you may wish to prove it yourself.

The current problem is to evaluate the multiplication of the matrix {1, 1, 1, 0. If it is simple to start a loop from 0, the Npower will require N operations, not faster than the previous method. However, we can consider the following properties:

/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

To obtain the power of N, we first obtain the power of n/2, and then square the result of n/2. If we look at the question of the power of Npower as a big problem, we can think of n/2 as a small problem. This idea of breaking down a big problem into one or more small problems is called divide and conquer law. In this way, only logn operations are required to calculate the nth power.

To implement this method, you must first define a 2 × 2 matrix and define the multiplication and multiplication operations of the matrix. After these operations are defined, the rest will become very simple. The complete implementation code is as follows.

#include <cassert>///////////////////////////////////////////////////////////////////////// A 2 by 2 matrix///////////////////////////////////////////////////////////////////////struct Matrix2By2{ Matrix2By2 ( long long m00 = 0, long long m01 = 0, long long m10 = 0, long long m11 = 0 ) :m_00(m00), m_01(m01), m_10(m10), m_11(m11) { } long long m_00; long long m_01; long long m_10; long long m_11;};///////////////////////////////////////////////////////////////////////// Multiply two matrices// Input: matrix1 - the first matrix// matrix2 - the second matrix//Output: the production of two matrices///////////////////////////////////////////////////////////////////////Matrix2By2 MatrixMultiply( const Matrix2By2& matrix1, const Matrix2By2& matrix2){ return Matrix2By2( matrix1.m_00 * matrix2.m_00 + matrix1.m_01 * matrix2.m_10, matrix1.m_00 * matrix2.m_01 + matrix1.m_01 * matrix2.m_11, matrix1.m_10 * matrix2.m_00 + matrix1.m_11 * matrix2.m_10, matrix1.m_10 * matrix2.m_01 + matrix1.m_11 * matrix2.m_11);}///////////////////////////////////////////////////////////////////////// The nth power of matrix // 1 1// 1 0///////////////////////////////////////////////////////////////////////Matrix2By2 MatrixPower(unsigned int n){ assert(n > 0); Matrix2By2 matrix; if(n == 1) { matrix = Matrix2By2(1, 1, 1, 0); } else if(n % 2 == 0) { matrix = MatrixPower(n / 2); matrix = MatrixMultiply(matrix, matrix); } else if(n % 2 == 1) { matrix = MatrixPower((n - 1) / 2); matrix = MatrixMultiply(matrix, matrix); matrix = MatrixMultiply(matrix, Matrix2By2(1, 1, 1, 0)); } return matrix;}///////////////////////////////////////////////////////////////////////// Calculate the nth item of Fibonacci Series using devide and conquer///////////////////////////////////////////////////////////////////////long long Fibonacci_Solution3(unsigned int n){ int result[2] = {0, 1}; if(n < 2) return result[n]; Matrix2By2 PowerNMinus2 = MatrixPower(n - 1); return PowerNMinus2.m_00;}

[Programming question] Question: Define the input N of the Fibonacci series, and calculate the nth item of the series in the fastest way.