P1962 斐波那契数列

链接:Miku

----------------------

不显然的矩阵乘法

------------------

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
long long n;
long long m=1000000007;
long long c[5][5],a[5][5];
long long ans[5][5];
int main(){
    cin>>n;
    n--;
    ans[1][1]=ans[2][2]=1;
    a[1][2]=a[2][1]=a[2][2]=1;
    while(n){
        if(n%2){
            memset(c,0,sizeof(c));
            for(long long i=1;i<=2;++i)
                for(long long j=1;j<=2;++j)
                    for(long long k=1;k<=2;++k)
                        c[i][j]=(c[i][j]+ans[i][k]*a[k][j])%m;
            for(long long i=1;i<=2;++i)
                for(long long j=1;j<=2;++j)
                    ans[i][j]=c[i][j];
        }
        n/=2;
        memset(c,0,sizeof(c));
        for(long long i=1;i<=2;++i)
            for(long long j=1;j<=2;++j)
                for(long long k=1;k<=2;++k)
                    c[i][j]=(c[i][j]+a[i][k]*a[k][j])%m;
        for(long long i=1;i<=2;++i)
            for(long long j=1;j<=2;++j)
                a[i][j]=c[i][j];
    }
    cout<<(ans[1][1]+ans[2][1])%m;
    return 0;
}

 

posted @ 2020-03-14 17:56  Simex  阅读(233)  评论(10编辑  收藏  举报