思路: 矩阵快速幂
分析:
1 裸题
代码:
/************************************************
* By: chenguolin *
* Date: 2013-08-30 *
* Address: http://blog.csdn.net/chenguolinblog *
************************************************/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long int64;
const int MOD = 1e9+9;
const int N = 3;
int64 n;
struct Matrix{
int64 mat[N][N];
Matrix operator*(const Matrix& m)const{
Matrix tmp;
for(int i = 0 ; i < N ; i++){
for(int j = 0 ; j < N ; j++){
tmp.mat[i][j] = 0;
for(int k = 0 ; k < N ; k++)
tmp.mat[i][j] += mat[i][k]*m.mat[k][j]%MOD;
tmp.mat[i][j] %= MOD;
}
}
return tmp;
}
};
int Pow(Matrix m){
if(n <= 3) return n-1;
Matrix ans;
memset(ans.mat , 0 , sizeof(ans.mat));
for(int i = 0 ; i < N ; i++)
ans.mat[i][i] = 1;
n -= 3;
while(n){
if(n%2)
ans = ans*m;
n /= 2;
m = m*m;
}
int64 sum = 0;
sum += ans.mat[0][0]*2%MOD;
sum += ans.mat[0][1]*1%MOD;
return sum%MOD;
}
int main(){
Matrix m;
memset(m.mat , 0 , sizeof(m.mat));
for(int i = 0 ; i < N ; i++)
m.mat[0][i] = 1;
m.mat[1][0] = 1 ; m.mat[2][1] = 1;
while(scanf("%lld" , &n) && n)
printf("%d\n" , Pow(m));
return 0;
}