HDU 5318 The Goddess Of The Moon(构造矩阵 )——2015 Multi-University Training Contest 3
来源:互联网 发布:网络文档 编辑:程序博客网 时间:2024/06/06 06:36
传送门
The Goddess Of The Moon
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1471 Accepted Submission(s): 636
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.
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.
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题目大意:
给你
你有构成多少个不同的串?
解题思路:
其实我们可以先构造一个转移矩阵
一行的每个元素求和就行了。
/**2016 - 08 - 25 晚上Author: ITAKMotto:今日的我要超越昨日的我,明日的我要胜过今日的我,以创作出更好的代码为目标,不断地超越自己。**/#include <iostream>#include <algorithm>#include <stdio.h>#include <stdlib.h>#include <cmath>#include <cstring>#include <set>using namespace std;typedef long long LL;string s[60];int len[60];const int MAXN = 60;int n = 0;const int MOD = 1e9+7;typedef struct{ LL mat[MAXN][MAXN];} Matrix;Matrix p, I;Matrix Init_I(){ memset(I.mat, 0, sizeof(I.mat)); for(int i=0; i<MAXN; i++) { for(int j=0; j<MAXN; j++) { if(i == j) I.mat[i][j] = 1LL; } }}///矩阵乘法Matrix Mul_Matrix(Matrix a, Matrix b){ Matrix c; for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { c.mat[i][j] = 0; for(int k=0; k<n; k++) { c.mat[i][j] = (c.mat[i][j] + a.mat[i][k] * b.mat[k][j]) % MOD; c.mat[i][j] %= MOD; } } } return c;}///矩阵的快速幂Matrix quick_Mod_Matrix(int m){ Matrix ans = I, b = p; while(m) { if(m & 1) ans = Mul_Matrix(ans, b); m>>=1; b = Mul_Matrix(b, b); } return ans;}string str[MAXN];set <string> se;int main(){ Init_I(); int T; int m; cin>>T; while(T--) { memset(p.mat,0,sizeof(p.mat)); scanf("%d%d",&n,&m); int cnt = 0; se.clear(); for(int i=0; i<n; i++) { string st; cin>>st; if(se.find(st) == se.end()) { se.insert(st); s[cnt] = st; len[cnt++]=st.size(); } } n = cnt; int sum = 0; for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { int minn=min(len[i],len[j]); for(int k=2; k<=minn; k++) { int flag=1; for(int ii=0; ii<k; ii++) { if(s[i][ii]!=s[j][len[j]-(k-ii)]) { flag=0; break; } } if(flag==1) { p.mat[j][i]=1; break; } } } } Matrix tmp = quick_Mod_Matrix(m-1); Matrix tp; memset(tp.mat, 0, sizeof(tp.mat)); for(int i=0; i<n; i++) tp.mat[0][i] = 1; tmp = Mul_Matrix(tp, tmp); LL ans = 0; for(int i=0; i<n; i++) { ans = ans+tmp.mat[0][i]; ans %= MOD; } ans = (ans%MOD+MOD)%MOD; cout<<ans<<endl; } return 0;}
0 0
- HDU 5318 The Goddess Of The Moon(构造矩阵 )——2015 Multi-University Training Contest 3
- hdu-5318 The Goddess Of The Moon(2015 Multi-University Training Contest 3)
- hdu 5318 The Goddess Of The Moon 矩阵快速幂
- hdu 5318 The Goddess Of The Moon 【矩阵快速幂】
- HDU 5318 The Goddess Of The Moon (矩阵快速幂)
- HDU 5318 The Goddess Of The Moon
- HDU 5318 The Goddess Of The Moon
- hdu 5318 The Goddess Of The Moon(矩阵快速幂)
- HDU 5318 The Goddess Of The Moon(矩阵快速幂)
- hdu 5318 The Goddess Of The Moon (矩阵快速幂)
- HDU 5318 The Goddess Of The Moon(矩阵快速幂详解)
- Hdu 5318 The Goddess Of The Moon (dp+矩阵快速幂)
- HDU 5318 The Goddess Of The Moon(矩阵快速幂)
- HDU 5373 The shortest problem(模拟)——2015 Multi-University Training Contest 7
- hdu 4348 To the moon(线段树成段更新) 2012 Multi-University Training Contest 5
- HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )
- hdu5318(2015多校3)--The Goddess Of The Moon(dp+矩阵优化)
- HDU 5318(The Goddess Of The Moon-矩阵乘法)[Template:矩阵]
- 关于Binder在Java上工作的一点愚见(一)
- Debug Caffe on Ubuntu with GDB
- ruby on rails中创建xml数据
- NYOJ 199 无线网络覆盖
- tjut 2846
- HDU 5318 The Goddess Of The Moon(构造矩阵 )——2015 Multi-University Training Contest 3
- 适配器
- Java中判断字符串是否为数字的五种方法
- Effective Modern C++ 条款18 用std::unique_ptr管理独占所有权的资源
- 1014 福尔摩斯的约会
- POJ 2109
- 李笑来 -把时间当作朋友
- 上拉XML
- Convert Sorted List to Binary Search Tree