The 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=;
const ll mod=1e9+;
struct matrix{
ll s[N][N];
matrix(){
memset(s,,sizeof s);
}
};
int n,m,T,cnt,len[N];ll ans;
matrix operator *(const matrix &a,const matrix &b){
matrix c;
for(int i=;i<n;i++){
for(int j=;j<n;j++){
for(int k=;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=;i<n;i++) res.s[i][i]=;
for(;p;p>>=,a=a*a) if(p&) 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=;
for(int i=;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=,f,L;i<n;i++){
for(int j=;j<n;j++){
L=min(len[i],len[j]);
for(int l=;l<=L;l++){
f=;
for(int k=;k<l;k++){
if(s[i][k]!=s[j][len[j]-(l-k)]){
f=;
break;
}
}
if(!f){
A.s[j][i]=;
break;
}
} }
}
for(int i=;i<n;i++) F.s[][i]=;
A=fpow(A,m-);
F=F*A;
ll ans=;
for(int i=;i<n;i++) ans=(ans+F.s[][i])%mod;
cout<<ans<<'\n';
}
return ;
}

hdu 5318 The Goddess Of The Moon的更多相关文章

  1. hdu 5318 The Goddess Of The Moon 矩阵高速幂

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5318 The Goddess Of The Moon Time Limit: 6000/3000 MS ( ...

  2. HDU 5318——The Goddess Of The Moon——————【矩阵快速幂】

    The Goddess Of The Moon Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  3. 2015 Multi-University Training Contest 3 hdu 5318 The Goddess Of The Moon

    The Goddess Of The Moon Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  4. DP+矩阵快速幂 HDOJ 5318 The Goddess Of The Moon

    题目传送门 /* DP::dp[i][k] 表示选择i个字符串,最后一次是k类型的字符串,它由sum (dp[i-1][j]) (a[j], a[k] is ok)累加而来 矩阵快速幂:将n个字符串看 ...

  5. hdu5318 The Goddess Of The Moon (矩阵高速幂优化dp)

    题目:pid=5318">http://acm.hdu.edu.cn/showproblem.php?pid=5318 题意:给定n个数字串和整数m,规定若数字串s1的后缀和数字串s2 ...

  6. HDU 4348 SPOJ 11470 To the moon

    Vjudge题面 Time limit 2000 ms Memory limit 65536 kB OS Windows Source 2012 Multi-University Training C ...

  7. hdu 5411 CRB and Puzzle (矩阵高速幂优化dp)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5411 题意:按题目转化的意思是,给定N和M,再给出一些边(u,v)表示u和v是连通的,问走0,1,2... ...

  8. 2015 Multi-University Training Contest 3

    1001 Magician 线段树.根据奇偶性分成4个区间.维护子列和最大值. 想法很简单.但是并不好写. 首先初始化的时候对于不存在的点要写成-INF. 然后pushup的时候.对于每个区间要考虑四 ...

  9. 2015 多校联赛 ——HDU5302(矩阵快速幂)

    The Goddess Of The Moon Sample Input 2 10 50 12 1213 1212 1313231 12312413 12312 4123 1231 3 131 5 5 ...

随机推荐

  1. linux设置开机服务自动启动/关闭自动启动命令

    linux设置开机服务自动启动/关闭自动启动命令 2012-02-06 15:13 [root@localhost ~]# chkconfig --list     显示开机可以自动启动的服务[roo ...

  2. DIV+CSS 命名规范

    常用的CSS命名规则: 头:header 内容:content/container 尾:footer 导航:nav 侧栏:sidebar栏目:column 页面外围控制整体布局宽度:wrapper 左 ...

  3. numpy得到数组的index

    itemindex = numpy.where(array==item)

  4. C++ 编译器用于把源代码编译成最终的可执行程序

    C++ 编译器写在源文件中的源代码是人类可读的源.它需要"编译",转为机器语言,这样 CPU 可以按给定指令执行程序. C++ 编译器用于把源代码编译成最终的可执行程序. 大多数的 ...

  5. 利用CMake和OpenCV源代码生成Visual Studio工程

    OpenCV1.0版本有windows,linux之分,笔者曾经一直使用Opencv1.0.这个版本在下载,安装之后,在 \OpenCV\_make文件夹下面已经存在了一个opencv.dsw的工程文 ...

  6. 10个最佳的触控手式的JavaScript框架(转)

    由于各种原因移动开发是一项艰难的工作,比如它是非常耗时的.充满压力的任务.最重要的是,作为一个开发人员,你必须保持更新所有最新 的技术和技巧——你必须知道所有最新的趋势,问题和解决方案等.例如跨浏览器 ...

  7. PHPOffice下PHPWord生成Word2007(docx)使用方法

    要正常使用,下载依赖包: PhpOffice/Common:https://github.com/PHPOffice/Common Zend/Escaper:https://github.com/ze ...

  8. html5移动端根据百度地图api获取详细地址

    <script type="text/javascript" src="js/BMap.js" ></script> <scrip ...

  9. POI-根据Cell获取对应的String类型值

    /** * 根据不同情况获取Java类型值 * <ul><li>空白类型<ul><li>返回空字符串</li></ul>< ...

  10. Spring-注入外部值

    Spring注入需要初始化,但前面均使用硬编码注入,如: JavaConfig配置: package soundSystem; import org.springframework.stereotyp ...