题目: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)的更多相关文章

  1. hdu 5411 CRB and Puzzle 矩阵高速幂

    链接 题解链接:http://www.cygmasot.com/index.php/2015/08/20/hdu_5411/ 给定n个点 常数m 以下n行第i行第一个数字表示i点的出边数.后面给出这些 ...

  2. HDOJ 5411 CRB and Puzzle 矩阵高速幂

    直接构造矩阵,最上面一行加一排1.高速幂计算矩阵的m次方,统计第一行的和 CRB and Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  3. hdu5318 The Goddess Of The Moon (矩阵高速幂优化dp)

    题目:pid=5318">http://acm.hdu.edu.cn/showproblem.php?pid=5318 题意:给定n个数字串和整数m,规定若数字串s1的后缀和数字串s2 ...

  4. HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和)

    HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 1588 Gauss Fibonacci 题意:  g(i)=k*i+b;i为变量.  给出 ...

  5. 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 ...

  6. HDU 5411 CRB and puzzle (Dp + 矩阵高速幂)

    CRB and Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) T ...

  7. HDU 5411 CRB and Puzzle (2015年多校比赛第10场)

    1.题目描写叙述:pid=5411">点击打开链接 2.解题思路:本题实际是是已知一张无向图.问长度小于等于m的路径一共同拥有多少条. 能够通过建立转移矩阵利用矩阵高速幂解决.当中,转 ...

  8. 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 ...

  9. 2018.10.22 bzoj1009: [HNOI2008]GT考试(kmp+矩阵快速幂优化dp)

    传送门 f[i][j]f[i][j]f[i][j]表示从状态"匹配了前i位"转移到"匹配了前j位"的方案数. 这个东西单次是可以通过跳kmp的fail数组得到的 ...

随机推荐

  1. Access restriction: The method XXX from the type XXX is not accessible due to restriction XXX

    插件重构的时候 遇到这个问题 Access restriction: The method setDefaultAutoCommit(boolean) from the type BasicDataS ...

  2. Xamarin adventures – Differences between iOS simulator and device

    I had been happily coding an iOS app (targeting iPad) using Xamarin/VS.Net with everything working f ...

  3. jeffy-vim-v3.2

    jeffy-vim-v3.2 增加了vim-gutentags 插件,支持tags自动生成.

  4. Windows Phone本地数据库(SQLCE):10、创建数据库(翻译) (转)

    这是“windows phone mango本地数据库(sqlce)”系列短片文章的第十篇. 为了让你开始在Windows Phone Mango中使用数据库,这一系列短片文章将覆盖所有你需要知道的知 ...

  5. future封装了callable,thread封装future。

    三.使用Callable,Future返回结果 总结:future封装了callable,thread封装future.将callable的返回结果封装在future中,thread封装future, ...

  6. Maven 使用了一个标准的目录结构和一个默认的构建生命周期。

    Maven 使用了一个标准的目录结构和一个默认的构建生命周期. 约定优于配置 当创建 Maven 工程时,Maven 会创建默认的工程结构.开发者只需要合理的放置文件,而在 pom.xml 中不再需要 ...

  7. 解决sublime text 3中文乱码问题

    解决中文乱码,需要安装两个插件 一.安装Codecs Ctrl+Shift+P 输入Install Package-->Codecs 二.安装gbk support 菜单:preferences ...

  8. spring 5.0.1.RELEASE官方任然不支持velocity(平台升级)

    官方说明: Dear Spring community, It is my pleasure to announce that Spring Framework 5.0.1 is available ...

  9. wince程序调用另外一个wince exe程序?

    记住:要释放句柄 清空内存(当前程序) 在虚拟机下测试如图: 在reyo.ini文件中配置另一wince执行程序的路径,如果不配置程序会报错: 如果配置的程序不存在报错: 没有问题就调用所在位置的wi ...

  10. Material Designer的低版本兼容实现(九)—— Float Button & Small Float Button

    5.0一个新特性就是出现了这么一个圆形的悬浮指示按钮,这个按钮可以用来体现一个全局的重要功能,比如添加账户什么的.这个按钮有两种大小,一种是正常的按钮大小,一种是小型的按钮.官方文档中介绍的是小心的按 ...