Link
Solve
(O(n))的递推显然是要挂的,考虑矩阵乘法
推出式子就好了,实在模板
注意要从(n-2)开始乘
(n<2)的特判一下
Code
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL TT=1e9+7;
LL N;
struct AS{
LL a[3][3];
AS(){memset(a,0,sizeof a);}
void clear(){memset(a,0,sizeof a);a[2][1]=a[1][1]=a[1][2]=1;}
AS operator *(const AS B){
AS C;
for(int i=1;i<3;i++)
for(int j=1;j<3;j++)
for(int k=1;k<3;k++)
C.a[i][j]=(C.a[i][j]+a[i][k]*B.a[k][j]%TT)%TT;
return C;
}
};
int main(){
freopen("P1962.in","r",stdin);
freopen("P1962.out","w",stdout);
scanf("%lld
",&N);
if(N==1){printf("%d
",1);return 0;}
if(N==0){printf("%d
",0);return 0;}
AS s,w;s.a[1][1]=s.a[1][2]=1;w.clear();
N-=2;
while(N){
if(N&1)s=s*w;
w=w*w;N>>=1;
}
printf("%lld
",s.a[1][1]%TT);
return 0;
}