HDU 4965 Fast matrix calculation (using matrix operation properties)

Source: Internet
Author: User
Tags ini mul

Test instructions: Give the n*k matrix A and K*n B, for (AB) ^ (n*n) result matrix of the sum of the elements of Module 6. (n<=1000,k<=6)

Idea: The matrix of a*b is the matrix of N*n (1000*1000), and then the fast power is sure to time out, using multiplication to bind the law a^ (n*n) * b^ (n*n) = A*b*a*b*a*b*a = A * (b*a) * (b*a), The matrix of b*a 6*6 can be quickly power

62MS 1716K 1968 B C + + #include <cstdio> #include <iostream> #include <cstring> #include <algorithm&
Gt


using namespace Std;
    struct Mat {int a[10][10];
    int r,c;
        Mat () {memset (a,0,sizeof (a));
    r=c=0;


} void Mem () {memset (a,0,sizeof (a));}};
int n,l;
Mat I;


int a[1010][10],b[10][1010];
    Mat Mul (Mat m1,mat m2) {mat ans; for (int i=1;i<=l;i++) for (int. j=1;j<=l;j++) if (M1.a[i][j]) for (int k=1;k<=l;k
    + +) ans.a[i][k]= (ans.a[i][k]+m1.a[i][j]*m2.a[j][k])%6;
return ans;
    } Mat Quickmul (Mat m,int k) {mat ans;
    for (int i=1;i<=l;i++) ans.a[i][i]=1;
        while (k) {if (k&1) Ans=mul (ans,m);
        M=mul (M,M);
    k>>=1;
} return ans;

} int ans[1010][10];
    void Ini () {memset (ans,0,sizeof (Ans));
I.mem (); } int main () {while (scanf ("%d%d", &n,&l), (n| |
        L)) {ini (); for (int i=1;i<=n; i++) for (int j=1;j<=l;j++) scanf ("%d", &a[i][j]);

        for (int i=1;i<=l;i++) for (int j=1;j<=n;j++) scanf ("%d", &b[i][j]); for (int i=1;i<=l;i++)//Calculate b*a matrix for (int j=1;j<=n;j++) if (b[i][j]) fo

        R (int k=1;k<=l;k++) i.a[i][k]= (i.a[i][k]+b[i][j]*a[j][k])%6; 
        
        Mat Tmp=quickmul (i,n*n-1);
                    for (int. i=1;i<=n;i++)//a* (b*a) ^ (n-1) for (int j=1;j<=l;j++) if (A[i][j])
        for (int k=1;k<=l;k++) ans[i][k]= (ans[i][k]+a[i][j]*tmp.a[j][k])%6;
        int ans=0; for (int. i=1;i<=n;i++)//a* (b*a) ^ (n-1) ^b= (a*b) ^n for (int j=1;j<=n;j++) {int
                Tmp=0;
                for (int k=1;k<=l;k++) {tmp+=ans[i][k]*b[k][j];
   } ans+= (tmp%6);         } printf ("%d\n", ans);
} return 0; }


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.