HDU 4965 fast matrix calculation (matrix high speed Power)

Source: Internet
Author: User

HDU 4965 Fast Matrix calculation

Topic links

Matrix multiplied by axbxaxb ... Multiply nn times. Ability to turn ax (Bxaxbxa ...) XB, which is multiplied by nn-1 times, so that the middle matrix is a mere 6x6. Can be done with a matrix of high-speed power

Code:

#include <cstdio> #include <cstring>const int n = 1005;const int M = 10;int N, m;int a[n][m], b[m][n], c[m][m],    Cc[n][n];int ans[m][m];void tra () {memset (cc, 0, sizeof (CC));        for (int i = 0, i < m; i++) {for (int j = 0; J < m; J + +) {Cc[i][j] = 0;        for (int k = 0; k < m; k++) {Cc[i][j] = (Cc[i][j] + c[i][k] * c[k][j])% 6; }}} for (int i = 0; i < m; i++) for (int j = 0; J < m; j + +) C[i][j] = Cc[i][j];}        void Mul () {for (int i = 0, i < m; i++) {for (int j = 0; J < m; J + +) {Cc[i][j] = 0;        for (int k = 0; k < m; k++) {Cc[i][j] = (Cc[i][j] + ans[i][k] * c[k][j])% 6; }}} for (int i = 0; i < m; i++) for (int j = 0; J < m; j + +) Ans[i][j] = Cc[i][j];}    void Pow_mod (int k) {memset (ans, 0, sizeof (ans));    for (int i = 0; i < m; i++) ans[i][i] = 1;    while (k) {if (k&1) Mul ();    TRA ();    K >>= 1;  }}void Init () {  for (int i = 0; i < n; i++) for (int j = 0; J < m; j + +) scanf ("%d", &a[i][j]); for (int i = 0; i < m; i++) for (int j = 0; J < N; j + +) scanf ("%d", &b[i][j]);}        int solve () {for (int i = 0, i < m; i++) {for (int j = 0; J < m; J + +) {C[i][j] = 0;        for (int k = 0; k < n; k++) {C[i][j] = (C[i][j] + b[i][k] * a[k][j])% 6;    }}} pow_mod (n * n-1);    for (int i = 0, i < m; i++) {for (int j = 0; J < m; J + +) {C[i][j] = ans[i][j];        }} for (int i = 0, i < n; i++) {for (int j = 0; J < m; J + +) {Cc[i][j] = 0;        for (int k = 0; k < m; k++) {Cc[i][j] = (Cc[i][j] + a[i][k] * c[k][j])% 6;    }}} for (int i = 0, i < n; i++) for (int j = 0; J < m; j + +) A[i][j] = Cc[i][j];    int ans = 0;        for (int i = 0, i < n; i++) {for (int j = 0; J < N; j + +) {int sum = 0; for (int k = 0; k < m; k++){sum = (sum + a[i][k] * b[k][j])% 6;    } ans + = sum; }} return ans;    int main () {while (~scanf ("%d%d", &n, &m) && n | | m) {init ();    printf ("%d\n", Solve ()); } return 0;}


HDU 4965 fast matrix calculation (matrix high speed 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.