# An example of the optimization matrix of Fibonacci sequence _c language

Source: Internet
Author: User
Tags pow

When you do programming topics, you often encounter the "Fibonacci sequence" related topics, especially in doing OJ. Here are some ways to:

(i) Recursion

Recursion is the slowest occurrence of repetitive computations, with a exponentially of time complexity.

Copy Code code as follows:

Long long FAC (int n)
{
if (n==1) return 1;
else if (n==2) return 2;
else return FAC (n-1) +FAC (n-2);
}

(ii) Circulation

Using temporary variables to save the middle of the calculation process, speed up the operation.

Copy Code code as follows:

Long long FAC (int n)
{
Long Long a=1,b=2,c;
if (n==1) return 1;
else if (n==2) return 2;
Else
{
for (int i=3;i<=n;i++)
{
C=a+b; 　　A=b; B=c;
}
}
return b;
}

(c) Matrix multiplication + Space Change time (reduce multiplication, modulo operation)

The recursive formula for the sequence is: F (1) =1,f (2) =2,f (n) =f (n-1) +f (n-2) (n>=3)

Represented by a matrix:

Further, we can derive the direct derivation formula:

Because the matrix multiplication satisfies the binding law, the 64,32,16,8,4,2,1 of the matrix can be given in advance to speed up the execution time of the program. (Some of the topics require modulo operations, but also can be carried out in advance). The given matrix power is related to the binary because, as the following formula exists, the solution satisfies xi={0 or 1}:

In order to guarantee that the solution satisfies xi={0 or 1}, the solution to the above formula is from right to left, that is, the solution order is xn,xn-1,xn-2,...., x1,x0.

The complete code implementation is as follows:

Copy Code code as follows:

Solve FAC (n)%100000 where n is a positive integer greater than or equal to 3
#include <stdio.h>
#include <math.h>
Long long fac_tmp[6][4]={///storing matrix power
Location: 00 01 10 11
{24578,78309,78309,46269},///32 power%100000
{1597,987,987,610},///16 power%100000
{34,21,21,13},///8 power%100000
{5,3,3,2},///4 power%100000
{2,1,1,1},///2 power%100000
{1,1,1,0},///1 power%100000
};
void FAC (int);

int main ()
{
int n;
scanf ("%d", &n);
FAC (n);
return 1;
}

void fac (int k)///k>=3
{
int i;
Long Long t00=1,t01=1,t10=1,t11=0; ///represents the 1 power of the matrix
long long a,b,c,d;
The     k=k-3; ///formula is a n-2 power, (t00,t01,t10,t11) representing a power of 1 times. So altogether minus 3 times
for (i=k;i>=32;i=i-32)   ///to K that is greater than or equal to 32;
{
a= (t00*fac_tmp[0][0]+t01*fac_tmp[0][2])% 100000;
b= (t00*fac_tmp[0][1]+t01*fac_tmp[0][3])%100000;
c= (t10*fac_tmp[0][0]+t11*fac_tmp[0][2])%100000;
d= (t10*fac_tmp[0][1]+t11*fac_tmp[0][3])%100000;
t00=a;  t01=b;  t10=c;t11=d;
}

i=4;
while (i>=0)    ///for K (16,8,4,2,1) less than 32;
{
if (k>= (Long Long) POW (2,i))  /// If k is greater than the power
{

of a certain 2

A= (t00*fac_tmp[5-i][0]+t01*fac_tmp[5-i][2])%100000; (5-i): The position of the I power of the 2 of the matrix in the array fac_tmp is fac_tmp[5-i]
B= (t00*fac_tmp[5-i][1]+t01*fac_tmp[5-i][3])%100000;
C= (t10*fac_tmp[5-i][0]+t11*fac_tmp[5-i][2])%100000;
D= (t10*fac_tmp[5-i][1]+t11*fac_tmp[5-i][3])%100000;
T00=a;  T01=b; T10=c;t11=d;
k=k-(int) pow (2,i);
}
i--;
}

A= (t00*2+t01*1)%100000;
printf ("%lld\n", a);
}

Related Keywords:

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.