P6835 [Cnoi2020]线形生物 题解
通过这道题可以看出来 pz 根本不会期望
考虑期望线性性质,设 \(E_{x\to y}\) 表示从 \(x\) 走到 \(y\) 的期望步数,那么有 \(E_{1\to n+1}=\sum_{i=1}^nE_{i\to i+1}\),因此考虑计算 \(E_{i\to i+1}\),下记 \(f_i=E_{i\to i+1}\)。
设 \(d_i\) 表示 \(i\) 的返祖边边数,\(E\) 表示返祖边边集,\(sum_i=\sum_{p=1}^{i}f_p\)则有如下一系列转化:
\[E_{i\to i+1}=\dfrac1{d_x+1}\times1+\sum_{i\to u\in E}E_{u\to i+1}
\]
\[E_{i\to i+1}=\dfrac1{d_x+1}\times1+\sum_{i\to u\in E}\sum_{p=u}^iE_{p\to p+1}
\]
\[f_i=\dfrac1{d_x+1}+\sum_{i\to u\in E}(sum_i-sum_{u-1})
\]
下面这步将和式的 \(sum_i\) 提出,化简得到:
\[f_i=(d_x+1)+\sum_{i\to u\in E}(sum_{i-1}-sum_{u-1})
\]
维护前缀和即可。
Code:
/*
========= Plozia =========
Author:Plozia
Problem:P6835 [Cnoi2020]线形生物
Date:2022/9/26
========= Plozia =========
*/
#include <bits/stdc++.h>
typedef long long LL;
using std::vector;
const int MAXN = 1e6 + 5;
const LL P = 998244353;
int n, m;
vector <int> Edge[MAXN];
LL f[MAXN], sum[MAXN];
int Read()
{
int sum = 0, fh = 1; char ch = getchar();
for (; ch < '0' || ch > '9'; ch = getchar()) fh -= (ch == '-') << 1;
for (; ch >= '0' && ch <= '9'; ch = getchar()) sum = (sum << 3) + (sum << 1) + (ch ^ 48);
return sum * fh;
}
int main()
{
Read(); n = Read(), m = Read();
for (int i = 1; i <= m; ++i) { int u = Read(), v = Read(); Edge[u].push_back(v); }
for (int i = 1; i <= n; ++i)
{
f[i] = Edge[i].size() + 1;
for (int j = 0; j < Edge[i].size(); ++j)
f[i] = ((f[i] + sum[i - 1] - sum[Edge[i][j] - 1]) % P + P) % P;
sum[i] = (sum[i - 1] + f[i]) % P;
}
printf("%lld\n", sum[n]); return 0;
}