隐藏页面特效

hdu 5318 The Goddess Of The Moon

1|0The Goddess Of The Moon


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


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
 

 

Author
ZSTU
 

 

Source
 

 

Recommend
wange2014   |   We have carefully selected several similar problems for you:  6022 6021 6020 6019 6018 
 

 

#include<set> #include<string> #include<cstdio> #include<cstring> #include<iostream> using namespace std; typedef long long ll; const int N=60; const ll mod=1e9+7; struct matrix{ ll s[N][N]; matrix(){ memset(s,0,sizeof s); } }; int n,m,T,cnt,len[N];ll ans; matrix operator *(const matrix &a,const matrix &b){ matrix c; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ for(int k=0;k<n;k++){ c.s[i][j]+=a.s[i][k]*b.s[k][j]; } c.s[i][j]%=mod; } } return c; } matrix fpow(matrix a,ll p){ matrix res; for(int i=0;i<n;i++) res.s[i][i]=1; for(;p;p>>=1,a=a*a) if(p&1) res=res*a; return res; } set<string>ag; string s[N],str; int main(){ for(scanf("%d",&T);T--;){ scanf("%d%d",&n,&m); matrix A,F; ag.clear();cnt=0; for(int i=1;i<=n;i++){ cin>>str; if(ag.find(str)==ag.end()){ ag.insert(str); s[cnt]=str; len[cnt++]=str.length(); } } n=cnt; for(int i=0,f,L;i<n;i++){ for(int j=0;j<n;j++){ L=min(len[i],len[j]); for(int l=2;l<=L;l++){ f=0; for(int k=0;k<l;k++){ if(s[i][k]!=s[j][len[j]-(l-k)]){ f=1; break; } } if(!f){ A.s[j][i]=1; break; } } } } for(int i=0;i<n;i++) F.s[0][i]=1; A=fpow(A,m-1); F=F*A; ll ans=0; for(int i=0;i<n;i++) ans=(ans+F.s[0][i])%mod; cout<<ans<<'\n'; } return 0; }

 


__EOF__

本文作者shenben
本文链接https://www.cnblogs.com/shenben/p/6727015.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   神犇(shenben)  阅读(557)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示