矩阵快速幂 学习笔记

2022-09-19 08:13:53 浏览数 (1)

举例1:Fibonacci

题目传送门

题意

f[1]=1,f[2]=1,f[3]=2,f[4]=3 dots f[n]=f[n-1] f[n-2]

那么输入。

1leq n leq 2 times 10^9

因为:

f[i]=1 times f[i-1] 1 times f[i-2]
f[i-1]=1times f[i-1] 0 times f[i-2]

所以,我们可以发现递推式可以转化为矩阵运算:

left(begin{array}{rcl}f[i]\f[i-1]end{array} right) = left(begin{array}{rcl}1 quad 1\1quad 0end{array} right) times left(begin{array}{rcl}f[i-1]\f[i-2]end{array} right)=left(begin{array}{rcl}1 quad 1\1quad 0end{array} right)^2 times left(begin{array}{rcl}f[i-2]\f[i-3]end{array} right)

那么可得:

left(begin{array}{rcl}f[n]\f[n-1]end{array} right)=left(begin{array}{rcl}1 quad 1\1quad 0end{array} right)^{n-2} times left(begin{array}{rcl}f[2]\f[1]end{array} right)
代码语言:javascript复制
#include<algorithm>
#include<bitset>
#include<complex>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<iterator>
#include<limits>
#include<list>
#include<locale>
#include<map>
#include<memory>
#include<new>
#include<numeric>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<typeinfo>
#include<utility>
#include<valarray>
#include<vector>
#include<cctype>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<climits>
#include<clocale>
#include<cmath>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#define ll long long
using namespace std;
inline ll read(){
    ll res=0,f=1;char ch=getchar();
    while(ch<'0'ch>'9'){if(ch=='-') f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') res=res*10 ch-'0',ch=getchar();
    return res*f;
}
inline void write(ll x){
    if(x<0) putchar('-'),x=-x;
    if(x<10) putchar(x '0');
    else{
        write(x/10);
        putchar(x '0');
    }
}
ll n,m;
struct node{
    ll f[3][3];
}a,f,s;
void build(node &x){
    for(ll i=0;i<=1;i  ){
        for(ll j=0;j<=1;j  ){
            if(i==j) x.f[i][j]=1;
            else x.f[i][j]=0;
        }
    }
}
void Mul(node &x,node &y,node &z){
    memset(z.f,0,sizeof(z.f));
    for(ll i=0;i<=1;i  ){
        for(ll j=0;j<=1;j  ){
            if(x.f[i][j]!=0){
                for(ll k=0;k<=1;k  ){
                    z.f[i][k] =x.f[i][j]*y.f[j][k];
                    z.f[i][k]%=m;
                }
            }
        }
    }
}
node Pow(ll b){
    node res;
    build(res);
    node tmp=f,t;
    while(b){
        if(b%2==1) Mul(res,tmp,t),res=t;
        Mul(tmp,tmp,t),tmp=t,b>>=1; 
    }
    return res;
}
int main(){
    n=read();m=read();
    if(n<=2){
        puts("1");
        return 0;
    }
    f.f[0][0]=1;f.f[1][0]=1;f.f[0][1]=1;f.f[1][1]=0;
    node q=Pow(n-2);
    write((q.f[0][0] q.f[1][0] m)%m);
    return 0;
}
//(f[n]  )=(1 1)^(n-2)*(f[2])
//(f[n-1])=(1 0)      *(f[1])

举例2:Fibonacci求和

题目传送门

题意

f[n]quad modquad m

$f[n]=f[n-1] f[n-2]
f[n-1]=f[n-2] f[n-3],f[n]=2times f[n-2] f[n-3]
f[n-2]=f[n-3] f[n-4],f[n]=f[n-2] 2times f[n-3] f[n-4]
f[n-3]=f[n-4] f[n-5],f[n]=f[n-2] f[n-3] 2times f[n-4] f[n-5]

以此类推,可得:

f[n]=f[n-2] f[n-3] f[n-4] f[n-5] dots f[2] 2 times f[1]

那么:

f[n-2] f[n-3] f[n-4] f[n-5] dots f[2] f[1]=f[n]-f[1]
f[n-2] f[n-3] f[n-4] f[n-5] dots f[2] f[1]=f[n]-1

也就是:

f[n] f[n-1] f[n-2] f[n-3] dots f[2] f[1]=f[n 2]-1

所以只需要将上题代码改一改就好了:

代码语言:javascript复制
#include<algorithm>
#include<bitset>
#include<complex>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<iterator>
#include<limits>
#include<list>
#include<locale>
#include<map>
#include<memory>
#include<new>
#include<numeric>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<typeinfo>
#include<utility>
#include<valarray>
#include<vector>
#include<cctype>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<climits>
#include<clocale>
#include<cmath>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#define ll long long
using namespace std;
inline ll read(){
    ll res=0,f=1;char ch=getchar();
    while(ch<'0'ch>'9'){if(ch=='-') f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') res=res*10 ch-'0',ch=getchar();
    return res*f;
}
inline void write(ll x){
    if(x<0) putchar('-'),x=-x;
    if(x<10) putchar(x '0');
    else{
        write(x/10);
        putchar(x '0');
    }
}
ll n,m;
struct node{
    ll f[3][3];
}a,f,s;
void build(node &x){
    for(ll i=0;i<=1;i  ){
        for(ll j=0;j<=1;j  ){
            if(i==j) x.f[i][j]=1;
            else x.f[i][j]=0;
        }
    }
}
void Mul(node &x,node &y,node &z){
    memset(z.f,0,sizeof(z.f));
    for(ll i=0;i<=1;i  ){
        for(ll j=0;j<=1;j  ){
            if(x.f[i][j]!=0){
                for(ll k=0;k<=1;k  ){
                    z.f[i][k] =x.f[i][j]*y.f[j][k];
                    z.f[i][k]%=m;
                }
            }
        }
    }
}
node Pow(ll b){
    node res;
    build(res);
    node tmp=f,t;
    while(b){
        if(b%2==1) Mul(res,tmp,t),res=t;
        Mul(tmp,tmp,t),tmp=t,b>>=1; 
    }
    return res;
}
int main(){
    n=read();m=read();
    if(n<=2){
        puts("1");
        return 0;
    }
    f.f[0][0]=1;f.f[1][0]=1;f.f[0][1]=1;f.f[1][1]=0;
    node q=Pow(n);
    write((q.f[0][0] q.f[1][0] m-1)%m);
    return 0;
}

0 人点赞