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/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后面
 
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const int maxn = 55;
 5 const LL mod = 1000000007;
 6 int n,m,d[maxn];
 7 struct Matrix {
 8     int m[maxn][maxn];
 9     Matrix() {
10         memset(m,0,sizeof m);
11     }
12     Matrix operator*(const Matrix &t) const {
13         Matrix ret;
14         for(int i = 0; i < n; ++i)
15             for(int j = 0; j < n; ++j)
16                 for(int k = 0; k < n; ++k)
17                     ret.m[i][j] = (ret.m[i][j] + (LL)m[i][k]*t.m[k][j]%mod)%mod;
18         return ret;
19     }
20 };
21 
22 bool check(int a,int b) {
23     char sa[20],sb[20];
24     sprintf(sa,"%d",d[a]);
25     sprintf(sb,"%d",d[b]);
26     for(int i = 0,j; sa[i]; ++i) {
27         for(j = 0; sb[j] && sa[i+j] && sb[j] == sa[i+j]; ++j);
28         if(!sa[i+j] && j > 1) return true;
29     }
30     return false;
31 }
32 Matrix quickPow(Matrix b,int a) {
33     Matrix ret;
34     for(int i = 0; i < n; ++i)
35         ret.m[i][i] = 1;
36     while(a) {
37         if(a&1) ret = ret*b;
38         a >>= 1;
39         b = b*b;
40     }
41     return ret;
42 }
43 int main() {
44     int kase;
45     scanf("%d",&kase);
46     while(kase--) {
47         scanf("%d%d",&n,&m);
48         for(int i = 0; i < n; ++i)
49             scanf("%d",d+i);
50         sort(d,d+n);
51         n = unique(d,d+n) - d;
52         Matrix a;
53         for(int i = 0; i < n; ++i)
54             for(int j = 0; j < n; ++j)
55                 a.m[i][j] = check(i,j);
56         Matrix ret = quickPow(a,m-1);
57         int ans = 0;
58         for(int i = 0; i < n; ++i)
59             for(int j = 0; j < n; ++j)
60                 ans = (ans + ret.m[i][j])%mod;
61         printf("%d\n",ans);
62     }
63     return 0;
64 }
View Code

 

转载于:https://www.cnblogs.com/crackpotisback/p/4715200.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值