题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1242
N比较大,用矩阵快速幂解决。
与POJ 3070题目一样。
AC代码:
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
using namespace std;
typedef long long LL;
const int N = 5;
LL temp[N][N];
LL res[N][N];
const int mod = 1000000009;
void Mul(LL a[][N],LL b[][N])
{
memset(temp,0,sizeof(temp));
for(int i = 0; i < 2; i++) ///i行
for(int j = 0; j < 2; j++) ///j列
for(int k = 0; k < 2; k++)
temp[i][j] = (temp[i][j]+(a[i][k]*b[k][j])%mod)%mod;
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
a[i][j] = temp[i][j];
}
void Solve(LL a[][N],LL n) ///n是求的幂次
{
memset(res,0,sizeof(res));
for(int i = 0; i < 2; i++)
res[i][i] = 1;
while(n)
{
if(n&1)
Mul(res,a);
Mul(a,a);
n>>=1;
}
}
int main()
{
LL T;
while(~scanf("%lld",&T))
{
LL a[N][N];
a[0][0] = 1;
a[0][1] = 1;
a[1][0] = 1;
a[1][1] = 0;
if(T == 0 || T == 1) printf("%lld\n",T);
else
{
Solve(a,T);
printf("%lld\n",res[0][1]);
}
}
return 0;
}