题解 P6835 [Cnoi2020]线形生物
利用期望的线性性质,可以知道: \(E_{1\to n+1}=\sum_\limits{i=1}^n E_{i \to i+1}\) 。
那么设 \(f(x)=E_{x\to x+1}\) ,则:
\[f(x)=1+\dfrac{1}{d_x+1}\sum_{(x,y)\in E}\sum_{i=y}^x f(i)
\]
设 \(s_x=\sum_\limits{i=1}^x f(x)\) ,则:
\[f(x)=1+\dfrac{1}{d_x+1}\sum_{(x,y)\in E}s_x-s_{y-1} \\
f(x)=1+\dfrac{1}{d_x+1}(s_xd_x-\sum_{(x,y)\in E}s_{y-1}) \\
d_xf(x)+f(x)=d_x+1+s_xd_x-\sum_{(x,y)\in E} s_{y-1} \\
f(x)=d_x+1+s_{x-1}d_x-\sum_{(x,y)\in R} s_{y-1}
\]
动态维护前缀和与 \(f\) 的值即可,代码如下:
#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <iostream>
typedef long long LL;
using namespace std;
inline int read() {
int num = 0 ,f = 1; char c = getchar();
while (!isdigit(c)) f = c == '-' ? -1 : f ,c = getchar();
while (isdigit(c)) num = (num << 1) + (num << 3) + (c ^ 48) ,c = getchar();
return num * f;
}
const int N = 1e6 + 5 ,M = 1e6 + 5 ,mod = 998244353;
int s[N] ,f[N];
int n ,d[N] ,m;
struct Edge {
int to ,next;
Edge (int to = 0 ,int next = 0) : to(to) ,next(next) {}
}G[M]; int head[N] ,idx;
inline void add(int u ,int v) {
G[++idx] = Edge(v ,head[u]); head[u] = idx;
}
signed main() {
{int useless = read();}
n = read() ,m = read();
for (int i = 1; i <= m; i++) {
int u = read() ,v = read();
add(u ,v);
d[u]++;
}
for (int i = 1; i <= n; i++) {
f[i] = (d[i] + 1 + (LL)s[i - 1] * d[i] % mod) % mod;
for (int j = head[i]; j ; j = G[j].next) {
int v = G[j].to;
f[i] = ((f[i] - s[v - 1]) % mod + mod) % mod;
}
s[i] = (s[i - 1] + f[i]) % mod;
}
printf("%d\n" ,s[n]);
return 0;
}