您的位置:首页 > 编程语言 > Go语言

HDU 5318 The Goddess Of The Moon(矩阵快速幂)

2016-07-16 19:08 771 查看
http://acm.hdu.edu.cn/showproblem.php?pid=5318

The Goddess Of The Moon

Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1447 Accepted Submission(s): 625

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个链,但这n个链中可能会有重复的链,所以要把重复的链去掉。然后开始构造矩阵,如果一个字符串的后缀为另外一个字符串的前缀(缀的长度大于等于2),则这两个字符串在矩阵中对应的值为1,否则为0;由于技术原因,那些图不好画,在这里只给出代码了。。。希望可以从代码中看出来。。。

下面是AC代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
#define Mod 1000000007
int sum;

int judge(string a,string b)
{
int i,j,flag;
for(i=2; i<=a.size()&&i<=b.size(); i++)
{
flag=1;
for(j=0; j<i; j++)
{
if(a[a.size()-i+j]!=b[j])
flag=0;
}
if(flag)
return 1;
}
return 0;
}

struct Mat
{
ll a[55][55];
void init()
{
memset(a,0,sizeof(a));
for(int i=0; i<sum; i++)
{
a[i][i]=1;
}
}
void init2()
{
memset(a,0,sizeof(a));
for(int i=0; i<sum; i++)
{
a[0][i]=1;
}
}
};

Mat mul(Mat a,Mat b)
{
Mat ans;
for(int i=0; i<sum; i++)
{
for(int j=0; j<sum; j++)
{
ans.a[i][j]=0;
for(int k=0; k<sum; k++)
{
ans.a[i][j]=(ans.a[i][j]+a.a[i][k]*b.a[k][j])%Mod;
}
ans.a[i][j]%=Mod;
}
}
return ans;
}

Mat power(Mat a,ll num)
{
Mat ans;
ans.init();
while(num)
{
if(num&1)
{
ans=mul(ans,a);
num--;
}
num/=2;
a=mul(a,a);
}
return ans;
}
string str[105];

int main()
{
int t,n;
ll m;
scanf("%d",&t);
while(t--)
{
scanf("%d%lld",&n,&m);
string str1;
sum=0;
for(int i=0; i<n; i++)
{
int flag=0;
cin>>str1;
for(int j=0; j<sum; j++)
{
if(str1==str[j])
{
flag=1;
break;
}
}
if(flag==0)
{
str[sum]=str1;
sum++;
}
}
Mat c;
c.init2();
Mat b;
for(int i=0; i<sum; i++)
{
for(int j=0; j<sum; j++)
{
b.a[i][j]=0;
if(judge(str[i],str[j]))
{
b.a[i][j]=1;
}
}
}
Mat ans,s;
s=power(b,m-1);
ans=mul(c,s);
ll ss=0;
for(int i=0; i<sum; i++)
{
ss=(ss+ans.a[0][i])%Mod;
}
printf("%lld\n",ss%Mod);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  HDU