传送门--斐波那契数列第N项
矩阵连乘
快速幂取模
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=2;
struct Matrix
{
long long int m[maxn][maxn];
};
long long int n;
Matrix P={1,1,
1,0};
Matrix I={1,0,
0,1};
Matrix Matrixmul(Matrix a,Matrix b)
{
int i,j,k;
Matrix c;
for(int i=0;i<maxn;i++)
{
for(int j=0;j<maxn;j++)
{
c.m[i][j]=0;
for(int k=0;k<maxn;k++)
{
c.m[i][j]=c.m[i][j]+(a.m[i][k]*b.m[k][j])% 1000000009;
}
c.m[i][j]%= 1000000009;
}
}
return c;
}
Matrix quickpow(long long int n)
{
Matrix m=P,b=I;
while(n>=1)
{
if(n&1)
{
b=Matrixmul(b,m);
}
n=n>>1;
m=Matrixmul(m,m);
}
return b;
}
int main()
{
while(scanf("%lld",&n)!=-1)
{
if(n==0)
printf("0\n");
else if(n==-1)
break;
else{ Matrix x=quickpow(n-2);
printf("%lld\n",(x.m[0][0]+x.m[0][1])% 1000000009);}
}
return 0;
}