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. DVI与DVI-D的区别

    DVI-I兼容DVI-D和VGA,如果不使用VGA信号兼容,那么没有任何区别. 1) DVI接口是1999年由数字显示工作组DDWG(Digital Display Working Group)推出的 ...

  2. Window 2008 R2组策略之一——组策略管理控制台

    组策略管理在windows域管理中占有重要地位,本身也不是新的内容了.但微软在Windows2008中终于集成了一个非常好用的组策略管理工具——组策略管理控制台.并且为原有的组策略添加了新的元素.本文 ...

  3. java- ★学习资源★

    何静媛: http://blog.csdn.net/hejingyuan6/article/category/2367993 孤傲苍狼-java基础总结: http://www.cnblogs.com ...

  4. 专题实验 Statspack & 9大动态视图

    statspack 是一个DBA经常用的调优工具, 它的主要作用是, 针对数据库的不同时刻做快照, 然后来比对快照之前的差异和瓶颈, 快照可以是手动的也可以是自动的, 从 oracle 10g开始, ...

  5. Jquery 技术小结

    前记: 现在项目中经常要用到JS去操作一些事,对整个团队开发来说,JS的书写规范和正确对开发具有较大的帮助.在一个团队中常常会发生JS书写的不统一性和游览器不兼容性等情况发生.我觉的最好的方法就是有一 ...

  6. 第三百零三节,Django框架介绍——用pycharm创建Django项目

    Django框架介绍 Django是一个开放源代码的Web应用框架,由Python写成.采用了MVC的软件设计模式,即模型M,视图V和控制器C.它最初是被开发来用于管理劳伦斯出版集团旗下的一些以新闻内 ...

  7. 多种方法实现div两列等高(收集整理)

    HTML骨架 <div id="header">头部</div> <div id ="container"> <div ...

  8. Unity使用JsonFX插件进行序列化

    孙广东  2015.6.25 Unity and JSON – Quick Guide: 相比較XML的沉重和密集,Json更加高效. Introduction: 什么是 Json ?假设你从未使用过 ...

  9. fastx tookit 操作fasta/fastq 文件 (1)

    准备测试文件 test.fq, 包含4条fastq 文件,碱基编码格式为phred64; @FC12044_91407_8_200_406_24 NTTAGCTCCCACCTTAAGATGTTTA + ...

  10. BinarySearchTree二叉搜索树的实现

    /* 二叉搜索树(Binary Search Tree),(又:二叉查找树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; ...