标签:
题目大意:fibonacci的升级版,规则是f(n) = f(n-1) + f(n-2) + f(n-3)
解题思路:水题
#include<cstdio>
typedef long long ll;
const int N = 3;
const ll mod = 1e9 + 9;
struct Matrix{
ll mat[N][N];
}A, B, tmp;
ll n;
void init(){
for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++) {
A.mat[i][j] = B.mat[i][j] = 0;
if(i == j)
B.mat[i][j] = 1;
}
A.mat[0][0] = A.mat[1][0] = A.mat[2][0] = A.mat[0][1] = A.mat[1][2] = 1;
}
Matrix matMul(Matrix &x, Matrix &y) {
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] = (tmp.mat[i][j] + (x.mat[i][k] * y.mat[k][j]) % mod ) % mod;
}
return tmp;
}
void solve() {
while(n) {
if(n & 1)
B = matMul(B,A);
A = matMul(A,A);
n >>= 1;
}
}
int main() {
while(scanf("%lld", &n) != EOF && n) {
if(n == 3 || n == 1 || n == 2) {
printf("%lld\n", n - 1);
continue;
}
n -= 3;
init();
solve();
ll ans = 0;
ans = (ans + 2 * B.mat[0][0]) % mod;
ans = (ans + B.mat[1][0]) % mod;
printf("%lld\n", ans);
}
return 0;
}
标签:
原文地址:http://blog.csdn.net/l123012013048/article/details/46316573