Simple and Fast Matrix power:
FN 1 1 Fn-1
= *
1 Fn-1 0 Fn-2
You can use the preceding matrix as a fast power.
The Code is as follows:
# Include <cstdlib> # include <cstdio> # include <cstring> # include <algorithm> # define mod 10000 using namespace STD; struct matrix // ['meitriks] {int A [2] [2]; void new (int x, int y, int Z, int W) {A [0] [0] = X, a [0] [1] = y; A [1] [0] = Z, A [1] [1] = W;} Matrix Operator * (matrix P) {int sum = 0; matrix ret; For (INT I = 0; I <2; ++ I) {for (int K = 0; k <2; ++ K) {sum = 0; For (Int J = 0; j <2; ++ J) {Sum + = A [I] [J] * P. A [J] [k];} ret. A [I] [k] = sum % MOD;} return ret;} matrix _ POW (matrix P, int B) {matrix ret; ret. new (1, 0, 0, 1); while (B) {If (B & 1) {ret = RET * P;} p = p * P; B >>= 1 ;}return RET ;}void print () {for (INT I = 0; I <2; ++ I) {for (Int J = 0; j <2; ++ J) {printf ("% d", a [I] [J]);} puts ("") ;}} M, P, ans; int main () {int N; p. new (1, 1, 1, 0); M. new (1, 0, 0, 0 );// Save F0 and F1 while (scanf ("% d", & N), n! =-1) {ans = ans. _ POW (p, n); ans = ans * m; printf ("% d \ n", ans. A [1] [0]);} return 0 ;}