|
The Goddess Of The MoonTime Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 746 Accepted Submission(s): 326
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
Sample Output
Source
|
#include<cstdio> #include<cmath> #include<stdlib.h> #include<map> #include<set> #include<time.h> #include<vector> #include<queue> #include<string> #include<string.h> #include<iostream> #include<algorithm> using namespace std; #define eps 1e-8 #define INF 0x3f3f3f3f #define LL long long #define max(a,b) ((a)>(b)?(a):(b)) #define min(a,b) ((a)<(b)?(a):(b)) #define MOD 1000000007 #define maxn 55 int n, m; struct Matrix { LL go[maxn][maxn]; Matrix() { memset(go, 0, sizeof(go)); } }; Matrix mul(Matrix a, Matrix b) { Matrix tmp; for(int k=1; k<=n; k++) for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) tmp.go[i][j] = (tmp.go[i][j] + a.go[i][k]*b.go[k][j]) % MOD; return tmp; } Matrix mul_pow(Matrix a, int k) { Matrix res; for(int i=1; i<=n; i++) res.go[1][i] = 1; while(k) { if(k & 1) res = mul(res, a); a = mul(a, a); k >>= 1; } return res; } set<string> S; string str[maxn]; int check(string a, string b) { int len1 = a.size(), len2 = b.size(); if(len1 == 1 || len2 == 1) return 0; for(int i=len1-2; i>=0; i--) { int j = 0; int t = i; while(a[t] == b[j] && j < len2 && t < len1) { j++; t++; } if(t == len1) return 1; } return 0; } int main() { int T; scanf("%d", &T); while(T--) { S.clear(); scanf("%d%d", &n, &m); for(int i=0; i<n; i++) { cin>>str[i]; S.insert(str[i]); } n = S.size(); int cnt = 0; for(set<string>::iterator it=S.begin(); it!=S.end(); it++) str[cnt++] = *it; Matrix M; for(int i=0; i<n; i++) for(int j=0; j<n; j++) { if(check(str[i], str[j])) M.go[i+1][j+1] = 1; } Matrix ans, tmp = M; tmp = mul_pow(tmp, m-1); for(int i=1; i<=n; i++) ans.go[1][i] = 1; ans = mul(ans, tmp); // m--; // while(m) // { // if(m & 1) // ans = mul(ans , tmp); // tmp = mul(tmp , tmp); // m >>= 1; // } LL sum = 0; for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) sum = (sum + ans.go[i][j]) % MOD; printf("%I64d\n", sum); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/dojintian/article/details/47145227