Http://poj.org/problem? Id = 3070
According to the constructed matrix, F [N] is the number in the upper right corner of the N power of the matrix.
#include <stdio.h>#include <iostream>#include <map>#include <set>#include <list>#include <stack>#include <vector>#include <math.h>#include <string.h>#include <queue>#include <string>#include <stdlib.h>#include <algorithm>#define LL long long#define _LL __int64#define eps 1e-12#define PI acos(-1.0)#define C 240#define S 20using namespace std;const int maxn = 110;struct matrix{int mat[3][3];void init(){memset(mat,0,sizeof(mat));mat[1][1] = mat[2][2] = 1;}}a;matrix mul(matrix a, matrix b){matrix res;memset(res.mat,0,sizeof(res.mat));for(int i = 1; i <= 2; i++){for(int k = 1; k <= 2; k++){if(a.mat[i][k] == 0) continue;for(int j = 1; j <= 2; j++){int t = a.mat[i][k]*b.mat[k][j];res.mat[i][j] = (res.mat[i][j] + t)%10000;}}}return res;}matrix pow(matrix a, int n){matrix res;res.init();while(n){if(n&1)res = mul(res,a);a = mul(a,a);n >>= 1;}return res;}int main(){int n;while(~scanf("%d",&n)){if(n == -1) break;a.mat[1][1] = a.mat[1][2] = a.mat[2][1] = 1;a.mat[2][2] = 0;matrix ans = pow(a,n);printf("%d\n",ans.mat[1][2]);}return 0;}