码迷,mamicode.com
首页 > 其他好文 > 详细

Uva 11551 - Experienced Endeavour ( 矩阵快速幂 )

时间:2014-10-31 13:44:24      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   os   ar   for   sp   

 

Uva 11551 - Experienced Endeavour ( 矩阵快速幂 )

 

bubuko.com,布布扣

 

 

 

bubuko.com,布布扣
#include <cstdio>    
#include <cstring>
#include <algorithm>
using namespace std;
#define MAX_SIZE 50
#define MOD 1000
#define CLR( a, b ) memset( a, b, sizeof(a) )
struct Mat
{
    int n;
    int mat[MAX_SIZE][MAX_SIZE];
    Mat( int _n )
    {
        n = _n;
        CLR( mat, 0 );
    }
    void init()
    {
        for( int i = 0; i < n; ++i )
            for( int j = 0; j < n; ++j )
                mat[i][j] = ( i == j );
    }
    Mat operator * ( const Mat &b ) const
    {
        Mat c( b.n );
        for( int k = 0; k < n; ++k )
            for( int i = 0; i < n; ++i )    if( mat[i][k] )
                for( int j = 0; j < n; ++j )
                    c.mat[i][j] = ( c.mat[i][j] + mat[i][k] * b.mat[k][j] ) % MOD;
        return c;
    }
};

Mat fast_mod( Mat a, int b )
{
    Mat res( a.n );
    res.init();
    while( b )
    {
        if( b & 1 )    res = res * a;
        a = a * a;
        b >>= 1; 
    }
    return res;
}

void scan( int &x )
{
    char c;
    while( c = getchar(), c < 0 || c > 9 );
    x = c - 0;
    while( c = getchar(), c >= 0 && c <= 9 ) x = x *10 + c - 0;
}

void Orz()
{
    int n, k, t, x, y;
    scanf( "%d", &t );
    while( t-- )
    {
        scan( n ), scan( k );
        Mat c( n );
        for( int i = 0; i < n; ++i )
            scan( c.mat[i][0] );
        Mat d( n );
        for( int i = 0; i < n; ++i )
        {
            scan( x );
            for( int j = 0; j < x; ++j )
            {
                scan( y );
                d.mat[i][y] = 1;
            }
        }
        Mat res = fast_mod( d, k );
        res = res * c;
        for( int i = 0 ; i < n; ++i )
        {
            if( i != 0 ) putchar(   );
            printf( "%d",res.mat[i][0] );
        }
        putchar( \n );
    }    
}

int main()
{
    Orz();
    return 0;    
}
代码君

 

Uva 11551 - Experienced Endeavour ( 矩阵快速幂 )

标签:style   blog   http   io   color   os   ar   for   sp   

原文地址:http://www.cnblogs.com/BigBallon/p/4064802.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!