【矩阵快速幂】 斐波那契数列求解。

来源:互联网 发布:不配说爱我网络 编辑:程序博客网 时间:2024/05/21 06:58
               Fibonacci

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11123 Accepted: 7913
Description
In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …
An alternative formula for the Fibonacci sequence is
.
Given an integer n, your goal is to compute the last 4 digits of Fn.
Input
The input test file will contain multiple test cases. Each test case consists of a single line containing n (where 0 ≤ n ≤ 1,000,000,000). The end-of-file is denoted by a single line containing the number −1.
Output
For each test case, print the last four digits of Fn. If the last four digits of Fn are all zeros, print ‘0’; otherwise, omit any leading zeros (i.e., print Fn mod 10000).
Sample Input
0
9
999999999
1000000000
-1
Sample Output
0
34
626
6875
Hint
As a reminder, matrix multiplication is associative, and the product of two 2 × 2 matrices is given by
.
Also, note that raising any 2 × 2 matrix to the 0th power gives the identity matrix:
.

题意:给你一个求解第N个斐波那契数的公式。 让你求出Fn % 10000。

Solution:
都要比赛了,居然矩阵快速幂写得如此的不熟。。
思路:构造单位矩阵,使用矩阵快速幂。
PS
写矩阵乘法的时侯。

Matrix matrix_mul(Matrix A,Matrix B){    Matrix C;    memset(C.m,0,sizeof(C.m));    for (int i=0;i<=1;i++)        for (int k=0;k<=1;k++){            int r=A.m[i][k];                      for (int j=0;j<=1;j++)                C.m[i][j]=(C.m[i][j]+(r*B.m[k][j])%M)%M;        }    return C;}

这样的写法比下面的写法会快一些~

matrix multi(matrix a, matrix b){    matrix tmp;    for(int i = 0; i < 2; ++i)    {        for(int j = 0; j < 2; ++j)        {            tmp.m[i][j] = 0;            for(int k = 0; k < 2; ++k)                tmp.m[i][j] = (tmp.m[i][j] + a.m[i][k] * b.m[k][j]) % MOD;        }    }    return tmp;}

为什么呢。
大神说这是缓存读取数据比内存读取数据快得多的原因,
还不不懂。

www.jb51.net/aricle/36422.htm

这个地方有详细的解释~

接下来就是一个基本的矩阵快速幂,
注意事项都在程序里面写了,
注意多写写,增加熟练度。

#include<iostream>#include<cstdio>#include<cstring>using namespace std;struct Matrix{    int m[2][2];}ans,base;int n;void test(){    for (int i=0;i<=1;i++){        for (int j=0;j<=1;j++)            cout<<ans.m[i][j]<<' ';        cout<<endl;    }}/*    *This way to improve the efficiency*/const int M=1e4;Matrix matrix_mul(Matrix A,Matrix B){    Matrix C;    memset(C.m,0,sizeof(C.m));    for (int i=0;i<=1;i++)        for (int k=0;k<=1;k++){            int r=A.m[i][k];                      for (int j=0;j<=1;j++)                C.m[i][j]=(C.m[i][j]+(r*B.m[k][j])%M)%M;        }    return C;}/*    *The n_th power of matrix base.    *initialize base and initialize ans as identify matix.    *<pay attention here>*/ int power_matrix_mod(int n){    base.m[1][1]=0;    base.m[0][0]=base.m[0][1]=base.m[1][0]=1;    ans.m[0][0]=ans.m[1][1]=1;    ans.m[0][1]=ans.m[1][0]=0;    while(n){        if(n&1) ans=matrix_mul(ans,base);        base=matrix_mul(base,base);        n>>=1;    }    return ans.m[0][1];} int main(){    scanf("%d",&n);     printf("%d ",power_matrix_mod(n));    return 0;}
0 0
原创粉丝点击