BZOJ 1875

矩阵乘法加速递推。

如果不要求不能走同一条边,那么直接构造出矩阵快速幂即可,但是不走相同的道路,怎么办?

发现边数$m$也很小,我们直接把$2 * m$开成一个矩阵,相当于记录上一条边走过了编号为$j$的边的方案总数,这样子我们在构造转移矩阵的时候就可以不用计算往回走带来的贡献了。

时间复杂度$O(m^3log(MaxInt))$。

Code:

  1. #include <cstdio>
  2. #include <cstring>
  3. using namespace std;
  4. typedef long long ll;
  5.  
  6. const int N = ;
  7. const int M = ;
  8. const int P = ;
  9.  
  10. int n, m, tot = , from[M], to[M];
  11.  
  12. template <typename T>
  13. inline void read(T &X) {
  14. X = ; char ch = ; T op = ;
  15. for(; ch > '' || ch < ''; ch = getchar())
  16. if(ch == '-') op = -;
  17. for(; ch >= '' && ch <= ''; ch = getchar())
  18. X = (X << ) + (X << ) + ch - ;
  19. X *= op;
  20. }
  21.  
  22. template <typename T>
  23. inline void inc(T &x, T y) {
  24. x += y;
  25. if(x >= P) x -= P;
  26. }
  27.  
  28. struct Matrix {
  29. int len, wid, s[M][M];
  30.  
  31. inline void init() {
  32. len = wid = ;
  33. memset(s, 0LL, sizeof(s));
  34. }
  35.  
  36. friend Matrix operator * (const Matrix &x, const Matrix &y) {
  37. Matrix res; res.init();
  38. res.len = x.len, res.wid = y.wid;
  39. for(int k = ; k < x.wid; k++)
  40. for(int i = ; i < x.len; i++)
  41. for(int j = ; j < y.wid; j++)
  42. inc(res.s[i][j], (int) (1LL * x.s[i][k] * y.s[k][j] % P));
  43. return res;
  44. }
  45.  
  46. } f, tra;
  47.  
  48. inline Matrix fpow(Matrix x, int y) {
  49. Matrix res; res.init();
  50. res.len = x.len, res.wid = x.wid;
  51. for(int i = ; i < res.len; i++) res.s[i][i] = ;
  52.  
  53. for(; y > ; y >>= ) {
  54. if(y & ) res = res * x;
  55. x = x * x;
  56. }
  57.  
  58. return res;
  59. }
  60.  
  61. inline int opp(int now) {
  62. return (now & ) ? now + : now - ;
  63. }
  64.  
  65. int main() {
  66. int tim, st, ed;
  67. read(n), read(m), read(tim), read(st), read(ed);
  68. for(int x, y, i = ; i <= m; i++) {
  69. read(x), read(y);
  70. ++tot, from[tot] = x, to[tot] = y;
  71. ++tot, from[tot] = y, to[tot] = x;
  72. }
  73.  
  74. f.init();f.len = , f.wid = * m;
  75. for(int i = ; i <= tot; i++)
  76. if(from[i] == st) ++f.s[][i - ];
  77. tra.init(); tra.len = tra.wid = * m;
  78. for(int i = ; i <= tot; i++)
  79. for(int j = ; j <= tot; j++) {
  80. if(j == i || j == opp(i)) continue;
  81. if(to[i] == from[j])
  82. ++tra.s[i - ][j - ];
  83. }
  84.  
  85. tra = fpow(tra, tim - );
  86. f = f * tra;
  87.  
  88. int ans = ;
  89. for(int i = ; i <= tot; i++)
  90. if(to[i] == ed) inc(ans, f.s[][i - ]);
  91.  
  92. printf("%d\n", ans);
  93. return ;
  94. }

