hdu5318(2015多校3)--The Goddess Of The Moon(dp+矩陣最佳化),hdu5318goddess

來源:互聯網
上載者:User

hdu5318(2015多校3)--The Goddess Of The Moon(dp+矩陣最佳化),hdu5318goddess

題目連結:點擊開啟連結

題目大意:給出n種串,每種串有無限多個,現在要在這n種串中選擇m個連結起來,連結的規則是:如果a串的尾碼(len >= 2 )是b串的首碼,那麼就可以把b接到a的後面,問最終可以組成多少個不同的串

首先應該排除重複的,因為重複的不會多產生連結。然後找出對於第i種串,後面可以接哪幾個串。

然後dp[i][j],當連結了i個串後,以第j個串結尾的有多少種。這樣dp[i][j] = ∑dp[i-1][k] (k串後面可以接j)

因為m<=1e9,所以用矩陣最佳化,初始時dp[1][i]全部為1,計算矩陣temp.a[i][j],如果第i個串後面可以接j,那麼temp.a[i][j] = 1,否則為0,這樣dp[i]和temp矩陣相乘都會得到dp[i+1],使用矩陣快速冪計算出dp[1]*(temp.a)^(m-1)的和

#include <cstdio>#include <cstring>#include <stack>#include <algorithm>using namespace std ; #pragma comment(linker, "/STACK:1024000000,1024000000")#define LL __int64const int MOD = 1e9+7 ;struct node{    LL a[60][60] ;};char str[60][60] ;LL a[60] , len[60] ;stack<int> sta ;int n ;node mul(node p,node q) {    int i , j , k ;    node temp ;    for(i = 0 ; i < n ; i++) {        for(j = 0 ; j < n ; j++){            temp.a[i][j] = 0 ;            for(k = 0 ; k < n ; k++) {                temp.a[i][j] = (temp.a[i][j] + p.a[i][k]*q.a[k][j]) % MOD ;            }        }    }    return temp ;}node pow(node p,int k) {    node temp ;    int i , j ;    memset(temp.a,0,sizeof(temp.a)) ;    for(i = 0 ; i < n ; i++) temp.a[i][i] = 1 ;    while( k ) {        if( k%2 ) temp = mul(temp,p) ;        p = mul(p,p) ;        k /=2 ;    }    return temp ;}int main() {    int t , m ;    int i , j , k , l ;    node temp ;    LL ans ;    scanf("%d", &t) ;    while( t-- ) {        scanf("%d %d", &n, &m) ;        for(i = 0 ; i < n ; i++)            scanf("%I64d", &a[i]) ;        sort(a,a+n) ;        n = unique(a,a+n)-a;        while( !sta.empty() ) sta.pop() ;        for(i = 0 ; i < n ; i++) {            do{                sta.push(a[i]%10) ;                a[i] /= 10 ;            }while( a[i] ) ;            len[i] = 0 ;            while( !sta.empty() ) {                str[i][ len[i]++ ] = sta.top()+'0' ;                sta.pop() ;            }            str[i][ len[i] ] == '\0' ;        }        memset(temp.a,0,sizeof(temp.a)) ;        for(i = 0 ; i < n ; i++) {            for(j = 0 ; j < n ; j++) {                for(l = 2 ; l <= len[i] && l <= len[j] ; l++) {                    for(k = 0 ; k < l ; k++)                        if( str[i][ len[i]-l+k ] != str[j][k] ) break ;                    if( k >= l ){                        temp.a[i][j] = 1 ;                        break ;                    }                }            }        }        temp = pow(temp,m-1) ;        for(i = 0 , ans = 0 ; i < n ; i++){            for(j = 0 ; j < n ; j++) {                ans = (ans + temp.a[i][j]) % MOD ;            }        }        printf("%I64d\n", ans) ;    }}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.