Luogu 2151 [SDOI2009]HH去散步
BZOJ 1875
矩阵乘法加速递推。
如果不要求不能走同一条边,那么直接构造出矩阵快速幂即可,但是不走相同的道路,怎么办?
发现边数$m$也很小,我们直接把$2 * m$开成一个矩阵,相当于记录上一条边走过了编号为$j$的边的方案总数,这样子我们在构造转移矩阵的时候就可以不用计算往回走带来的贡献了。
时间复杂度$O(m^3log(MaxInt))$。
Code:
- #include <cstdio>
- #include <cstring>
- using namespace std;
- typedef long long ll;
- const int N = ;
- const int M = ;
- const int P = ;
- int n, m, tot = , from[M], to[M];
- template <typename T>
- inline void read(T &X) {
- X = ; char ch = ; T op = ;
- for(; ch > '' || ch < ''; ch = getchar())
- if(ch == '-') op = -;
- for(; ch >= '' && ch <= ''; ch = getchar())
- X = (X << ) + (X << ) + ch - ;
- X *= op;
- }
- template <typename T>
- inline void inc(T &x, T y) {
- x += y;
- if(x >= P) x -= P;
- }
- struct Matrix {
- int len, wid, s[M][M];
- inline void init() {
- len = wid = ;
- memset(s, 0LL, sizeof(s));
- }
- friend Matrix operator * (const Matrix &x, const Matrix &y) {
- Matrix res; res.init();
- res.len = x.len, res.wid = y.wid;
- for(int k = ; k < x.wid; k++)
- for(int i = ; i < x.len; i++)
- for(int j = ; j < y.wid; j++)
- inc(res.s[i][j], (int) (1LL * x.s[i][k] * y.s[k][j] % P));
- return res;
- }
- } f, tra;
- inline Matrix fpow(Matrix x, int y) {
- Matrix res; res.init();
- res.len = x.len, res.wid = x.wid;
- for(int i = ; i < res.len; i++) res.s[i][i] = ;
- for(; y > ; y >>= ) {
- if(y & ) res = res * x;
- x = x * x;
- }
- return res;
- }
- inline int opp(int now) {
- return (now & ) ? now + : now - ;
- }
- int main() {
- int tim, st, ed;
- read(n), read(m), read(tim), read(st), read(ed);
- for(int x, y, i = ; i <= m; i++) {
- read(x), read(y);
- ++tot, from[tot] = x, to[tot] = y;
- ++tot, from[tot] = y, to[tot] = x;
- }
- f.init();f.len = , f.wid = * m;
- for(int i = ; i <= tot; i++)
- if(from[i] == st) ++f.s[][i - ];
- tra.init(); tra.len = tra.wid = * m;
- for(int i = ; i <= tot; i++)
- for(int j = ; j <= tot; j++) {
- if(j == i || j == opp(i)) continue;
- if(to[i] == from[j])
- ++tra.s[i - ][j - ];
- }
- tra = fpow(tra, tim - );
- f = f * tra;
- int ans = ;
- for(int i = ; i <= tot; i++)
- if(to[i] == ed) inc(ans, f.s[][i - ]);
- printf("%d\n", ans);
- return ;
- }
Luogu 2151 [SDOI2009]HH去散步的更多相关文章
- Luogu P2151 [SDOI2009]HH去散步 矩乘加速DP
思路:矩乘优化DP 提交:3次(用了一个奇怪的东西导致常数过大) 题解: 如果可以走完正向边后又走反向边那就显然了,但是不能走,所以我们要将正反向边分别编号,区分正反向边. 所以这道题的矩阵是以边的编 ...
- 洛谷2151[SDOI2009]HH去散步(dp+矩阵乘法优化)
一道良好的矩阵乘法优化\(dp\)的题. 首先,一个比较\(naive\)的想法. 我们定义\(dp[i][j]\)表示已经走了\(i\)步,当前在点\(j\)的方案数. 由于题目中限制了不能立即走之 ...
- bzoj1875: [SDOI2009]HH去散步
终于A了...早上按自己以前的写法一直WA.下午换了一种写法就A了qwq #include<cstdio> #include<cstring> #include<iost ...
- BZOJ 1875: [SDOI2009]HH去散步( dp + 矩阵快速幂 )
把双向边拆成2条单向边, 用边来转移...然后矩阵乘法+快速幂优化 ------------------------------------------------------------------ ...
- BZOJ_1875_[SDOI2009]HH去散步_矩阵乘法
BZOJ_1875_[SDOI2009]HH去散步_矩阵乘法 Description HH有个一成不变的习惯,喜欢饭后百步走.所谓百步走,就是散步,就是在一定的时间 内,走过一定的距离. 但 是同时H ...
- bzoj 1875: [SDOI2009]HH去散步 -- 矩阵乘法
1875: [SDOI2009]HH去散步 Time Limit: 20 Sec Memory Limit: 64 MB Description HH有个一成不变的习惯,喜欢饭后百步走.所谓百步走, ...
- AC日记——[SDOI2009]HH去散步 洛谷 P2151
[SDOI2009]HH去散步 思路: 矩阵快速幂递推(类似弗洛伊德): 给大佬跪烂-- 代码: #include <bits/stdc++.h> using namespace std; ...
- 「 洛谷 」P2151 [SDOI2009]HH去散步
小兔的话 欢迎大家在评论区留言哦~ HH去散步 题目限制 内存限制:125.00MB 时间限制:1.00s 标准输入 标准输出 题目知识点 动态规划 \(dp\) 矩阵 矩阵乘法 矩阵加速 矩阵快速幂 ...
- 洛谷P2151 [SDOI2009] HH去散步 [矩阵加速]
题目传送门 HH去散步 题目描述 HH有个一成不变的习惯,喜欢饭后百步走.所谓百步走,就是散步,就是在一定的时间 内,走过一定的距离. 但是同时HH又是个喜欢变化的人,所以他不会立刻沿着刚刚走来的路走 ...
随机推荐
- 剑指offer--10.最小的K个数
边界判断,坑了一下 ----------------------------------------------- 时间限制:1秒 空间限制:32768K 热度指数:375643 本题知识点: 数组 ...
- 从零开始的acm竞赛生涯
经过了一段时间的训练,自己的成绩还是很不理想.回首过往,感觉自己还是练得太少,一直没有进入状态,缺乏硬怼出题的能力,思维也不够快,赛场上各种被卡题.可以说,我之前的训练有些仓促,还没有达到入门的水准, ...
- LeetCode 4 Keys Keyboard
原题链接在这里:https://leetcode.com/problems/4-keys-keyboard/description/ 题目: Imagine you have a special ke ...
- LeetCode Construct String from Binary Tree
原题链接在这里:https://leetcode.com/problems/construct-string-from-binary-tree/#/description 题目: You need t ...
- BZOJ3262:陌上花开
浅谈离线分治算法:https://www.cnblogs.com/AKMer/p/10415556.html 题目传送门:https://lydsy.com/JudgeOnline/problem.p ...
- CentOS 7 安装Percona,Xtrabackup
CentOS 7 安装Percona 5.7,Xtrabackup 简介 Percona Server为 MySQL 数据库服务器进行了改进,在功能和性能上较 MySQL 有着很显著的提升.该版本提升 ...
- CS231n笔记列表
课程基础1:Numpy Tutorial 课程基础2:Scipy Matplotlib 1.1 图像分类和Nearest Neighbor分类器 1.2 k-Nearest Neighbor分类器 1 ...
- Java-API-Package:javax.http.servlet
ylbtech-Java-API-Package:javax.http.servlet 1.返回顶部 1. Package javax.servlet.http This chapter descri ...
- 组装恢复rbd
标签: ceph,ceph实验,rbd cluster相关环境: # cat /etc/redhat-release CentOS Linux release 7.3.1611 (Core) # ce ...
- 1 ignite核心特性
1 Ignite是什么? Apache Ignite是一个以内存为中心的分布式数据库.缓存和处理平台,支持事务.分析以及流式负载,可以在PB级数据上享有内存级的性能. 2 Ignite是不是内存数据库 ...