分析
设(f[i])表示由点(i)走到点(i+1)的期望步数,
(dp[i])表示由点1走到点(i+1)的期望步数,
那么(dp)为(f)的前缀和,最后答案为(dp[n]),只用考虑怎么计算(f)
那么
[f[i]=frac{sum_{yin son}(dp[i-1]-dp[y-1])+f[i]}{deg[i]-1}+1
]
化简得到
[f[i]=sum_{yin son}(dp[i-1]-dp[y-1]+1)+1
]
其中(deg)表示(i)的度数,包括(i->i+1)
代码
#include <cstdio>
#include <cctype>
#include <algorithm>
#define rr register
using namespace std;
const int N=1000011,mod=998244353;
struct node{int y,next;}e[N];
int n,k,dp[N],f[N],as[N];
inline signed iut(){
rr int ans=0; rr char c=getchar();
while (!isdigit(c)) c=getchar();
while (isdigit(c)) ans=(ans<<3)+(ans<<1)+(c^48),c=getchar();
return ans;
}
inline signed mo(int x,int y){return x+y>=mod?x+y-mod:x+y;}
signed main(){
iut(),n=iut(),k=iut();
for (rr int i=1;i<=k;++i){
rr int x=iut(),y=iut();
e[i]=(node){y,as[x]},as[x]=i;
}
for (rr int i=1;i<=n;++i){
for (rr int j=as[i];j;j=e[j].next)
f[i]=mo(mo(f[i],dp[i-1]),mod-dp[e[j].y-1]+1);
dp[i]=mo(dp[i-1],f[i]=mo(f[i],1));
}
return !printf("%d",dp[n]);
}