Luogu 2151 [SDOI2009]HH去散步的更多相关文章

  1. Luogu P2151 [SDOI2009]HH去散步 矩乘加速DP

    思路:矩乘优化DP 提交:3次(用了一个奇怪的东西导致常数过大) 题解: 如果可以走完正向边后又走反向边那就显然了,但是不能走,所以我们要将正反向边分别编号,区分正反向边. 所以这道题的矩阵是以边的编 ...

  2. 洛谷2151[SDOI2009]HH去散步(dp+矩阵乘法优化)

    一道良好的矩阵乘法优化\(dp\)的题. 首先,一个比较\(naive\)的想法. 我们定义\(dp[i][j]\)表示已经走了\(i\)步,当前在点\(j\)的方案数. 由于题目中限制了不能立即走之 ...

  3. bzoj1875: [SDOI2009]HH去散步

    终于A了...早上按自己以前的写法一直WA.下午换了一种写法就A了qwq #include<cstdio> #include<cstring> #include<iost ...

  4. BZOJ 1875: [SDOI2009]HH去散步( dp + 矩阵快速幂 )

    把双向边拆成2条单向边, 用边来转移...然后矩阵乘法+快速幂优化 ------------------------------------------------------------------ ...

  5. BZOJ_1875_[SDOI2009]HH去散步_矩阵乘法

    BZOJ_1875_[SDOI2009]HH去散步_矩阵乘法 Description HH有个一成不变的习惯,喜欢饭后百步走.所谓百步走,就是散步,就是在一定的时间 内,走过一定的距离. 但 是同时H ...

  6. bzoj 1875: [SDOI2009]HH去散步 -- 矩阵乘法

    1875: [SDOI2009]HH去散步 Time Limit: 20 Sec  Memory Limit: 64 MB Description HH有个一成不变的习惯,喜欢饭后百步走.所谓百步走, ...

  7. AC日记——[SDOI2009]HH去散步 洛谷 P2151

    [SDOI2009]HH去散步 思路: 矩阵快速幂递推(类似弗洛伊德): 给大佬跪烂-- 代码: #include <bits/stdc++.h> using namespace std; ...

  8. 「 洛谷 」P2151 [SDOI2009]HH去散步

    小兔的话 欢迎大家在评论区留言哦~ HH去散步 题目限制 内存限制:125.00MB 时间限制:1.00s 标准输入 标准输出 题目知识点 动态规划 \(dp\) 矩阵 矩阵乘法 矩阵加速 矩阵快速幂 ...

  9. 洛谷P2151 [SDOI2009] HH去散步 [矩阵加速]

    题目传送门 HH去散步 题目描述 HH有个一成不变的习惯,喜欢饭后百步走.所谓百步走,就是散步,就是在一定的时间 内,走过一定的距离. 但是同时HH又是个喜欢变化的人,所以他不会立刻沿着刚刚走来的路走 ...

随机推荐

  1. 剑指offer--10.最小的K个数

    边界判断,坑了一下 ----------------------------------------------- 时间限制:1秒 空间限制:32768K 热度指数:375643 本题知识点: 数组 ...

  2. 从零开始的acm竞赛生涯

    经过了一段时间的训练,自己的成绩还是很不理想.回首过往,感觉自己还是练得太少,一直没有进入状态,缺乏硬怼出题的能力,思维也不够快,赛场上各种被卡题.可以说,我之前的训练有些仓促,还没有达到入门的水准, ...

  3. LeetCode 4 Keys Keyboard

    原题链接在这里:https://leetcode.com/problems/4-keys-keyboard/description/ 题目: Imagine you have a special ke ...

  4. LeetCode Construct String from Binary Tree

    原题链接在这里:https://leetcode.com/problems/construct-string-from-binary-tree/#/description 题目: You need t ...

  5. BZOJ3262:陌上花开

    浅谈离线分治算法:https://www.cnblogs.com/AKMer/p/10415556.html 题目传送门:https://lydsy.com/JudgeOnline/problem.p ...

  6. CentOS 7 安装Percona,Xtrabackup

    CentOS 7 安装Percona 5.7,Xtrabackup 简介 Percona Server为 MySQL 数据库服务器进行了改进,在功能和性能上较 MySQL 有着很显著的提升.该版本提升 ...

  7. CS231n笔记列表

    课程基础1:Numpy Tutorial 课程基础2:Scipy Matplotlib 1.1 图像分类和Nearest Neighbor分类器 1.2 k-Nearest Neighbor分类器 1 ...

  8. Java-API-Package:javax.http.servlet

    ylbtech-Java-API-Package:javax.http.servlet 1.返回顶部 1. Package javax.servlet.http This chapter descri ...

  9. 组装恢复rbd

    标签: ceph,ceph实验,rbd cluster相关环境: # cat /etc/redhat-release CentOS Linux release 7.3.1611 (Core) # ce ...

  10. 1 ignite核心特性

    1 Ignite是什么? Apache Ignite是一个以内存为中心的分布式数据库.缓存和处理平台,支持事务.分析以及流式负载,可以在PB级数据上享有内存级的性能. 2 Ignite是不是内存数据库 ...