# Define matr 105 // matrix size struct mat // matrix structure. A indicates the content. The matrix of column C in the r row starts from 1 {int A [matr] [matr]; int R, c; MAT () {r = C = 0; memset (A, 0, sizeof (A) ;}}; void print (MAT m) {int I, J; // printf ("% d \ n", M. size); for (I = 0; I <m. r; I ++) {for (j = 0; j <m. c; j ++) printf ("% d", M. A [I] [J]); printf ("\ n") ;}} mat MUL (MAT M1, mat m2, int mod) {mat ans = MAT (); ans. R = m1.r; ans. C = m2.c; For (INT I = 1; I <= m1.r; I ++) for (Int J = 1; j <= m2.r; j ++) if (m1.a [I] [J]) for (int K = 1; k <= m2.c; k ++) ans. A [I] [k] = (ans. A [I] [k] + m1.a [I] [J] * m2.a [J] [k]) % MOD; return ans;} mat quickmul (MAT M, int N, int mod) {mat ans = MAT (); int I; for (I = 1; I <= m. r; I ++) ans. A [I] [I] = 1; ans. R = m. r; ans. C = m. c; while (n) {If (N & 1) ans = MUL (M, ANS, MoD); M = MUL (M, M, MoD ); n> = 1;} return ans;}/* ans ^ = N-> mat ans = MAT (); ans. R = r; ans. C = C; initialize the ANS matrix ans = quickmul (ANS, N, MoD );*/View code
Typical problems solved by Matrix Multiplication
Http://www.matrix67.com/blog/archives/276
Rapid matrix power