洛谷 P6835 [Cnoi2020]线形生物 数学期望+递推
链接
根据期望定义得出相邻两点步数的期望,利用前缀和,期望的线性性
质进行优化即可,式子就不写了(不会用latex)注意,所得的
期望因取模,可能变成负值,加个模数即可(卡了我10分钟)
#include<cstring>
#include<string>
#include<cstdio>
#define ll long long
#define mod 998244353
using namespace std;
const int maxn=2000005;
inline ll read(){
ll ret=0;
ll f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')
f=-f;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
ret=ret*10+(ch^'0');
ch=getchar();
}
return ret*f;
}
ll id,n,m;
struct edge{
ll nex;
ll to;
}e[maxn];
ll head[maxn];
ll cnt;
void add(ll u,ll v){
cnt++;
e[cnt].nex=head[u];
e[cnt].to=v;
head[u]=cnt;
}
ll f[maxn];
ll u,v;
ll sum[maxn];
ll fz[maxn];
int main(){
// freopen("a.in","r",stdin);
id=read();
n=read();
// cin>>n>>m;
m=read();
for(int i=1;i<=m;i++){
// cin>>u>>v;
u=read();
v=read();
add(u,v);
fz[u]++;
}
for(int i=1;i<=n;i++){
f[i]=fz[i]+1;
for(int j=head[i];j;j=e[j].nex){
int y=e[j].to;
f[i]=((f[i]+(sum[i-1]-sum[y-1])%mod)%mod+mod);
}
sum[i]=(sum[i-1]+f[i])%mod;
}
cout<<sum[n]<<endl;
return 0;
}