当前位置:网站首页>[Luogu p2151] HH go for a walk (DP) (matrix multiplication)
[Luogu p2151] HH go for a walk (DP) (matrix multiplication)
2022-07-18 13:38:00 【SSL_ TJH】
HH Go for a walk
Topic link :luogu P2151
The main idea of the topic
Here's an undirected graph , Then give you the number of paths from the beginning to the end , Then every time you walk, you can't go back on that side , Then ask how many walking plans you have .
Ideas
First, let's talk about the normal practice .( Happy )
If you can go back, it's simple , But you can't look back , Consider distinguishing it .
Or you can see the edge as directed , Then see it as a point , The two edges cannot be connected , You can run matrix multiplication normally .
Here is a kind of follow m m m Irrelevant practice .
set up f x , y , z f_{x,y,z} fx,y,z For the last two points are x , y x,y x,y, go z z z Step scheme .( In this way, you can prevent turning back )
f x , y , z = g x , y ∑ i = 1 n f i , x , z − 1 − f y , x , z − 1 f_{x,y,z}=g_{x,y}\sum\limits_{i=1}^nf_{i,x,z-1}-f_{y,x,z-1} fx,y,z=gx,yi=1∑nfi,x,z−1−fy,x,z−1
But the side length of the matrix is n 2 n^2 n2 no way ( Or not good enough , Not very good somewhere )
Consider what your answer requires ( ∑ i = 1 n f i , T , L \sum\limits_{i=1}^nf_{i,T,L} i=1∑nfi,T,L)
g 1 x , z = ∑ i = 1 n f x , i , z = ∑ i = 1 n ( g x , i ∑ j = 1 n f j , x , z − 1 − f i , x , z − 1 ) = ∑ i = 1 n g x , i g 2 x , z − 1 − g 2 x , z − 1 = g 2 x , z − 1 ( ∑ i = 1 n g x , i − 1 ) g1_{x,z}=\sum\limits_{i=1}^nf_{x,i,z}=\sum\limits_{i=1}^n(g_{x,i}\sum\limits_{j=1}^nf_{j,x,z-1}-f_{i,x,z-1})=\sum\limits_{i=1}^ng_{x,i}g2_{x,z-1}-g2_{x,z-1}=g2_{x,z-1}(\sum\limits_{i=1}^ng_{x,i}-1) g1x,z=i=1∑nfx,i,z=i=1∑n(gx,ij=1∑nfj,x,z−1−fi,x,z−1)=i=1∑ngx,ig2x,z−1−g2x,z−1=g2x,z−1(i=1∑ngx,i−1)
g 2 y , z = ∑ i = 1 n f i , y , z = ∑ i = 1 n ( g i , y ∑ j = 1 n f j , i , z − 1 − f y , i , z − 1 ) = ∑ i = 1 n g x , i g 2 i , z − 1 − g 1 y , z − 1 g2_{y,z}=\sum\limits_{i=1}^nf_{i,y,z}=\sum\limits_{i=1}^n(g_{i,y}\sum\limits_{j=1}^nf_{j,i,z-1}-f_{y,i,z-1})=\sum\limits_{i=1}^ng_{x,i}g2_{i,z-1}-g1_{y,z-1} g2y,z=i=1∑nfi,y,z=i=1∑n(gi,yj=1∑nfj,i,z−1−fy,i,z−1)=i=1∑ngx,ig2i,z−1−g1y,z−1
Then make these two things , You find that you can only keep these two information for matrix multiplication , Then the side length of the matrix becomes 2 n 2n 2n You can pass !
Then if multiple groups ask different L , T L,T L,T, Then change the fast power into Multiplication , It's the same thing .
Code
#include<cstdio>
#include<cstring>
#define ll long long
//#define mo 998244353
#define mo 45989
using namespace std;
const int N = 105;
int n, m, Q, S, T, g[N][N];
int st[N << 1], jump[60][N << 1][N << 1], ans[N << 1], tmp[N << 1];
ll L;
int add(int x, int y) {
return x + y >= mo ? x + y - mo : x + y;}
int mul(int x, int y) {
return 1ll * x * y % mo;}
void Init() {
for (int i = 1; i <= n; i++) st[S] = add(st[S], g[S][i]);
for (int i = 1; i <= n; i++) st[n + i] = add(st[n + i], g[S][i]);
for (int i = 1; i <= n; i++) {
//g1
int tmp = mo - 1; for (int j = 1; j <= n; j++) tmp = add(tmp, g[i][j]);
jump[0][n + i][i] = tmp;
}
for (int i = 1; i <= n; i++) {
//g2
for (int j = 1; j <= n; j++) jump[0][n + j][n + i] = add(jump[0][n + j][n + i], g[i][j]);
jump[0][i][n + i] = add(jump[0][i][n + i], mo - 1);
}
for (int z = 1; z < 60; z++) {
for (int k = 1; k <= (n << 1); k++)
for (int i = 1; i <= (n << 1); i++)
for (int j = 1; j <= (n << 1); j++)
jump[z][i][j] = add(jump[z][i][j], mul(jump[z - 1][i][k], jump[z - 1][k][j]));
}
}
int main() {
// freopen("ancient.in", "r", stdin);
// freopen("ancient.out", "w", stdout);
// scanf("%d %d %d %d", &n, &m, &Q, &S);
scanf("%d %d %d %d %d", &n, &m, &L, &S, &T); S++; T++;
for (int i = 1; i <= m; i++) {
int x, y; scanf("%d %d", &x, &y);
x++; y++;
g[x][y]++; g[y][x]++;
}
Init();
// while (Q--) {
// scanf("%d %lld", &T, &L);
// if (!L) {printf("%d\n", (S == T) ? 1 : 0); continue;}
if (!L) {
printf("%d\n", (S == T) ? 1 : 0); return 0;}
L--; memcpy(ans, st, sizeof(ans));
for (int z = 59; z >= 0; z--) if ((L >> z) & 1) {
for (int k = 1; k <= (n << 1); k++)
for (int j = 1; j <= (n << 1); j++)
tmp[j] = add(tmp[j], mul(ans[k], jump[z][k][j]));
memcpy(ans, tmp, sizeof(ans)); memset(tmp, 0, sizeof(tmp));
}
printf("%d\n", ans[n + T]);
// }
return 0;
}
边栏推荐
- 为什么 Qt Creator 的编译如此之慢?
- 基于SSM的企业OA系统,高质量论文范例,附送源码、数据库脚本,项目导入与运行教程,论文撰写教程
- 单细胞论文记录(part15)--Integrated analysis of multimodal single-cell data
- 六、Jmeter定时器
- Get started quickly, Jupiter notebook
- Sword finger offer 11 Rotate the minimum number of the array
- 6、 JMeter timer
- Work method record
- 【软件测试】测试黑话,十年测试老员工给你的忠告
- 潮玩华猫来袭丨继周杰伦联名款后 ,方文山携上链购再推“华流”顶流联名款公仔数藏
猜你喜欢

