链接

题解链接:http://www.cygmasot.com/index.php/2015/08/20/hdu_5411/

给定n个点 常数m

以下n行第i行第一个数字表示i点的出边数。后面给出这些出边。

问:图里存在多少条路径使得路径长度<=m。路径上的点能够反复。

思路:

首先能得到一个m*n*n的dp。dp[i][j]表示路径长度为i 路径的结尾为j的路径个数 。

答案就是sigma(dp[i][j]) for every i from 1 to m, j from 1 to n;

我们先计算 路径长度恰好为 i 的方法数。

用矩阵高速幂,会发现是

当中B矩阵是一个n*n的矩阵。也就是输入的邻接矩阵。

A是一个n行1列的矩阵 A[i][1]表示长度为1且以i结尾的路径个数,所以A矩阵是全1矩阵。

相乘得到的n*1 的矩阵求和就是路径长度恰好为i的条数。

那么<=m的路径就是:

把A提出来,里面就是一个关于B的矩阵等比数列。

B的求发主要是二分。详见POJ3233

模板不大好,交G++能过

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
#include<ctime>  using namespace std;
template <class T>
inline bool rd(T &ret) {char c; int sgn;if (c = getchar(), c == EOF) return 0;while (c != '-' && (c<'0' || c>'9')) c = getchar();sgn = (c == '-') ? -1 : 1;ret = (c == '-') ? 0 : (c - '0');while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');ret *= sgn;return 1;
}
template <class T>
inline void pt(T x) {if (x <0) {putchar('-');x = -x;}if (x>9) pt(x / 10);putchar(x % 10 + '0');
}
const int mod = 2015;
const int N = 51;struct Matrix
{int m[N][N];
}G[2000];
int top;
Matrix I;
int n, k;
const int M = 2015;
Matrix add(Matrix a, Matrix b)
{Matrix &c = G[top++];for (int i = 0; i<n; i++){for (int j = 0; j<n; j++){c.m[i][j] = a.m[i][j] + b.m[i][j];c.m[i][j] %= M;}}top--;return c;
}Matrix multi(Matrix a, Matrix b)
{Matrix &c = G[top++];for (int i = 0; i<n; i++){for (int j = 0; j<n; j++){c.m[i][j] = 0;for (int k = 0; k<n; k++)c.m[i][j] += a.m[i][k] * b.m[k][j];c.m[i][j] %= M;}}top--;return c;
}Matrix power(Matrix A, int n)
{Matrix &ans = G[top++], &p = G[top++];ans = I; p = A;while (n){if (n & 1){ans = multi(ans, p);n--;}n >>= 1;p = multi(p, p);}top -= 2;return ans;
}Matrix sum(Matrix A, int k)
{if (k == 1) return A;Matrix &t = G[top++];t = sum(A, k / 2);if (k & 1){Matrix &cur = G[top++];cur = power(A, k / 2 + 1);t = add(t, multi(t, cur));t = add(t, cur);top--;}else{Matrix &cur = G[top++];cur = power(A, k / 2);t = add(t, multi(t, cur));top--;}top--;return t;
}int m;
void add(int &x, int y){x += y;if (x >= mod)x -= mod;
}
int B[N][N];
int main(){memset(I.m, 0, sizeof I.m);for (int i = 0; i < N; i++)I.m[i][i] = 1;int T; rd(T);while (T--){rd(n); rd(m);Matrix A;top = 0;memset(A.m, 0, sizeof A.m);for (int i = 1; i <= n; i++) {int tmp; rd(tmp); while (tmp--) { int u; rd(u); A.m[i-1][u-1] = 1; }}if (m == 0) { puts("1"); continue; }if (m == 1){ pt(n + 1); puts(""); continue; }Matrix ans = sum(A, m-1);for (int i = 0; i<n; i++)for (int j = 0; j<n; j++)B[i][j] = ans.m[i][j];for (int i = 0; i < n; i++)B[i][i] ++;int hehe = 0;for (int i = 0; i < n; i++){for (int j = 0; j < n; j++)add(hehe, B[i][j]);}pt(1 + hehe); puts("");}return 0;
}
/*
99
1 10
1 13 100000
3 1 2 3
3 1 2 3
3 1 2 35 3
5 1 2 3 4 5
4 2 3 4 5
3 1 3 5
5 1 2 3 4 5
3 1 2 3*/

