Topic Portal
1 /*2 Matrix Fast Power: To find the Fibonacci number of the nth term, transpose the matrix are given, set a template can be. It's high efficiency.3 */4#include <cstdio>5#include <algorithm>6#include <cstring>7#include <cmath>8 using namespacestd;9 Ten Const intMAXN = 1e3 +Ten; One Const intINF =0x3f3f3f3f; A Const intMOD =10000; - structMat { - intm[2][2]; the }; - - Mat Multi_mod (Mat A, Mat b) { -MAT ret; memset (RET.M,0,sizeof(RET.M)); + for(intI=0; i<2; ++i) { - for(intj=0; j<2; ++j) { + for(intk=0; k<2; ++k) { ARET.M[I][J] = (Ret.m[i][j] + a.m[i][k] * b.m[k][j])%MOD; at } - } - } - returnret; - } - in intMatrix_pow_mod (Mat x,intN) { -MAT ret; ret.m[0][0] = ret.m[1][1] =1; ret.m[0][1] = ret.m[1][0] =0; to while(n) { + if(N &1) ret =Multi_mod (ret, x); -x =multi_mod (x, x); theN >>=1; * } $ returnret.m[0][1];Panax Notoginseng } - the intMainvoid) {//POJ 3070 Fibonacci + intN; A while(SCANF ("%d", &n) = =1) { the if(n = =-1) Break; + Mat x; -x.m[0][0] = x.m[0][1] = x.m[1][0] =1; x.m[1][1] =0; $printf ("%d\n", Matrix_pow_mod (x, N)); $ } - - return 0; the}
Matrix Fast Power POJ 3070 Fibonacci