The Goddess Of The Moon

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

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
 
解题:矩阵快速幂加速dp
 
$dp[i][j] = \sum{dp[i-1][k]*a[k][j]}$ a[k][j]表示j是否可以跟在i后面
 
 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = ;
const LL mod = ;
int n,m,d[maxn];
struct Matrix {
int m[maxn][maxn];
Matrix() {
memset(m,,sizeof m);
}
Matrix operator*(const Matrix &t) const {
Matrix ret;
for(int i = ; i < n; ++i)
for(int j = ; j < n; ++j)
for(int k = ; k < n; ++k)
ret.m[i][j] = (ret.m[i][j] + (LL)m[i][k]*t.m[k][j]%mod)%mod;
return ret;
}
}; bool check(int a,int b) {
char sa[],sb[];
sprintf(sa,"%d",d[a]);
sprintf(sb,"%d",d[b]);
for(int i = ,j; sa[i]; ++i) {
for(j = ; sb[j] && sa[i+j] && sb[j] == sa[i+j]; ++j);
if(!sa[i+j] && j > ) return true;
}
return false;
}
Matrix quickPow(Matrix b,int a) {
Matrix ret;
for(int i = ; i < n; ++i)
ret.m[i][i] = ;
while(a) {
if(a&) ret = ret*b;
a >>= ;
b = b*b;
}
return ret;
}
int main() {
int kase;
scanf("%d",&kase);
while(kase--) {
scanf("%d%d",&n,&m);
for(int i = ; i < n; ++i)
scanf("%d",d+i);
sort(d,d+n);
n = unique(d,d+n) - d;
Matrix a;
for(int i = ; i < n; ++i)
for(int j = ; j < n; ++j)
a.m[i][j] = check(i,j);
Matrix ret = quickPow(a,m-);
int ans = ;
for(int i = ; i < n; ++i)
for(int j = ; j < n; ++j)
ans = (ans + ret.m[i][j])%mod;
printf("%d\n",ans);
}
return ;
}

最新文章

  1. MySql: 常见错误
  2. JavaScript:Location
  3. 服务器Ubuntu16.04下连接锐捷
  4. git Could not read from remote repository 解决
  5. 素数筛 poj 2689
  6. python 数据类型(sequence 序列、dictionary 词典、动态类型)
  7. 【DWT笔记】傅里叶变换与小波变换
  8. 微信分组群发45028,微信分组群发has no masssend quota hint
  9. [SOJ]&#160;Babelfish
  10. 简聊iOS支付集成(支付宝和微信支付)
  11. 第二次项目冲刺(Beta阶段)--第七天
  12. css元素选择器 first-child nth-child
  13. Python3 实现数据读写分离设计
  14. CorelDraw X8 破解激活问题
  15. TCP/IP协议、UDP协议、 Http协议
  16. java 线程理解
  17. 动画讲解TCP的3次握手,4次挥手
  18. Spring Security(二十一):6.3 Advanced Web Features
  19. NModbus类库使用
  20. [转载]error while loading shared libraries的解決方法

热门文章

  1. 如何在 VMware 上安装 CentOS 6.8
  2. OA项目知识总结
  3. 转载:Java编程风格与命名规范整理
  4. 八皇后问题java实现
  5. 用PHP去实现静态化
  6. Window7幻灯片字体显示混乱,难道真的是病毒么
  7. iOS-为方便项目开发在pch加入一些经常使用宏定义
  8. AngularJS 下拉列表demo
  9. 13.QT多窗口切换list
  10. Redis学习笔记(七) 基本命令:Set操作