Ultraviolet A 11149-power of matrix (Rapid power of matrix)

Source: Internet
Author: User

 

Ultraviolet A 11149-power of matrix (Rapid power of matrix)

 

 

 

 

#include <cstdio>#include <cstring>#define CLR( a, b ) memset( a, b, sizeof(a) )#define MOD 10#define MAX_SIZE 40struct Mat{    int r, c;    int mat[MAX_SIZE][MAX_SIZE];        Mat( int _r = 0 , int _c = 0 )    {        CLR( mat, 0 );        r = _r, c = _c;    }    void init()    {        for( int i = 0; i < r; ++i )            for( int j = 0; j < c; ++j )                mat[i][j] = ( i == j );    }    Mat operator + ( const Mat &b ) const    {        Mat t( r, c );        for( int i = 0; i < r; ++i )            for( int j = 0; j < c; ++j )                t.mat[i][j] = ( mat[i][j] + b.mat[i][j] ) % MOD;        return t;    }    Mat operator * ( const Mat &b ) const    {        Mat t( r, b.c );        for( int k = 0; k < c; ++k )            for( int i = 0; i < r; ++i ) if( mat[i][k] )                for( int j = 0; j < b.c; ++j )                    t.mat[i][j] = ( t.mat[i][j] + mat[i][k] * b.mat[k][j] ) % MOD;        return t;                    }    void debug()    {        for( int i = 0; i < r; ++i )        {            for( int j = 0; j < c; ++j )            {                if( j !=0 )    putchar( ‘ ‘ );                printf( "%d", mat[i][j] );            }            putchar( ‘\n‘ );        }    }};Mat fast_mod( Mat a, int b ){    Mat c( a.r, a.c );    c.init();    while( b )    {        if( b & 1 ) c = c * a;        a = a * a;        b >>= 1;    }    return c;}Mat solve( Mat a, int b ){    if( b == 1 )        return a;    Mat in( a.r, a.c );    in.init();    if( b == 0 )        return in;    if( b & 1 )        return solve( a, b/2 ) * ( in + fast_mod( a, b/2 ) ) + fast_mod( a, b );    else        return solve( a, b/2 ) * ( in + fast_mod( a, b/2 ) );}void Orz(){    int n, k, x;    int cas = 0;    while( ~scanf( "%d %d", &n, &k ) && n )    {        Mat t( n, n );        for( int i = 0; i < n; ++i )        {            for( int j = 0; j < n; ++j )            {                scanf( "%d", &x );                t.mat[i][j] = x % MOD;            }        }        t = solve( t, k );        t.debug();        putchar( ‘\n‘ );    }}int main(){    Orz();    return 0;}
Code Jun

 

Ultraviolet A 11149-power of matrix (Rapid power of matrix)

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.