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数组得到的 ...
随机推荐
- STM32F4 Alternate function mapping
#define GPIO_AF0_MCO // MCO (MCO1 and MCO2) Alternate Function mapping #define GPIO_AF0_RTC_50Hz // ...
- JS删除String里某个字符的方法
关于JS删除String里的字符的方法,一般使用replace()方法.但是这个方法只会删除一次,如果需要将string里的所以字符都删除就要用到正则. 1 2 3 4 var str = " ...
- 使用Oracle DBLink进行数据库之间对象的訪问操作
Oracle中自带了DBLink功能,它的作用是将多个oracle数据库逻辑上看成一个数据库,也就是说在一个数据库中能够操作还有一个数据库中的对象,比如我们新建了一个数据database1.我们须要操 ...
- Revit API通过相交过滤器找到与风管相交的对象。
相交过滤器的应用,比几何相交法简便.Excluding剔除 //找到与风管相交的对象,通过相交过滤器. [TransactionAttribute(Autodesk.Revit.Attributes. ...
- XPath and TXmlDocument
XML example, from the OmniXML XPath demo: <?xml version="1.0" encoding="UTF-8" ...
- 【Devops】【docker】【CI/CD】Jenkins源代码管理 添加gitlab项目地址,报错Failed to connect to repository : Error performing command: ls-remote -h git@192.168.92.130:8090/root/swapping.git HEAD
Jenkins源代码管理 添加gitlab项目地址 报错如下: Failed to connect to repository : Error performing command: ls-remot ...
- ios之申请后台延时执行和做一个假后台的方法
转自:http://sis hu ok.com/forum/blogCategory/showByCategory.html?categories_id=138&user_id=10385 ...
- Shape画圆形控件
这里涉及到shape的运用,这仅仅是一个实例 circle.xml <?xml version="1.0" encoding="utf-8"?> & ...
- 同一个ImageView根据xml文件来显示不同的图片--level-list
感谢:http://blog.sina.com.cn/s/blog_6111ce890100psq9.html 有时候,我们为了在一个ImageView中显示不同的图片,平时往往会使用: if (条件 ...
- 《Google Glass开发指南》
<Google Glass开发指南> 基本信息 作者: BestApp工作室 丛书名: 图灵原创 出版社:人民邮电出版社 ISBN:9787115349477 上架时间:2014-3-19 ...