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/Others)
Total Submission(s): 1487 Accepted Submission(s): 650
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.
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.
10 50
12 1213 1212 1313231 12312413 12312 4123 1231 3 131
5 50
121 123 213 132 321
797922656
11 111 is different with 111 11
#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的更多相关文章
- 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 ( ...
- 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/ ...
- 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/ ...
- 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个字符串看 ...
- hdu5318 The Goddess Of The Moon (矩阵高速幂优化dp)
题目:pid=5318">http://acm.hdu.edu.cn/showproblem.php?pid=5318 题意:给定n个数字串和整数m,规定若数字串s1的后缀和数字串s2 ...
- HDU 4348 SPOJ 11470 To the moon
Vjudge题面 Time limit 2000 ms Memory limit 65536 kB OS Windows Source 2012 Multi-University Training C ...
- hdu 5411 CRB and Puzzle (矩阵高速幂优化dp)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5411 题意:按题目转化的意思是,给定N和M,再给出一些边(u,v)表示u和v是连通的,问走0,1,2... ...
- 2015 Multi-University Training Contest 3
1001 Magician 线段树.根据奇偶性分成4个区间.维护子列和最大值. 想法很简单.但是并不好写. 首先初始化的时候对于不存在的点要写成-INF. 然后pushup的时候.对于每个区间要考虑四 ...
- 2015 多校联赛 ——HDU5302(矩阵快速幂)
The Goddess Of The Moon Sample Input 2 10 50 12 1213 1212 1313231 12312413 12312 4123 1231 3 131 5 5 ...
随机推荐
- CentOS6.5下安装Zabbix
一.安装环境LAMP 这次安装zabbix全程使用yum进行安装,在于速度快,而且,出错的机会小,如果以后在生产环境上时,也可以先使用可以联网的机器进行安装,安装好以后将机器隔离,这样安装速度更快更方 ...
- Oracle重置序列
oracle序列创建以后,如果想重置序列从 0 开始,逐渐递增1,可以采用如下存储过程: create or replace procedure reset_seq( p_seq_name in va ...
- chrome 如何利用快捷键将光标移动到地址栏
Windows: Ctrl + L 或 Alt + D 或 F6 Mac: Command + LLinux: Ctrl + L
- am335x uart分析
/************************************************************ * am335x uart分析 * 本文记录am335x uart驱动的注册 ...
- Python 出现错误 SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform.
报出SNIMissingWarning和InsecurePlatformWarning警告. 解决方法: 在cmd中输入: pip install pyopenssl ndg-httpsclient ...
- e675. 翻转缓冲图像
// To create a buffered image, see e666 创建缓冲图像 // Flip the image vertically AffineTransform tx = Aff ...
- Node.js进程通信模块child_process
前言 Node.js是一种单线程的编程模型,对Node.js的赞美和诟病的也都是因为它的单线程模型,所有的任务都在一个线程中完成(I/O等例外).单线程模型,不仅让代码非常简洁,更是直接避免了线程调度 ...
- ceRNA 调控机制
ceRNA 不同于mRNA, lncRNA, ncRNA 等概念,其指的既不是某一种类型的RNA(比如mRNA, lncRNA), 也不是某一类的RNA(如ncRNA); ceRNA 其实指的是不同种 ...
- Golang - OSX配置VIM下的Golang开发环境 (MacOS为例)
测试环境 MacOS 10.12.6 首先安装VIM brew install vim 我已经安装了 Vim 8.0版本 然后安装 Vundle ,这是一个vim包管理器 git clone http ...
- PHP json_decode 无法解析特殊问号字符
在通过别人接口请求信息的时候,偶尔会遇到由于部分字符,如以下情况,则通过json_decode是会返回null的 但是这种情况通常不是由于整体编码的问题,因为在解析的时候就是以utf-8的编码解析的 ...