|
Welcome to the Bestcoder-every Saturday night (with rice!) ) |
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 Descriptionchang ' E (Chang ' An) is a well-known character in Chinese ancient mythology. She ' s The goddess of the Moon. There is 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 is given the elixir of immortality as a reward, but he does 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 he house and forced Chang ' E to give up the elixir of immortality To him, but she refused-do. Instead, Chang ' E drank it and flew upwards towards the heavens, choosing the Moon as residence to being nearby her beloved Hu Sband.
Yi discovered what had transpired and felt sad, so he displayed the fruits and cakes that he wife Chang ' E had liked, and Gave sacrifices to her. Now, let's help Yi and the moon so, and he can see his beloved wife. Imagine the earth is a point and the moon was also a point, there was n kinds of short chains on the Earth, each chain is D Escribed as a number, we can also take it as a string, the quantity of each kind of chain are infinite. The only condition. A string a connect another string B is there is a suffix of a, equals a prefix of B, and the Leng th of the suffix (prefix) must bigger than one (just make the joint + stable for security concern), Yi can connect some O f The chains to make a long chain so that he can reach the moon, but before he connect the chains, he wonders Y different long chains he can make if he choose M chains from the original chains. Inputthe first line was an integer T represent the number of test cases. Each of the "test case" begins with the integers n, M. (n <=, M <= 1e9) The following line contains n integer numbers describe the n kinds of chains. All the integers is less or equal than 1e9.
Outputoutput the answer mod 1000000007. Sample Input210 5012 1213 1212 1313231 12312413 12312 4123 1231 3 1315 50121 123 213 132 321
Sample Output86814837797922656Hint111 is different with 111
Source2015 multi-university Training Contest 3
|
#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) #define MIN (a) < (b) ( A):(B)) #define MOD 1000000007#define maxn 55int 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;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Multi-School HDU