题解: 矩阵快速幂。 转载一篇文章:关于快速幂和矩阵快速幂
code:
/*
adrui's submission
Language : C++
Result : Accepted
Love : ll
favorite : Dragon Balls
Standing in the Hall of Fame
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define debug 0
#define LL long long
#define mod 10000
#define M(a, b) memset(a, b, sizeof(a))
struct Matrix {
LL a[2][2];
void init1() {
a[0][0] = a[0][1] = a[1][0] = 1;
a[1][1] = 0;
}
void init2() {
a[0][0] = a[1][1] = 1;
a[0][1] = a[1][0] = 0;
}
};
Matrix operator * (Matrix x, Matrix y) { //矩阵乘法
Matrix c;
M(c.a, 0);
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
c.a[i][j] += (x.a[i][k] * y.a[k][j]) % mod;
}
c.a[i][j] %= mod; //取模
}
return c;
}
Matrix Matrix_fast_mod(Matrix tmp, int b) {
Matrix c;
c.init1(); //幂底数矩阵
while (b) {
if (b & 1) tmp = tmp * c; //快速幂
c = c * c;
b >>= 1;
}
return tmp;
}
LL n;
int main() {
#if debug
freopen("in.txt", "r", stdin);
#endif //debug
while (~scanf("%I64d", &n), n != -1) {
Matrix ans;
ans.init2(); //初始化一个单位矩阵
ans = Matrix_fast_mod(ans, n); //矩阵快速幂
printf("%I64d\n", ans.a[0][1]); //根据矩阵输出Fn
}
return 0;
}