标签:create while int main fine log 斐波那契 n+2 def
利用这个递推式:\({F_{n+2}\choose F_{n+1}}={{1 1}\choose{1 0}}{F_{n+1}\choose{F_{n}}}\)
记\({{1 1}\choose{1 0}}\)为\(A\)
得到\({F_{n+1}\choose{F_{n}}}=A^n{F_1\choose F_0}=A^n{1\choose 0}\)
// Created by CAD on 2020/2/18.
#include <bits/stdc++.h>
#define ll long long
using namespace std;
typedef vector<vector<ll> > mat;
const int mod=1e9+7;
mat operator *(mat &a,mat &b){
mat ans(a.size(),vector<ll>(b[0].size()));
for(int i=0;i<a.size();++i)
for(int j=0;j<b[0].size();++j)
for(int k=0;k<b.size();++k)
ans[i][j]=(a[i][j]+a[i][k]*b[k][j])%mod;
return ans;
}
mat qpow(mat x,ll n){
mat ans(x.size(),vector<ll>(x.size()));
for(int i=0;i<x.size();++i)
ans[i][i]=1;
while(n){
if(n&1) ans=ans*x;
n>>=1,x=x*x;
}
return ans;
}
int main()
{
ll n;cin>>n;
mat a(2,vector<ll>(2));
a[0][0]=1,a[0][1]=1;
a[1][0]=1,a[1][1]=0;
mat ans=qpow(a,n);
cout<<ans[1][0]<<'\n';
return 0;
}
标签:create while int main fine log 斐波那契 n+2 def
原文地址:https://www.cnblogs.com/CADCADCAD/p/12329977.html