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 namespace std;
const int mod = 2015;
int N,M;
struct Matrix
{
int M[55][55];
Matrix(){memset(M,0,sizeof(M));}
}U,P; Matrix Add(const Matrix &a,const Matrix &b)
{
Matrix ret;
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
ret.M[i][j]=(a.M[i][j]+b.M[i][j])%mod;
return ret;
} Matrix Multi(const Matrix &a,const Matrix &b)
{
Matrix ret;
for(int i=1;i<=N;i++)
{
for(int j=1;j<=N;j++)
{
for(int k=1;k<=N;k++)
ret.M[i][j]+=a.M[i][k]*b.M[k][j];
ret.M[i][j]%=mod;
}
}
return ret;
}
Matrix Pow(Matrix f,int n) //f^n,U为单位矩阵
{
Matrix ret=U;
while(n)
{
if(n&1)
ret=Multi(ret,f);
n>>=1;
f=Multi(f,f);
}
return ret;
} Matrix solve(int n) //等比数列求和
{
if(n==1)
return P;
Matrix temp=solve(n>>1);
if(n&1)
{
Matrix t=Pow(P,n/2+1);
return Add(Add(Multi(temp,t),temp),t);
}
else
return Add(temp,Multi(temp,Pow(P,n>>1)));
} int main()
{
int ncase,i,j,k,x;
for(i=0;i<55;i++)
U.M[i][i]=1;
scanf("%d",&ncase);
while(ncase--)
{
scanf("%d%d",&N,&M);
memset(P.M,0,sizeof(P.M));
for(i=1;i<=N;i++)
{
scanf("%d",&k);
while(k--)
{
scanf("%d",&x);
P.M[i][x]=1;
}
}
if(M==1)
{
printf("%d\n",N+1);
continue ;
}
Matrix temp=solve(M-1);
int ans=N+1;
for(i=1;i<=N;i++)
for(j=1;j<=N;j++)
ans+=temp.M[i][j];
printf("%d\n",ans%mod);
}
return 0;
}
hdu 5411 CRB and Puzzle (矩阵高速幂优化dp)的更多相关文章
- hdu 5411 CRB and Puzzle 矩阵高速幂
链接 题解链接:http://www.cygmasot.com/index.php/2015/08/20/hdu_5411/ 给定n个点 常数m 以下n行第i行第一个数字表示i点的出边数.后面给出这些 ...
- HDOJ 5411 CRB and Puzzle 矩阵高速幂
直接构造矩阵,最上面一行加一排1.高速幂计算矩阵的m次方,统计第一行的和 CRB and Puzzle Time Limit: 2000/1000 MS (Java/Others) Memory ...
- hdu5318 The Goddess Of The Moon (矩阵高速幂优化dp)
题目:pid=5318">http://acm.hdu.edu.cn/showproblem.php?pid=5318 题意:给定n个数字串和整数m,规定若数字串s1的后缀和数字串s2 ...
- HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和)
HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 1588 Gauss Fibonacci 题意: g(i)=k*i+b;i为变量. 给出 ...
- 2018.10.23 bzoj1297: [SCOI2009]迷路(矩阵快速幂优化dp)
传送门 矩阵快速幂优化dp简单题. 考虑状态转移方程: f[time][u]=∑f[time−1][v]f[time][u]=\sum f[time-1][v]f[time][u]=∑f[time−1 ...
- HDU 5411 CRB and puzzle (Dp + 矩阵高速幂)
CRB and Puzzle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) T ...
- HDU 5411 CRB and Puzzle (2015年多校比赛第10场)
1.题目描写叙述:pid=5411">点击打开链接 2.解题思路:本题实际是是已知一张无向图.问长度小于等于m的路径一共同拥有多少条. 能够通过建立转移矩阵利用矩阵高速幂解决.当中,转 ...
- HDU 2256 Problem of Precision(矩阵高速幂)
题目地址:HDU 2256 思路: (sqrt(2)+sqrt(3))^2*n=(5+2*sqrt(6))^n; 这时要注意到(5+2*sqrt(6))^n总能够表示成an+bn*sqrt(6); a ...
- 2018.10.22 bzoj1009: [HNOI2008]GT考试(kmp+矩阵快速幂优化dp)
传送门 f[i][j]f[i][j]f[i][j]表示从状态"匹配了前i位"转移到"匹配了前j位"的方案数. 这个东西单次是可以通过跳kmp的fail数组得到的 ...
随机推荐
- Who is YaoGe.(搞笑篇)
耀哥是google的大牛.主持google各种牛逼分布式系统的设计,比方Mapreduce之类的,关于大神的传说,如同春哥一样多,当然,有些传说仅仅有程序猿能明确! 耀哥当初面试Google时.被 ...
- SIMATIC PID温度控制
SIMATIC PID温度控制 // VAR_INPUT ------------------------------------------------------------------- #if ...
- CoreSight™ Technology
ARM Cortex-M processor-based devices use the ARM CoreSight technology which introduces powerful new ...
- Parallel Programming--perfbook
https://www.kernel.org/pub/linux/kernel/people/paulmck/perfbook/perfbook.html
- Enable WiX project in Visual Studio 2013
I have a VS 2012 solution with WiX Installer projects. However, when I open the solution in VS 2013 ...
- AngularJS使用ngMessages进行表单验证
名称为"ngMessages"的module,通过npm install angular-messages进行安装.在没有使用ngMessages之前,我们可能这样写验证: < ...
- eclipse中设置自定义文档签名(工具)
今天第一次认真学习eclipse的使用,看到自定义文档签名,步骤如下: 1.点击window->preferences->java->Code Style->Code Tem ...
- iOS 各种控件默认高度(图示)
1.状态栏 状态栏一般高度为20像素,在打手机或者显示消息时会放大到40像素高,注意,两倍高度的状态栏在好像只能在纵向的模式下使用.如下图 用户可以隐藏状态栏,也可以将状态栏设置为灰色,黑色或者半透明 ...
- 【python】python安装步骤
1.官网下载python 官网地址:https://www.python.org/getit/ 2.下载完成后点击安装 勾选Add python to PATH 是可以自己去配置环境变量的 注意:这里 ...
- 平台升级至nginx+Tomcat9.0.1(Spring5.0.1+velocity2.0+quartz-2.3.0)+redis集群
在公司部份应用上 使用了 Tomcat9.0.1 稳定性还可以,由于将公司的集群服务也升级为 Tomcat9.0.1,下面我们来谈一下改变: 1:logging.properties 支持日志最大天数 ...