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去散步的更多相关文章

  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. 搞定thrift双向消息

    thrift作为脱胎于facebook的rpc框架,各方面都非常优秀.清晰的分层设计,多语言的支持,以及不输protocolbuffer的效率(compact下优于protocolbuffer),都让 ...

  2. SaaS模式实现架构

    SaaS模式实现架构 https://blog.csdn.net/xwq911/article/details/50630266 1. 数据库层: 数据库这一层的设计模式是很清晰的,无外乎只有3种方案 ...

  3. 19年博客flag

    目录 为什么没有年终总结 为什么今天更新了 19年博客flag 个人博客链接:我在马路边 https://hhongwen.cn 更好的阅读体验点击查看原文:19年博客flag 原创博客,转载请注明出 ...

  4. 【LeetCode】006. ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  5. Makefile生成

    安装:sudo yum install automake使用: 1 运行autoscan生成两个文件:autoscan.log和configure.scan.将configure.scan重命名为co ...

  6. 杂项-Grunt:grunt build 打包和常见错误

    ylbtech-杂项-Grunt:grunt build 打包和常见错误 1. 安装.打包返回顶部 1. npm WARN deprecated coffee-script@: CoffeeScrip ...

  7. Web项目安全相关博客日志大集合(仅供学习及参考)

    强制使用HTTPS --- Tomcat篇 ---通过在tomcat/conf/web.xml中进行配置,从而将http自动转为https.(即强制HSTS)http://blog.csdn.net/ ...

  8. Spring学习八

    1: Tomcat容器四个等级? Container, Engine,  Servlet容器, Context 真正管理Servlet的容器是Context容器:一个context对应一个web工程. ...

  9. iphone配置实用工具iPhone Configuration Utility

    下载地址 http://support.apple.com/kb/DL1466 安装完毕后,在设备->控制台,可以很方便看到报错信息

  10. vBulletin 5.x 版本通杀远程代码执行漏洞复现

    漏洞介绍 vBulletin中存在一个文件包含问题,可使恶意访问者包含来自 vBulletin 服务器的文件并且执行任意 PHP 代码.未经验证的恶意访问者可通过向index.php发出包含 rout ...