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
 

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;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值