Matrix Fast Power: http://www.cnblogs.com/atmacmer/p/5184736.html
Topic links
#include <iostream>#include<cstdio>using namespaceStd;typedefLong Longll;#defineMOD 10000ll a[7],b[7],a0[7],b0[7];voidPow_mod (ll N) {a0[1]=a0[2]=a0[3]=1, a0[4]=0; b0[1]=b0[4]=1, b0[2]=b0[3]=0; while(n) {if(n%2==1) {b[1]= (a0[1]*b0[1]+a0[2]*b0[3])%MOD; b[2]= (a0[1]*b0[2]+a0[2]*b0[4])%MOD; b[3]= (a0[3]*b0[1]+a0[4]*b0[3])%MOD; b[4]= (a0[3]*b0[2]+a0[4]*b0[4])%MOD; for(intI=1; i<=4; i++) B0[i]=B[i]; } a[1]= (a0[1]*a0[1]+a0[2]*a0[3])%MOD; a[2]= (a0[1]*a0[2]+a0[2]*a0[4])%MOD; a[3]= (a0[3]*a0[1]+a0[4]*a0[3])%MOD; a[4]= (a0[3]*a0[2]+a0[4]*a0[4])%MOD; for(intI=1; i<=4; i++) A0[i]=A[i]; N/=2; }}intMain () {ll n; while(~SCANF ("%i64d", &n) &&n!=-1) { if(n==0) printf ("0\n"); Else{pow_mod (n); printf ("%i64d\n", b[2]); } } return 0;}
POJ 3070 Matrix Solving fib problem with fast power