• [Cnoi2020]线性生物


    期望入门题。但是我不会做。

    考虑设\(E_{x\to{x+1}}\)\(x\)\(x+1\)点的期望步数。

    \(ans = \sum_{i = 0}^{n} E_{x\to{x+1}}\)

    \(E_{y\to{x+1}} = \sum_{i = y}^{x}E_{i\to{i + 1}}\)

    \(E_{x\to{x+1}} = \frac{1}{son + 1} + \frac{1}{son + 1}\sum_{(x,y)\ in\ {Son}}(E_{y\to{x+1}} + 1)\)

    \(E_{x\to{x+1}} = f_x\),\(sum_x = \sum_i^xf_i\)

    \(f_x = (son + 1) + \sum_{(x,y)\ in\ Son}sum_{x-1} - sum_{y - 1}\)

    // Problem: P6835 [Cnoi2020]线形生物
    // Contest: Luogu
    // URL: https://www.luogu.com.cn/problem/P6835
    // Memory Limit: 128 MB
    // Time Limit: 1000 ms
    // 
    // Powered by CP Editor (https://cpeditor.org)
    
    #include<iostream>
    #include<cstdio>
    #define ll long long
    #define mod 998244353
    #define N 1000005
    
    int head[N],cnt;
    struct P{
    	int to,next;
    }e[N << 1];
    
    inline void add(int x,int y){
    	e[++cnt].to = y;
    	e[cnt].next = head[x];
    	head[x] = cnt;
    }
    
    ll n,m,k,out[N];
    ll f[N],sum[N];
    
    int main(){
    	scanf("%lld%lld%lld",&k,&n,&m);
    	for(int i = 1;i <= m;++i){
    		ll l,r;
    		scanf("%lld%lld",&l,&r);
    		add(l,r);
    		out[l]++;
    	}
    	for(int i = 1;i <= n;++i){
    		f[i] = (out[i] + 1);
    		for(int j = head[i];j;j = e[j].next){
    			int v = e[j].to;
    			f[i] = (f[i] + (sum[i - 1] - sum[v - 1]) % mod + mod) % mod;
    		}
    		sum[i] = (sum[i - 1] + f[i]) % mod;
    	}
    	std::cout<<sum[n] % mod<<std::endl;
    	return 0;
    }
    
  • 相关阅读:
    C++中的函数
    C++基本语句
    面向对象程序设计
    数据结构中的算法
    数据结构开篇
    条件编译
    文件包含
    简单的宏替换
    系统启动过程
    parted 命令学习
  • 原文地址:https://www.cnblogs.com/dixiao/p/15015755.html
Copyright © 2020-2023  润新知