POJ 3233 rapid Matrix Multiplication

Source: Internet
Author: User

I think my professional courses are falling behind, and acm is not doing well in this aspect = It hurts me !! Write a blog and start learning !!

The fast power of the matrix is similar to a ^ B % n. Use the binary idea. In this question, we need to calculate the sum of A + A ^ 2 + A ^ 3 +... + A ^ k. Due to the combination law of matrix multiplication! @ # $ % ^ &*.....

The code is ugly. Take a place... the second write recursion !! My God ~~

#include<iostream>#define MAXN 31using namespace std;struct Matrix{       unsigned long long matrix[MAXN][MAXN];};int n,m;void MatrixPow( int k,Matrix &pre ){     int i,j,p;     if( k<=1 )return ;     else if( k==2 )     {          Matrix now1=pre;         for( i=1;i<=n;i++ )         for( j=1;j<=n;j++ )         {              pre.matrix[i][j]=0;              for( p=1;p<=n;p++ )                   pre.matrix[i][j]+=now1.matrix[i][p]*now1.matrix[p][j];              pre.matrix[i][j]%=m;         }     }     else if( k%2==0 )     {          Matrix now1=pre;          MatrixPow( k>>1,now1 );          MatrixPow( 2,now1 );          pre=now1;     }     else     {         Matrix now1=pre;         Matrix now2=pre;         MatrixPow( k>>1,now1 );         MatrixPow( 2,now1 );         for( i=1;i<=n;i++ )         for( j=1;j<=n;j++ )         {              pre.matrix[i][j]=0;              for( p=1;p<=n;p++ )                   pre.matrix[i][j]+=now1.matrix[i][p]*now2.matrix[p][j];              pre.matrix[i][j]%=m;         }     }}void addMatrix( int k,Matrix &pre ){     int i,j,p;     if( k<=1 )     return ;     else if( k==2 )     {          Matrix now1=pre;          Matrix now2=pre;          MatrixPow( 2,now2 );          for( i=1;i<=n;i++ )          for( j=1;j<=n;j++ )          {               pre.matrix[i][j]=now1.matrix[i][j]+now2.matrix[i][j];               pre.matrix[i][j]%=m;          }     }     else if( k%2==0 )     {          Matrix now1=pre;          Matrix now2=pre;          addMatrix( k>>1,now1 );          MatrixPow( k>>1,now2 );                    for( i=1;i<=n;i++ )          for( j=1;j<=n;j++ )          {               pre.matrix[i][j]=0;               for( p=1;p<=n;p++ )                    pre.matrix[i][j]+=now1.matrix[i][p]*now2.matrix[p][j];               pre.matrix[i][j]%=m;          }                    for( i=1;i<=n;i++ )          for( j=1;j<=n;j++ )          {               pre.matrix[i][j]+=now1.matrix[i][j];               pre.matrix[i][j]%=m;          }     }     else if( k%2==1 )     {         Matrix now1=pre;         Matrix now2=pre;         addMatrix( k-1,now1 );         MatrixPow( k,now2 );         for( i=1;i<=n;i++ )         for( j=1;j<=n;j++ )         {              pre.matrix[i][j]=now1.matrix[i][j]+now2.matrix[i][j];              pre.matrix[i][j]%=m;         }     }}int main(){    int k;    while( scanf( "%d %d %d",&n,&k,&m )!=EOF )    {           int i,j;           Matrix ans;           for( i=1;i<=n;i++ )           for( j=1;j<=n;j++ )           {                scanf( "%llu",&ans.matrix[i][j] );           }           addMatrix( k,ans );                      for( i=1;i<=n;i++ )           {                for( j=1;j<n;j++ )                     printf( "%llu ",ans.matrix[i][j]%m );                printf( "%llu\n",ans.matrix[i][n]%m );                }    }     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.