转载于:https://www.cnblogs.com/wzjhoutai/p/6936138.html

hdu 5411 CRB and Puzzle 矩阵高速幂相关推荐

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

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

  2. HDU - 5411 CRB and Puzzle 矩阵快速幂

    HDU - 5411 考虑直接dp会T, 用矩阵优化一下就好了. #include<bits/stdc++.h> #define LL long long #define LD long ...

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

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

  4. hdu 5411 CRB and Puzzle(矩阵快速幂)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5411 解题思路: 题目大意: 给定n个点 常数m 下面n行第i行第一个数字表示i点的出边数,后面给出 ...

  5. 【矩阵快速幂】 HDU 5411 CRB and Puzzle 等比

    点击打开链接 题意显然是 求 A+A^2+A^3+....+A^m 这就是经典题目 矩阵乘法十种经典题目 递归解决 后半部分提取 A^(m/2) 该题再特判下 m==1的时候 #include < ...

  6. hdu 5411 CRB and Puzzle

    Sample Input 1 3 2 1 2 1 3 0 Sample Output 6 Hint possible patterns are ∅, 1, 2, 3, 1→2, 2→3 解释样例: 第 ...

  7. hdu 3221 Brute-force Algorithm(高速幂取模,矩阵高速幂求fib)

    http://acm.hdu.edu.cn/showproblem.php?pid=3221 一晚上搞出来这么一道题..Mark. 给出这么一个程序.问funny函数调用了多少次. 我们定义数组为所求 ...

  8. HDOJ How many ways?? 2157【矩阵高速幂】

    How many ways? ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations (矩阵高速幂)

    题目地址:http://codeforces.com/contest/551/problem/D 分析下公式能够知道,相当于每一位上放0或者1使得最后成为0或者1.假设最后是0的话,那么全部相邻位一定 ...

最新文章

  1. 巧用find命令清除系统垃圾
  2. 搭建Python+Django开发环境
  3. predicate java_java代码之美(13)--- Predicate详解
  4. php月历,PHP生成月历代码
  5. PCI总线原理(二)
  6. Linux驱动(1)--关于驱动的概述
  7. 七年前的电脑可以用python_1年前的小五都用 Python 来做什么?
  8. 大学c语言第三章作业,c语言程序设计一章部分和第三章习题答案.doc
  9. HTML常用的标签总结
  10. [工具:iperf吞吐率测试工具 ]安装以及使用
  11. #笔记#圣思园 JavaSE 第29讲——String类源代码深析
  12. 【雅思大作文考官范文】——第十七篇: 'economic progress' essay
  13. 自然数e的故事(转)
  14. Flex使用ribbit.com的服务给手机发送短信SMS
  15. Mapbox相机动画整理(1)示例解析
  16. “学习金字塔”理论的一个应用
  17. 一个Dom绑定多个事件
  18. .设计一个Student类,该类中包括学生的姓名和成绩。创建Student类的5个对象,如下所示: 姓名 成绩 刘德华 85 张学友 100 刘杰 65 章子怡 58 周迅 76 将以上5个对象
  19. 【转】WebRTC多人音视频解决方案
  20. 视频流编码格式(四字符码)对照表

热门文章

  1. 利用python分析电商_Python实现爬取并分析电商评论
  2. Qt学习之路(10): Meta-Object系统
  3. Jetty入门(1-1)Jetty入门教程
  4. 11月13日上午ajax返回数据类型为JSON数据的处理
  5. pg_restore使用
  6. HttpClient4.5.2 使用cookie保持会话——连接池的实现结束篇(4)
  7. 在AX4.0中使用C#脚本的实现
  8. Quartz.Net 2.0 bate1 使用
  9. 新浪微博***事件分析
  10. ORACLE LATERAL-SQL-INJECTION 个人见解