POJ 3070
思路:矩阵快速幂,没什么可说的。
#include<cstdio> #include<string> #include<cstring> #include<iostream> #include<algorithm> using namespace std; typedef struct Matrix{ int m[2][2]; Matrix(){ memset(m, 0, sizeof(m)); } }Matrix; Matrix mtMul(Matrix A, Matrix B){ Matrix tmp; for(int i = 0;i < 2;i ++) for(int j = 0;j < 2;j ++) for(int k = 0;k < 2;k ++){ int t = (A.m[i][k] * B.m[k][j])%10000; tmp.m[i][j] = (tmp.m[i][j] + t)%10000; } return tmp; } Matrix mtPow(Matrix A, int k){ if(k == 1) return A; Matrix tmp = mtPow(A, k >> 1); Matrix res = mtMul(tmp, tmp); if(k & 1) res = mtMul(res, A); return res; } int main(){ int n; while(~scanf("%d", &n) && (n+1)){ if(n == 0) printf("0\n"); else{ Matrix M; M.m[0][0] = M.m[0][1] = M.m[1][0] = 1; M.m[1][1] = 0; Matrix tmp = mtPow(M, n); printf("%d\n", tmp.m[1][0]); } } return 0; }