基准时间限制:1
秒 空间限制:131072
KB 分值:
0
难度:基础题
斐波那契数列的定义如下:
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, ...)
给出n,求F(n),由于结果很大,输出F(n) % 1000000009的结果即可。
Input
输入1个数n(1 <= n <= 10^18)。
Output
输出F(n) % 1000000009的结果。
Input示例
11
Output示例
89
大神链接:https://zhuanlan.zhihu.com/p/19768646
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<limits.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<math.h>
#include<map>
using namespace std;
#define Mod 1000000009
struct node
{
__int64 c[2][2];
};
node a;
node work(node x,node y)
{
node t={0};
int i,j,k;
for(i=0;i<2;i++)
for(j=0;j<2;j++)
for(k=0;k<2;k++)
{
t.c[i][j]+=(x.c[i][k]*y.c[k][j])%Mod;
t.c[i][j]%=Mod;
}
return t;
}
node Pow(node x,__int64 n)
{
node temp=x;
if(n<0) return temp;
while(n)
{
if(n&1)
{
temp=work(temp,x);
n--;
}
x=work(x,x);
n/=2;
}
return temp;
}
int main()
{
__int64 n;
scanf("%I64d",&n);
a.c[0][0]=1;
a.c[0][1]=1;
a.c[1][0]=1;
a.c[1][1]=0;
node ans=Pow(a,n-2);
printf("%I64d\n",ans.c[0][0]);
}