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 (嫦娥) 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

2015 Multi-University Training Contest 3

Recommend

wange2014   |   We have carefully selected several similar problems for you:  5338 5337 5334 5333 5332

解题:

比赛的时候,都没看到这道题,以为超级难,过的人很少,其实就是道矩阵快速幂,不过也有可能过不了,因为有个坑点,串可能重复,所以要去重。

离散中的可达矩阵B(i,j)的意义是,通过路径长为x,能够从i点到j点,通过长度为x的路径到达的方式有B(i,j)种。m为1时,需特判,方案数为不重复的串的数量。其余情况m--,通过矩阵快速幂求去重后矩阵的(m-1)次,并累加结果即可。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#define maxn 55
#define mod 1000000007
using namespace std;
int n,len[55],cnt;
struct Matrix
{
	int map[maxn][maxn];
};
//单位矩阵和计算矩阵
Matrix unit,refl;
int min(int a,int b)
{
	return a<b?a:b;
}
//单位矩阵
void init()
{
	memset(unit.map,0,sizeof(unit.map));
	for(int i=0;i<maxn;i++)
		unit.map[i][i]=1;
}
//矩阵乘
Matrix mult(Matrix a,Matrix b)
{
	int i,j,k;
	Matrix c;
	for(i=0;i<cnt;i++)
		for(j=0;j<cnt;j++)
		{
			c.map[i][j]=0;
			for(k=0;k<cnt;k++)
				c.map[i][j]=(c.map[i][j]+1LL*a.map[i][k]*b.map[k][j])%mod;
		}
	return c;
}
//快速幂
Matrix quick_pow(int x)
{
  Matrix tmp=refl,res=unit;
  while(x)
  {
	  //二进制,取或不取
	  if(x&1)
        res=mult(res,tmp);
	  x=x>>1;
	  tmp=mult(tmp,tmp);
  }
  return res;
}
int main()
{
	char store[55][55],temp[55][55];
    int t,m,l1,l2,ans;
	init();
	scanf("%d",&t);
	Matrix ansM;
	while(t--)
	{
	  //读入
	  memset(refl.map,0,sizeof(refl.map));
	  memset(ansM.map,0,sizeof(ansM.map));
      scanf("%d%d",&n,&m);
	  cnt=0;
	  //去重
	  for(int i=0;i<n;i++)
	  {
		  scanf("%s",temp[i]);
		  bool sign=true;
		  for(int j=0;j<i;j++)
		  {
			  if(strcmp(temp[j],temp[i])==0)
			  {
				  sign=false;
				  break;
			  }
		  }
		  if(sign)
		  {
			  int j;
			  for(j=0;j<strlen(temp[i]);j++)
			  {
				  store[cnt][j]=temp[i][j];
			  }
			  //这里忘记加串结束符,郁闷了我好久....
			  store[cnt][j]=0;
			  cnt++;
		  }
	  }
	  for(int i=0;i<cnt;i++)
		  len[i]=strlen(store[i]);
	  //特判
	  if(m==1)
	  {
		  printf("%d\n",cnt);
		  continue;
	  }
	  //处理能否连接关系
	  for(int i=0;i<cnt;i++)
	  {
		  if(len[i]>1)
		  {
            for(int j=0;j<cnt;j++)
			{
				if(len[j]>1)
				{
					if(i==j)
					{
						refl.map[i][i]=1;
						continue;
					}
					l1=len[i];
					l2=len[j];
					bool flag;
					for(int k=2;k<=min(l1,l2);k++)
					{
						flag=true;
						for(int z=1;z<=k;z++)
						{
                           if(store[i][l1-z]!=store[j][k-z])
						   {
							   flag=false;
							   break;
						   }
						}
						if(flag)break;
					}
                    if(flag)
						refl.map[i][j]=1;
				}
			}
		  }
	  }
      m--;
	  //快速幂
      ansM=quick_pow(m);
	  ans=0;
	  //累加
	  for(int i=0;i<cnt;i++)
	  {
		  for(int j=0;j<cnt;j++)
		  {
			  ans=(ans+ansM.map[i][j])%mod;
		  }
	  }
	  printf("%d\n",ans);
	}
	return 0;
}

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

时间: 2024-11-09 10:29:58

HDU 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 矩阵快速幂

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 mytholog

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 5171 GTY&#39;s birthday gift(矩阵快速幂 )

HDU 5171 GTY's birthday gift ( 矩阵快速幂裸题目 ) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define MAX_SIZE 3 #define MOD 10000007 #define clr( a, b ) memset( a, b, sizeof(a) ) typedef long long LL; struct M

HDU 4990 Reading comprehension(找规律+矩阵快速幂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4990 Problem Description Read the program below carefully then answer the question. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include<iostream> #include

hdu 2604 Queuing dp找规律 然后矩阵快速幂。坑!!

http://acm.hdu.edu.cn/showproblem.php?pid=2604 这题居然O(9 * L)的dp过不了,TLE,  更重要的是找出规律后,O(n)递推也过不了,TLE,一定要矩阵快速幂.然后立马GG. 用2代表m,1代表f.设dp[i][j][k]表示,在第i位,上一位站了的人是j,这一位站的人是k,的合法情况. 递推过去就是,如果j是1,k是2,那么这一位就只能放一个2,这个时猴dp[i][k][2] += dp[i - 1][j][k]; 其他情况分类下就好,然后

HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)

Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 249    Accepted Submission(s): 140 Problem Description Farmer John likes to play mathematics games with his N cows. Recently, t

(hdu 6030) Happy Necklace 找规律+矩阵快速幂

题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=6030 Problem Description Little Q wants to buy a necklace for his girlfriend. Necklaces are single strings composed of multiple red and blue beads. Little Q desperately wants to impress his girlfriend,