Sword finger offer 10- ii Frog jumping on steps

DPR-34、AC220V双位置继电器

Quick use of jdbctemplate

Time consuming evaluation of image pixel values accessed by opencv, emgucv and opencvsharp pointers (with source code)

蓝牙技术|首批蓝牙LE Audio兼容设备年内上市,蓝牙耳机续航将突飞猛进

Installing the g2opy frame

Introduction of oscilloscope bandwidth

众昂矿业:萤石保障新能源工业氟元素供应

Practice Guide for peanut shell inner net penetration

动态炫酷的404页面源码
随机推荐
7.15模拟赛总结
Time consuming evaluation of image pixel values accessed by opencv, emgucv and opencvsharp pointers (with source code)
DSP-Core类库的.NET版本降级
How can the computer recover deleted files? How can I recover deleted data
6、 JMeter timer
基于SSM的企业OA系统,高质量论文范例,附送源码、数据库脚本,项目导入与运行教程,论文撰写教程
2022-07-信息论-吴军
DLS-12B/DC220V双位置继电器
How to generate non repeated random numbers in Excel, multi method + principle
MySQL 窗函数 流动平均数 running average
视频处理:视频抽样
山东省中小企业数字化转型论坛成功举办,九州云赋能中小企业数智升级
28K monthly salary for software testing interview questions for large factories (you can take away the test paper with the permission of the interviewer)
Sword finger offer 32 - I. print binary tree from top to bottom, breadth first search (queue)
Tools to measure the gap between two distributions: cross entropy and KL divergence
dpdk flow filter总结(flow director/ rte_flow)
[notes] cryptography from introduction to earth | des
【软件测试】08 -- 黑盒测试方法(边界值分析法、因果图与决策表法)
ORA-19625异常处理记录
DzzOffice_flowplayer播放器更改