题目传送门
题意: 对于数列f[1]=1,f[2]=1,f[i]=f[i-1]+f[i-2] (i>=3),求第n项对1e9+7取模的结果。
思路: 之前写的模板好像有点问题 ,模板题,距阵快速幂加速递推。
代码:
#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ls p<<1
#define rs p<<1|1
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll long long
#define int long long
#define pii pair<int,int>
#define ull unsigned long long
#define all(x) x.begin(),x.end()
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ct cerr<<"Time elapsed:"<<1.0*clock()/CLOCKS_PER_SEC<<"s.\n";
char *fs,*ft,buf[1<<20];
#define gc() (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<20,stdin),fs==ft))?0:*fs++;
inline int read(){int x=0,f=1;char ch=gc();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=gc();}
return x*f;}
using namespace std;
const int N=1e5+5;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
const double eps=1e-7;
const double PI=acos(-1);
struct node
{
int m,l,v[3][3];
};
node mul(node a,node b)
{
node res;
res.m=a.m;res.l=b.l;
for(int i=1;i<=res.m;i++)
{
for(int j=1;j<=res.l;j++)
{
res.v[i][j]=0;
for(int k=1;k<=res.l;k++)
{
res.v[i][j]=(res.v[i][j]+a.v[i][k]*b.v[k][j])%mod;
}
}
}
return res;
}
int qpow(int n)
{
node t,p;
t.m=2;t.l=2;t.v[1][1]=t.v[1][2]=t.v[2][1]=1;t.v[2][2]=0;
p.m=1;p.l=2;p.v[1][1]=p.v[1][2]=1;
while(n)
{
if(n&1)
p=mul(p,t);
t=mul(t,t);
n>>=1;
}
return p.v[1][2]%mod;
}
signed main()
{
int n;
cin>>n;
cout<<qpow(n-1)<<endl;
}