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;i<=m;i++)
	for(int j=1;j<=n;j++)
		for(int k=1;k<=n;k++)
			if(connect(j,k))
				dp[i][j]+=dp[i-1][k];

然后,之前非常早有人跟我讲过用矩阵能够算路径数,,,,,,。能够利用上述递推式构造矩阵从而高速计算出结果。定义:A矩阵里面的元素ai表示以s[i]结尾的串的方案数。

B矩阵bij表示s[j]能够拼接在s[i]的后面。

那么结果矩阵就是A*B^(m-1);

比如:n=2,m=5,s[1]="322",s[2]="22",那么

A={1,1}
B={1,1,
   0,1}

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
const int maxn = 55;
const int mod = 1000000007;
int N,M,use[100];
char s1[20],s2[20];
struct Matrix
{
	long long M[maxn][maxn];
	Matrix(){memset(M,0,sizeof(M));}
}U,P;
Matrix Multi(Matrix &a,Matrix &b)
{
	Matrix ans;
	for(int i=0;i<N;i++)
		for(int j=0;j<N;j++)
			for(int k=0;k<N;k++)
				ans.M[i][j]=(ans.M[i][j]+a.M[i][k]*b.M[k][j])%mod;
	return ans;
}
Matrix Power(Matrix a,int n)
{
	Matrix ans=U;
	while(n)
	{
		if(n&1)
			ans=Multi(ans,a);
		n>>=1;
		a=Multi(a,a);
	}
	return ans;
}
bool Match(int a,int b)  //ok
{
	sprintf(s1,"%d",a);
	sprintf(s2,"%d",b);
	int len1=strlen(s1),len2=strlen(s2);
	for(int i=len1-1,j=0;i>=0 && j<len2;i--,j++)
		if(len1-i>=2 && string(s1+i,s1+len1)==string(s2,s2+j+1))
			return true;
	return false;
}
void Init()
{
	memset(P.M,0,sizeof(P.M));
	for(int i=0;i<N;i++)
		for(int j=0;j<N;j++)
			if(Match(use[i],use[j]))
				P.M[i][j]=1;
}
long long GetAns(Matrix &ans)
{
	long long ret=0;
	for(int i=0;i<N;i++)
		for(int j=0;j<N;j++)
			ret+=ans.M[i][j];
	return ret%mod;
}
int main()
{
	for(int i=0;i<maxn;i++)
		U.M[i][i]=1;
	int ncase,i,j;
	scanf("%d",&ncase);
	while(ncase--)
	{
		scanf("%d%d",&N,&M);
		for(i=0;i<N;i++)
			scanf("%d",&use[i]);
		sort(use,use+N);
		N=unique(use,use+N)-use;
		Init();
		Matrix ans=Power(P,M-1);
		printf("%I64d\n",GetAns(ans));
	}
	return 0;
} 
时间: 2024-08-03 03:19:57

hdu5318 The Goddess Of The Moon (矩阵高速幂优化dp)的相关文章

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

hdu 5411 CRB and Puzzle (矩阵快速幂优化dp)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5411 题意:按题目转化的意思是,给定N和M,再给出一些边(u,v)表示u和v是连通的,问走0,1,2.....M步的方案数. 分析:这题和 hdu5318 The Goddess Of The Moon差不多,就是多了一个等比数列求和. 代码: #include <cstdio> #include <iostream> #include <cstring> using name

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的不同走法有多少种

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

矩阵快速幂 优化dp 模板

相关博客 :https://blog.csdn.net/china_xyc/article/details/89819376#commentBox 关于能用矩阵乘法优化的DP题目,有如下几个要求: 转移式只有加法,清零,减法etc.,max和min运算不允许 转移式中关于前几位dp结果得到的系数必须是常量 转移次数一般超级多 由于转移次数多,一般都要模一个int范围内的数 综上,举一个例子: dp[i]=a×dp[i−1]+b×dp[i−2]+c×dp[i−3] 其中,a,b,c是常量,而在需要

bzoj 4000 矩阵快速幂优化DP

建立矩阵,跑快速幂 1 /************************************************************** 2 Problem: 4000 3 User: idy002 4 Language: C++ 5 Result: Accepted 6 Time:32 ms 7 Memory:836 kb 8 ****************************************************************/ 9 10 #inclu

LibreOJ #2325. 「清华集训 2017」小Y和恐怖的奴隶主(矩阵快速幂优化DP)

哇这题剧毒,卡了好久常数才过T_T 设$f(i,s)$为到第$i$轮攻击,怪物状态为$s$时对boss的期望伤害,$sum$为状态$s$所表示的怪物个数,得到朴素的DP方程$f(i,s)=\sum \frac{1}{sum+1}*(f(i+1,s')+[s==s'])$ 状态数只有$C_{8+3}^3=165$个,所以就可以矩乘优化啦.再加上一个用于转移的$1$,矩阵大小是$166*166$的,因为多组询问,所以可以先把$2$的所有次幂的矩阵都预处理出来. 然后会发现复杂度是$O(T*166^3

排队 矩阵快速幂优化dp

\(T1\) 排队 ? Description ?? 抢饭是高中生活的一部分,现在有一列队伍长度为 \(n\),(注意:由于人与人之间要保持距离,且不同情况所保持的距离大小不同,所以长度并不能直接体现队列的人数).已知男男之间的距离为 \(a\),男女之间距离为 bb,女女之间距离为 \(c\).一个男生打饭时间为 \(d\),一个女生打饭时间为 \(e\),求所有情况的排队时间总和(忽略身体的大小对队伍长度的贡献),答案对 $10^{9}+7 $取模. ?? Input Format 一行六个

czy的后宫——矩阵快速幂优化DP

题意 有 n 个位置排成一行,可以放 m 种妹子.每个位置可以放也可以不放,规定某些妹子不能相邻,求方案数. 分析 #include<bits/stdc++.h> using namespace std; typedef long long ll; ll qmul(ll x,ll y,ll p){ //快速乘 x%=p; y%=p; ll ans=0; while(y){ if(y&1){ ans+=x; if(ans>=p) ans-=p; //这样写不能有负数 } x<