Hdu_4965 Fast Matrix Calculation 2014 multi-School 9 matrix Rapid power + witty matrix combination Law

Source: Internet
Author: User

At first, I thought this question was a raw question about the rapid power of the matrix,

Later it was found that it would time out, and the super would exceed M = C ^ (N * n). This operation, and the C itself is a matrix of N * n, with N at the maximum of 1000.

But here is a clever place: the source of C is actually = a * B. A is a matrix of N * k, and B is a matrix of K * n, K is up to 10. Here is the breakthrough. The combination Law of the matrix should be used.

That is to say, I will not combine a * B first. I will first combine B * A. Isn't m required to be C ^ N * n, calculate the N x n (B x A) values, multiply the value of 10x10 by the value of logn x n. Then multiply A and B at the two ends.

I did not expect the combination of law.

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define LL __int64using namespace std;LL matA[1010][10],matB[10][1010];LL matC[1010][10];LL MM[1010][1010];int n,k;struct Mat{    LL mat[10][10];}E;Mat operator *(Mat a,Mat b){    Mat c;    for (int i=0;i<k;i++)    for (int j=0;j<k;j++){        c.mat[i][j]=0;        for (int w=0;w<k;w++){            c.mat[i][j]+=a.mat[i][w]*b.mat[w][j];            c.mat[i][j]%=6;        }    }    return c;}Mat operator ^(Mat a,int x){    Mat c=E;    for (int i=x;i;i>>=1){        if (i&1){            c=c*a;        }        a=a*a;    }    return c;}int main(){    memset(E.mat,0,sizeof E.mat);    for (int i=0;i<10;i++) E.mat[i][i]=1;    while (scanf("%d%d",&n,&k)!=EOF)    {        if (n==0 && k==0) break;        for (int i=0;i<n;i++)            for (int j=0;j<k;j++) scanf("%I64d",&matA[i][j]);        for (int i=0;i<k;i++)            for (int j=0;j<n;j++) scanf("%I64d",&matB[i][j]);        Mat a;        for (int i=0;i<k;i++)            for (int j=0;j<k;j++){               a.mat[i][j]=0;               for (int w=0;w<n;w++){                 a.mat[i][j]+=matB[i][w]*matA[w][j];               }               //cout<<i<<" aaa "<<j<<" "<<a.mat[i][j]<<endl;            }        Mat M=a^(n*n-1);        for (int i=0;i<n;i++)        for (int j=0;j<k;j++){            matC[i][j]=0;            for (int w=0;w<k;w++)                 matC[i][j]+=matA[i][w]*M.mat[w][j];           // cout<<matC[i][j]<<" Matc "<<endl;        }        int ans=0;        for (int i=0;i<n;i++)        for (int j=0;j<n;j++){            MM[i][j]=0;            for (int w=0;w<k;w++){                MM[i][j]+=matC[i][w]*matB[w][j];            }           // cout<<MM[i][j]<<" qwe "<<endl;            ans+=MM[i][j]%6;        }        printf("%d\n",ans);    }    return 0;}

  

Hdu_4965 Fast Matrix Calculation 2014 multi-School 9 matrix Rapid power + witty matrix combination Law

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.