题解 P6835 【[Cnoi2020] 线形生物】
Daniel13265 · · 题解
前言
这道题实际上不需要复杂的数学推导,只需要利用期望的线性性就能轻松推出答案。
分析
设
期望具有线性性,于是对于任意
二者结合,可以得到
移项化简得
再次根据期望的线性性有
有明显初始状态
参考代码
#include <cstdio>
int read() {
static int ch, x;
while ((ch = getchar()) < 48) {}
x = ch ^ 48;
while ((ch = getchar()) >= 48) x = (((x << 2) + x) << 1) + (ch ^ 48);
return x;
}
#define MAXN 1000010
const int P = 998244353;
// e[x] 表示文中的 E_{1 \to x}。
int e[MAXN], head[MAXN], nxt[MAXN], to[MAXN];
int main() {
read();
const int n = read(), m = read();
for (int i = 1; i <= m; ++i) {
const int x = read(), y = read();
nxt[i] = head[x];
to[i] = y;
head[x] = i;
}
int ans = 0;
for (int i = 1; i <= n; ++i) {
e[i] = ans;
long long d = 0;
// 注意不能直接累加 ans,因为可能出现 x = y 的情况。
for (int j = head[i]; j; j = nxt[j]) d += ans - e[to[j]] + 1;
ans = (ans + 1 + d) % P;
}
printf("%d\n", ans + (ans >> 31 & P));
return 0;
}