[P1962] Fibonacci number (matrix fast power)

The meaning of problems: obtaining f (n) mod value of 1000000007, n is in the long long range;

Solution: Rapid power matrix;

1. Fast Power matrix;

 = X …………①

Similarly:

 =  X …………②

We put into ① ② type style

Too:

  = X

Attach Code:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
#define rg register
using namespace std;
const int mod = 1e9+7;

ll n;
ll a[101][101],b[101][101];
ll ans[101][101];

inline void matrix(){
    memcpy(b,ans,sizeof(ans));
    memset(ans,0,sizeof(ans));
    for(rg int k=1;k<=2;++k)
    for(rg int i=1;i<=2;++i)
    for(rg int j=1;j<=2;++j) ans[i][j]=(ans[i][j]+(b[i][k]*a[k][j])%mod)%mod;
}

inline void matrix_(){
    memset(b,0,sizeof(b));
    for(rg int k=1;k<=2;++k)
    for(rg int i=1;i<=2;++i)
    for(rg int j=1;j<=2;++j) b[i][j]=(b[i][j]+(a[i][k]*a[k][j])%mod)%mod;
    memcpy(a,b,sizeof(b));
}

inline void matrix_ksm(ll bas){
    while(bas){
        if(bas&1) matrix();
        matrix_();
        bas>>=1;
    }
}

int main()
{
    scanf("%lld",&n);
    a[1][1]=a[1][2]=a[2][1]=1;
    ans[1][1]=ans[2][2]=1;
    matrix_ksm(n);
    printf("%lld",ans[2][1]);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/nnezgy/p/11515503.html