BZOJ-1875: [SDOI2009]HH去散步(动态规划+矩阵快速幂)

#include <cstdio>
#include <algorithm>
#include <cstring>
 
using namespace std ;
 
#define rep( i , x ) for ( int i = 0 ; i ++ < x ; )
#define MAXN 210
#define mod 45989
 
struct mat {
     
    int a[ MAXN ][ MAXN ] , n , m ;
     
    mat(  ) {
        memset( a , 0 , sizeof( a ) ) ;
    }
     
    void I( int _n ) {
        n = m = _n ;
        rep( i , n ) a[ i ][ i ] = 1 ;
    }
 
} e , a ;
 
mat operator * ( const mat &x , const mat &y ) {
    mat ret ;
    ret.n = x.n , ret.m = y.m ;
    rep( i , ret.n ) rep( j , ret.m ) {
        rep( k , x.m ) {
            ( ret.a[ i ][ j ] += x.a[ i ][ k ] * y.a[ k ][ j ] ) %= mod ;
        }
    }
    return ret ;
}
 
mat power( mat x , int y ) {
    mat ret ;
    ret.I( x.n ) ;
    for ( ; y ; y >>= 1 ) {
        if ( y & 1 ) ret = ret * x ;
        x = x * x ;
    }
    return ret ;
}
 
int E[ MAXN ][ 3 ] , cnt = 0 , n , m , T , A , B ;
 
int main(  ) {
    scanf( "%d%d%d%d%d" , &n , &m , &T , &A , &B ) ;
    rep( i , m ) {
        int s , t ; scanf( "%d%d" , &s , &t ) ;
        E[ ++ cnt ][ 0 ] = s ; E[ cnt ][ 1 ] = t , E[ cnt ][ 2 ] = i ;
        E[ ++ cnt ][ 0 ] = t ; E[ cnt ][ 1 ] = s , E[ cnt ][ 2 ] = i ;
    }
    e.n = e.m = cnt + 1 ;
    rep( i , cnt ) rep( j , cnt ) if ( E[ i ][ 0 ] == E[ j ][ 1 ] && E[ i ][ 2 ] != E[ j ][ 2 ] ) {
        e.a[ i ][ j ] = 1 ;
    }
    a.n = cnt + 1 , a.m = 1 , a.a[ cnt + 1 ][ 1 ] = 1 ;
    rep( i , cnt ) if ( E[ i ][ 0 ] == A ) e.a[ i ][ cnt + 1 ] = 1 ;
    mat w = power( e , T ) * a ;
    int ans = 0 ;
    rep( i , cnt ) if ( E[ i ][ 1 ] == B ) ( ans += w.a[ i ][ 1 ] ) %= mod ;
    printf( "%d\n" , ans ) ;
    return 0 ;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值