#defineMatr 10//size of the matrix, attention can be small, smallstructMat//Matrix struct, a for content, size matrix starting from 1{ll a[matr][matr],size; Mat () {size=0; Memset (A,0,sizeof(a)); } }; voidPrint (Mat m)//output matrix information, debug with{ inti,j; printf ("%d\n", m.size); for(i=0; i<m.size;i++) { for(j=0; j<m.size;j++) printf ("%d", M.a[i][j]); printf ("\ n"); }} Mat multi (Mat m1,mat m2,intMoD//Two multiplication of equal matrices, 0 optimization without arithmetic for sparse matrices{mat ans=mat (); Ans.size=m1.size; for(intI=1; i<=m1.size;i++) for(intj=1; j<=m2.size;j++) if(M1.a[i][j])//Sparse matrix Optimization for(intk=1; k<=m1.size;k++) Ans.a[i][k]= (Ans.a[i][k]+m1.a[i][j]*m2.a[j][k])%MoD; returnans; } Mat Quickmulti (Mat m,intNintMoD//Two-point fast power{mat ans=Mat (); inti; for(i=1; i<=m.size;i++) ans.a[i][i]=1; Ans.size=m.size; while(n) {if(n&1) ans=multi (M,ANS,MOD); M=multi (M,M,MOD); N>>=1; } returnans; }
Matrix Quick Power Template