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 ;
}

2015 Multi-University Training Contest 3 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. 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. 2015 Multi-University Training Contest 8 hdu 5390 tree

    tree Time Limit: 8000ms Memory Limit: 262144KB This problem will be judged on HDU. Original ID: 5390 ...

  5. 2015 Multi-University Training Contest 8 hdu 5383 Yu-Gi-Oh!

    Yu-Gi-Oh! Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID:  ...

  6. 2015 Multi-University Training Contest 8 hdu 5385 The path

    The path Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: 5 ...

  7. 2015 Multi-University Training Contest 3 hdu 5324 Boring Class

    Boring Class Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  8. 2015 Multi-University Training Contest 3 hdu 5317 RGCDQ

    RGCDQ Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  9. 2015 Multi-University Training Contest 10 hdu 5406 CRB and Apple

    CRB and Apple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

随机推荐

  1. RobotFrameWork+APPIUM实现对安卓APK的自动化测试----第六篇【AppiumLibrary等待函数介绍】

    http://blog.csdn.net/deadgrape/article/details/50622441 废话不多说,少年们请看下面. Wait Until Page Contains text ...

  2. spring datasource和mybatis的datasource来源在哪里

    配置一个数据源     spring在第三方依赖包中包含了两个数据源的实现类包,其一是Apache的DBCP,其二是 C3P0.可以在Spring配置文件中利用这两者中任何一个配置数据源.  配置一个 ...

  3. HDU 4343

    二分加贪心,水过了.贪心是因为,不能存在覆盖,当存在覆盖时,留小坐标的. #include <iostream> #include <cstdio> #include < ...

  4. NHibernate之旅(14):探索NHibernate中使用视图

    本节内容 引入 1.持久化类 2.映射文件 3.測试 结语 引入 在数据库操作中,我们除了对表操作,还有视图.存储过程等操作,这一篇和下篇来学习这些内容.这篇我们来学习怎样在NHibernate中使用 ...

  5. Bootstrap警告

    警告(Alerts)向用户提供了一种定义消息样式的方式.它们为典型的用户操作提供了上下文信息反馈. 警告(Alerts) 步骤: 1.创建一个 <div> 2.并向其加入一个 .alert ...

  6. Android面试题集

    前几天整理了Java面试题集合,今天再来整理下Android相关的面试题集合.假设你希望能得到最新的消息,能够关注https://github.com/closedevice/interview-ab ...

  7. CocoaPods的简单介绍及安装和使用

    CocoaPods的简单介绍及安装和使用   一.CocoaPods是什么? 当你开发iOS应用时,会常常使用到非常多第三方开源类库.比方JSONKit.AFNetWorking等等. 可能某个类库又 ...

  8. linux含有某字符串的文件

    find .|xargs grep -ri "IBM" -l 只列出文件名:grep -rn "Item" * -l pattern files

  9. BZOJ:3441 乌鸦喝水

    bzoj:3441 乌鸦喝水 题目传送门 Description 一只乌鸦在自娱自乐,它在面前放了n个有魔力的水缸,水缸里装有无限的水. 他准备从第1个水缸飞到第n个水缸,共m次.在飞过一个水缸的过程 ...

  10. USACO 2.1 Hamming Codes

    Hamming CodesRob Kolstad Given N, B, and D: Find a set of N codewords (1 <= N <= 64), each of ...