bbqub

# 理解

F(0) = 0
F(1) = 1
F(n) = F(n - 1) + F(n - 2) (n >= 2)
(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, ...)

## 代码

``````#include<iostream>
#include<memory.h>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<list>
#include<map>
#include<algorithm>
typedef long long LL;
const LL maxn=1000+10;
const LL mod=1000000007;
const int N=2;
using namespace std;
struct Matrix
{
LL m[N][N];
};
Matrix A=
{
1,1,
1,0
};
Matrix I=
{
1,0,
0,1
};
Matrix multi(Matrix a,Matrix b)
{
Matrix c;
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
c.m[i][j]=0;
for(int k=0;k<N;k++)
c.m[i][j]+=a.m[i][k]*b.m[k][j]%mod;

c.m[i][j]%=mod;
}
}
return c;
}
Matrix power(Matrix A,int k)
{
Matrix ans=I,p=A;
while(k)
{
if(k&1)
{
ans=multi(ans,p);
k--;
}
k>>=1;
p=multi(p,p);
}
return ans;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
Matrix ans =power(A,n-1);
printf("%lld\n",ans,m[0][0]);
}
return 0;
}
``````

6 声望
0 粉丝
0 条评论