Rapid matrix power

Source: Internet
Author: User
# 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.