HDOJ 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): 540    Accepted Submission(s): 215

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

Source

2015 Multi-University Training Contest 3

/* ***********************************************
Author        :CKboss
Created Time  :2015年07月29日 星期三 09时55分36秒
File Name     :HDOJ5318.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

typedef long long int LL;

const LL mod=1000000007;

int n,m;

string str[55];

bool check(int a,int b)
{
	if(a==b) return true;

	int n1=str[a].length();
	int n2=str[b].length();

	for(int i=2,ml=min(n1,n2);i<=ml;i++)
	{
		int p1=n1-i,p2=0;

		bool flag=true;
		for(int j=0;j<i&&flag;j++,p1++,p2++)
		{
			if(str[a][p1]==str[b][p2]) continue;
			else flag=false;
		}

		if(flag==true) return flag;
	}

	return false;

}

struct Matrix
{
	LL m[52][52];

	Matrix()
	{
		memset(m,0,sizeof(m));
	}

	void getE()
	{
		for(int i=0;i<n;i++) m[i][i]=1;
	}

	void toString()
	{
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
				cout<<m[i][j]<<" ";
			cout<<endl;
		}
		cout<<endl;
	}
};

Matrix Muilt(Matrix a,Matrix b)
{
	Matrix mat;

	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			LL temp=0;
			for(int k=0;k<n;k++)
			{
				temp=(temp+(a.m[i][k]*b.m[k][j])%mod)%mod;
			}
			mat.m[i][j]=temp;
		}
	}

	return mat;
}

Matrix Pow(Matrix& m,int k)
{
	Matrix e;
	e.getE();

	while(k)
	{
		if(k&1) e=Muilt(e,m);
		m=Muilt(m,m);
		k/=2;
	}
	return e;
}

LL Solve(Matrix& e)
{
	LL ans=0;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			ans=(ans+e.m[i][j])%mod;
		}
	}

	return ans;
}

set<string> ss;

int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);

	int T_T;
	scanf("%d",&T_T);
	while(T_T--)
	{
		scanf("%d%d",&n,&m);

		ss.clear();

		int kut=0;
		string sss;

		for(int i=0;i<n;i++)
		{
			cin>>sss;
			if(sss.length()<2) continue;
			if(ss.count(sss)==0)
			{
				ss.insert(sss);
				str[kut++]=sss;
			}
		}
		n=kut;
		Matrix mt;
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				if(check(i,j)==true) mt.m[i][j]=1;
			}
		}

		Matrix e = Pow(mt,m-1);
		cout<<Solve(e)<<endl;;
	}

    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-28 16:54:55

HDOJ 5318 The Goddess Of The Moon 矩阵快速幂的相关文章

HDU 5318 The Goddess Of The Moon (矩阵快速幂)

题目链接:HDU 5318 The Goddess Of The Moon 题意:给出N串字符串,若是一个字符串的后缀与另一个字符串的前缀相同并且长度大于1,就表示这两个字符串是可以相连的,问M个字符串相连不同方案数为多少. 思路: 1.将输入的字符串预处理存入一个矩阵中,mp[i][j]=1说明str[i]与str[j]能相连,反之,则不能相连. 2.str[i]与str[j]能相连 转化为 i点到j点可达,那么就可以得到一个有向图,长度为M的意思就是 两点之间所走的步数为M的不同走法有多少种

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 (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 438    Accepted Submission(s): 150 Problem Description Chang'e (嫦娥) is

矩阵快速幂 HDOJ 5318 The Goddess Of The Moon

题目传送门 1 /* 2 DP::dp[i][k] 表示选择i个字符串,最后一次是k类型的字符串,它由sum (dp[i-1][j]) (a[j], a[k] is ok)累加而来 3 矩阵快速幂:将n个字符串看成n*n的矩阵,如果匹配,矩阵对应位置为1.矩阵缩短递推dp时间,然后乘m-1次(dp[m][i])累加即可 4 注意去重 5 详细解释:http://blog.csdn.net/keshuai19940722/article/details/47111215 6 */ 7 #inclu

hdu5318 The Goddess Of The Moon (矩阵高速幂优化dp)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5318 题意:给定n个数字串和整数m,规定若数字串s1的后缀和数字串s2的前缀同样且长度≥2,则s2能够拼接在s1的后面,每一个串能够反复用,问拼接m个数字串有多少种方法. n<=50,m<=1e9 分析:定义dp[i][j]为拼接了i个串而且这个长串以s[j](输入的第j个数字串)结尾的方案数. 那么有 for(int i=1;i<=n;i++) dp[1][i]=1; for(int i=2;

hdu 5318 The Goddess Of The Moon(矩阵快速幂)

题目链接:hdu 5318 The Goddess Of The Moon 将50个串处理成50*50的矩阵,注意重复串. #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> using namespace std; const int maxn = 55; const int mod = 1e9+7; int N, M, A[maxn]; struct M

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 (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 800    Accepted Submission(s): 349 Problem Description Chang'e (嫦

【HDOJ 4686】 Arc of Dream (矩阵快速幂)

[HDOJ 4686] Arc of Dream (矩阵快速幂) 两个公式 a(i) = a(i-1)*Ax+Ay b(i) = b(i-1)*Bx+By 求 0~(n-1) 的a(i)*b(i) 初始矩阵为                                       求幂矩阵为 a0                                                      Ax          0           0          0        

HDOJ 6030 矩阵快速幂

链接: http://acm.hdu.edu.cn/showproblem.php?pid=6030 题意: 给一个手链染色,每连续素数个数的珠子中红色不能比蓝的多,问有多少种情况 题解: 公式为f[i]=f[i-1]+f[i-3],类似菲波那切数列,使用矩阵快速幂即可 代码: 31 typedef vector<ll> vec; 32 typedef vector<vec> mat; 33 34 mat mul(mat &A, mat &B) { 35 mat C

HDOJ Arc of Dream 4686【矩阵快速幂】

Arc of Dream Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 3126    Accepted Submission(s): 982 Problem Description An Arc of Dream is a curve defined by following function: where a0 = A0 ai =