.
Matrix high-speed power idea and fast power same
#include <iostream>#include<cstdio>#include<cstring>#defineMOD 10000using namespacestd;structMatrix {intmat[2][2]; Matrix () {//constructor with the same name as the struct if the writing init () function is to be calledMemset (Mat,0,sizeof(MAT)); }};matrix mul (Matrix A, Matrix B) {matrix C; for(intI=0; i<=1; i++) { for(intj=0; j<=1; j + +) { for(intk=0; k<=1; k++) {C.mat[i][j]= (C.mat[i][j]+a.mat[i][k]*b.mat[k][j])%MOD; } } } returnC;} Matrix Powmul (Matrix A,intN) {matrix B; //Initialize to the unit arrayb.mat[0][0]=1; b.mat[1][1]=1; while(n>=1) { if(n&1) {B=Mul (b,a); } A=mul (A,a);//Self PowerN/=2; } returnB;}intMain () {intN; while(~SCANF ("%d",&N)) {if(n==-1) Break; Matrix A; a.mat[0][0]=a.mat[0][1]=a.mat[1][0]=1; a.mat[1][1]=0; A=Powmul (a,n); cout<<a.mat[0][1]<<Endl; } return 0; }
POJ 3070 Matrix Mob