HDU 5318 The Goddess Of The Moon(构造矩阵 )——2015 Multi-University Training Contest 3

来源:互联网 发布:网络文档 编辑:程序博客网 时间:2024/06/06 06:36

传送门

The Goddess Of The Moon

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1471    Accepted Submission(s): 636


Problem Description
Chang’e (嫦娥) is a well-known character in Chinese ancient mythology. She’s the goddess of the Moon. There are many tales about Chang’e, but there’s a well-known story regarding the origin of the Mid-Autumn Moon Festival. In a very distant past, ten suns had risen together to the heavens, thus causing hardship for the people. The archer Yi shot down nine of them and was given the elixir of immortality as a reward, but he did not consume it as he did not want to gain immortality without his beloved wife Chang’e.



However, while Yi went out hunting, Fengmeng broke into his house and forced Chang’e to give up the elixir of immortality to him, but she refused to do so. Instead, Chang’e drank it and flew upwards towards the heavens, choosing the moon as residence to be nearby her beloved husband.



Yi discovered what had transpired and felt sad, so he displayed the fruits and cakes that his wife Chang’e had liked, and gave sacrifices to her. Now, let’s help Yi to the moon so that he can see his beloved wife. Imagine the earth is a point and the moon is also a point, there are n kinds of short chains in the earth, each chain is described as a number, we can also take it as a string, the quantity of each kind of chain is infinite. The only condition that a string A connect another string B is there is a suffix of A , equals a prefix of B, and the length of the suffix(prefix) must bigger than one(just make the joint more stable for security concern), Yi can connect some of the chains to make a long chain so that he can reach the moon, but before he connect the chains, he wonders that how many different long chains he can make if he choose m chains from the original chains.
 

Input
The first line is an integer T represent the number of test cases.
Each of the test case begins with two integers n, m.
(n <= 50, m <= 1e9)
The following line contains n integer numbers describe the n kinds of chains.
All the Integers are less or equal than 1e9.
 

Output
Output the answer mod 1000000007.
 

Sample Input
2
10 50
12 1213 1212 1313231 12312413 12312 4123 1231 3 131
5 50
121 123 213 132 321
 

Sample Output
86814837
797922656

Hint
11 111 is different with 111 11

题目大意:

给你 n(<=50) 种字符串。如果 a 串的后缀和 b 串的前缀相等,并且长度 >=2,则

b串可以连在 a 串后面,每个串的个数都是无穷个,现在让你选m(<=109)个串,问

你有构成多少个不同的串?

解题思路:

其实我们可以先构造一个转移矩阵 A 的,A[i][j] 的值表示 j 链能否连接在 i 链后面(

1 表示可以,0 表示不可以),得到这个矩阵之后,我们就将初始状态设置为全是 1,那么 A 矩阵的 m1 就是要得到的矩阵,然后再乘以初始状态就行了,最后对矩阵的第

一行的每个元素求和就行了。

MyCode

/**2016 - 08 - 25 晚上Author: ITAKMotto:今日的我要超越昨日的我,明日的我要胜过今日的我,以创作出更好的代码为目标,不断地超越自己。**/#include <iostream>#include <algorithm>#include <stdio.h>#include <stdlib.h>#include <cmath>#include <cstring>#include <set>using namespace std;typedef long long LL;string s[60];int len[60];const int MAXN = 60;int n = 0;const int MOD = 1e9+7;typedef struct{    LL mat[MAXN][MAXN];} Matrix;Matrix p, I;Matrix Init_I(){    memset(I.mat, 0, sizeof(I.mat));    for(int i=0; i<MAXN; i++)    {        for(int j=0; j<MAXN; j++)        {            if(i == j)                I.mat[i][j] = 1LL;        }    }}///矩阵乘法Matrix Mul_Matrix(Matrix a, Matrix b){    Matrix c;    for(int i=0; i<n; i++)    {        for(int j=0; j<n; j++)        {            c.mat[i][j] = 0;            for(int k=0; k<n; k++)            {                c.mat[i][j] = (c.mat[i][j] + a.mat[i][k] * b.mat[k][j]) % MOD;                c.mat[i][j] %= MOD;            }        }    }    return c;}///矩阵的快速幂Matrix quick_Mod_Matrix(int m){    Matrix ans = I, b = p;    while(m)    {        if(m & 1)            ans = Mul_Matrix(ans, b);        m>>=1;        b = Mul_Matrix(b, b);    }    return ans;}string str[MAXN];set <string> se;int main(){    Init_I();    int T;    int m;    cin>>T;    while(T--)    {        memset(p.mat,0,sizeof(p.mat));        scanf("%d%d",&n,&m);        int cnt = 0;        se.clear();        for(int i=0; i<n; i++)        {            string st;            cin>>st;            if(se.find(st) == se.end())            {                se.insert(st);                s[cnt] = st;                len[cnt++]=st.size();            }        }        n = cnt;        int sum = 0;        for(int i=0; i<n; i++)        {            for(int j=0; j<n; j++)            {                int minn=min(len[i],len[j]);                for(int k=2; k<=minn; k++)                {                    int flag=1;                    for(int ii=0; ii<k; ii++)                    {                        if(s[i][ii]!=s[j][len[j]-(k-ii)])                        {                            flag=0;                            break;                        }                    }                    if(flag==1)                    {                        p.mat[j][i]=1;                        break;                    }                }            }        }        Matrix tmp = quick_Mod_Matrix(m-1);        Matrix tp;        memset(tp.mat, 0, sizeof(tp.mat));        for(int i=0; i<n; i++)            tp.mat[0][i] = 1;        tmp = Mul_Matrix(tp, tmp);        LL ans = 0;        for(int i=0; i<n; i++)        {            ans = ans+tmp.mat[0][i];            ans %= MOD;        }        ans = (ans%MOD+MOD)%MOD;        cout<<ans<<endl;    }    return 0;}
0 0