zoukankan      html  css  js  c++  java
  • BZOJ1297: [SCOI2009]迷路 矩阵快速幂

    Description

    windy在有向图中迷路了。 该有向图有 N 个节点,windy从节点 0 出发,他必须恰好在 T 时刻到达节点 N-1。 现在给出该有向图,你能告诉windy总共有多少种不同的路径吗? 注意:windy不能在某个节点逗留,且通过某有向边的时间严格为给定的时间。

    Input

    第一行包含两个整数,N T。 接下来有 N 行,每行一个长度为 N 的字符串。 第i行第j列为'0'表示从节点i到节点j没有边。 为'1'到'9'表示从节点i到节点j需要耗费的时间。

    Output

    包含一个整数,可能的路径数,这个数可能很大,只需输出这个数除以2009的余数。

    Sample Input

    【输入样例一】
    2 2
    11
    00

    【输入样例二】
    5 30
    12045
    07105
    47805
    12024
    12345


    Sample Output

    【输出样例一】
    1

    【样例解释一】
    0->0->1

    【输出样例二】
    852

    HINT

    30%的数据,满足 2 <= N <= 5 ; 1 <= T <= 30 。 100%的数据,满足 2 <= N <= 10 ; 1 <= T <= 1000000000 。

    Solution

    矩阵快速幂

    一开始看到以为还是板子...不过发现这题是有边权的。后来在hjw大佬的点醒下发现可以拆点

    然后套板子就行了

    新姿势++

    #include <bits/stdc++.h>
    
    #define ll long long
    #define inf 0x3f3f3f3f 
    #define il inline 
    
    namespace io {
    
        #define in(a) a=read()
        #define out(a) write(a)
        #define outn(a) out(a),putchar('
    ')
    
        #define I_int int 
        inline I_int read() {
            I_int x = 0 , f = 1 ; char c = getchar() ;
            while( c < '0' || c > '9' ) { if( c == '-' ) f = -1 ; c = getchar() ; } 
            while( c >= '0' && c <= '9' ) { x = x * 10 + c - '0' ; c = getchar() ; } 
            return x * f ;
        } 
        char F[ 200 ] ;
        inline void write( I_int x ) {
            I_int tmp = x > 0 ? x : -x ;
            if( x < 0 ) putchar( '-' ) ;
            int cnt = 0 ;
            while( tmp > 0 ) {
                F[ cnt ++ ] = tmp % 10 + '0' ;
                tmp /= 10 ;
            }
            while( cnt > 0 ) putchar( F[ -- cnt ] ) ;
        }
        #undef I_int
    
    }
    using namespace io ;
    
    using namespace std ;
    
    #define N 500
    const int mod = 2009 ;
    
    int n = read() , T = read() ;
    struct matrix {
        int m[ N ][ N ] ;
        matrix() { memset( m , 0 , sizeof( m ) ) ; }
        int *operator[] ( int a ) { return m[ a ] ; } 
        matrix operator * ( matrix &x ) {
            matrix ans ;
            memset( ans.m , 0 , sizeof( ans.m ) ) ;
            for( int i = 1 ; i <= n ; i ++ ) {
                for( int j = 1 ; j <= n ; j ++ ) {
                    for( int k = 1 ; k <= n ; k ++ ) {
                        ans[ i ][ j ] = ( ans[ i ][ j ] + m[ i ][ k ] * x[ k ][ j ] % mod ) % mod ;
                    }
                }
            }
            return ans ;
        }
    } a ;
    
    matrix power( matrix a , int b ) {
        matrix ans , base = a ; 
        memset( ans.m , 0 , sizeof( ans.m ) ) ;
        for( int i = 1 ; i <= n ; i ++ ) 
            ans[ i ][ i ] = 1 ;
        while( b ) {
            if( b & 1 ) ans = ans * base ;
            base = base * base ;
            b >>= 1 ;
        }
        return ans ;
    }
    
    char ch[ 100 ] ;
    
    int main() {
        for( int i = 1 ; i <= n ; i ++ ) {
            scanf( "%s" , ch+1 ) ;
            for( int j = 1 ; j <= n ; j ++ ) {
                if( ch[ j ] > '0' ) {
                    a[ 9 * ( i - 1 ) + (ch[ j ] - '0' ) ][ 9 * ( j - 1 ) + 1 ] = 1 ;
                }
            }
        }
        for( int i = 1 ; i <= n ; i ++ ) {
            for( int j = 1 ; j < 9 ; j ++ ) {
                a[ 9 * ( i - 1 ) + j ][ 9 * ( i - 1 ) + j + 1 ] = 1 ;
            }
        }
        n *= 9 ; //puts("233");
        matrix ans = power( a , T ) ;
        printf( "%d
    " , ans[ 1 ][ n - 8 ] ) ;
    }
  • 相关阅读:
    乱谈数学--傅里叶变换(级数)的原理(一)
    Linux day03
    Linux day02
    Linux day01(二)虚拟机快照和克隆的用法介绍
    Linux day01(一) 创建Linux虚拟机,设置虚拟机默认属性,虚拟机和Xhell建立连接
    Django day 38 结算中心,支付中心,计算价格方法
    Django day 37 网站视频的播放,购物车接口,优惠券表分析
    Django day 36 支付宝支付,微信推送
    Django day35 redis连接池,redis-list操作,django中使用redis,支付宝支付
    Django day 34 过滤课程,登录,redis,python操作redis
  • 原文地址:https://www.cnblogs.com/henry-1202/p/BZOJ1297.html
Copyright © 2011-2022 走看看