poj 3233 Matrix Power Series

这是一道经典的矩阵题:

这个题就是要构造一个矩阵;

A 1   *  A  1  ---> A*A   A + 1  .....  A^k   S(k-1) + 1;

0 1       0  1           0        1              0          1;

View Code
#include<iostream>

#include<cstdio>

#include<cstdlib>

#include<algorithm>

#include<cmath>

#include<queue>

#include<set>

#include<cstring>

#include<vector>

#include<string>

#define LL long long

using namespace std;

int n,m;

void multi( int B[][64], int A[][64] )

{

    int G[64][64]= {0};

    for( int i = 0 ; i < n ; i ++)

       for( int j = 0 ; j < n ; j ++ )

            for( int k = 0 ; k < n ; k ++ )

                 G[i][j] += B[i][k] * A[k][j];        

    for( int i = 0 ; i < n ; i ++ )

       for( int j = 0 ; j < n ; j ++ )

            B[i][j] = G[i][j]%m;    

}

int main( )

{

    int k,A[64][64],B[64][64];

    while( scanf( "%d %d %d",&n,&k,&m )==3 )

    {

          memset( A, 0 ,sizeof( A ) );

          memset( B, 0, sizeof( B ) );

          for( int i = 0; i < n ; i ++ )

          {

              for( int j = 0 ; j < n ; j ++ )

              {

                   scanf( "%d",&A[i][j] );

                   A[i][j] %= m;    

              }

              A[i][i+n] = 1;

              A[i+n][i+n] = 1;

              B[i][i] = 1;

              B[i+n][i+n] =1;

             

          }

          n <<= 1;

          for( int i = 0 ; (1<<i)<=k+1 ; i ++ )

          {

               if( (1<<i)&(k+1) ) multi( B  ,A );

               multi( A , A );        

          }

          int t = n;

          n>>=1;

          for( int i = 0 ;i < n  ; i++ )

          {

               B[i][i+n] = (B[i][i+n] - 1 + m)%m;

               for( int j = n ; j < t ; j ++ )

               printf( j==(t-1)?"%d\n":"%d ",B[i][j]);        

          }

    }

    return 0;    

}

 

你可能感兴趣的:(Matrix)