P6835 [Cnoi2020]线形生物(期望概率,游戏冲关模型,分析流量关系)

题意:每个结点(1到n)都有一条连向下一个结点编号的边,又给出m条返祖 边,求从1号点到n+1号点的期望步数。
分析与总结:这种有关流量关系的期望题,必定和度数有关系(往往和高斯消元也有联系,但这里显然不是)。设x点到y点的期望是 e x p [ x − > y ] exp[x->y] exp[x>y],设点 x x x的度数为 d x d_x dx,那么这个走出这个点的期望步数是 1 / d x + 1 / d x ∗ ( e x p [ y − > x ] + 1 ) 1/d_x+1/dx*(exp[y->x]+1) 1/dx+1/dx(exp[y>x]+1),显然1e6的数据不能n^2。那就接着推式子,推成我们想要的exp[1->x]这样前缀的形式。洛谷里的题解不错,建议学习。

#include<bits/stdc++.h>
#define f(i,a,b) for( int i=a;i<=b;++i)
#define ff(i,a,b) for( int i=a;i>=b;--i)
#define debug(x) cerr << #x << " : " << x << " " << endl
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<string, string> pss;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const int inf = 0x3f3f3f3f;
const double tiaohe = 0.57721566490153286060651209;
ll oula(ll x) { ll res = x;f(i, 2, x / i) { if (x % i == 0) { res = res / i * (i - 1);while (x % i == 0) x /= i; } }if (x > 1) res = res / x * (x - 1);return res; }
ll quickmod(ll a, ll n, ll m) { ll s = 1;while (n) { if (n & 1) { s = s * a % m; }a = (a*a) % m;n = n / 2; }return s; }
ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
void ex_gcd(ll a, ll b, ll &x, ll &y, ll &d) { if (!b) { d = a, x = 1, y = 0; } else { ex_gcd(b, a % b, y, x, d);y -= x * (a / b); } }
ll inv(ll t, ll p) { ll d, x, y;ex_gcd(t, p, x, y, d);return d == 1 ? (x % p + p) % p : -1; }
bool isPrime(ll x) { if (x == 2)return true;if (x % 2 == 0)return false;for (ll i = 2;i*i <= x;i++) if (x % i == 0)return false; return true; }
inline int in() { char ch = getchar();int x = 0, f = 1;while (ch<'0' || ch>'9') { if (ch == '-')f = -1;ch = getchar(); }while (ch >= '0'&&ch <= '9') { x = x * 10 + ch - '0';ch = getchar(); }return x * f; }
//double a = log(n) +tiaohe + 1.0 / (2 * n);
double eqa = (1 + sqrt(5.0)) / 2.0;
double E = 2.7182818284;
const double eps = 1e-6;
const int N = 1e6 + 5;
vector<int> G[N];
int d[N];
ll f[N];
int main()
{
#ifndef ONLINE_JUDGE 
	freopen("in.txt", "r", stdin);
#endif

	int id, n, m;
	cin >> id >> n >> m;
	f(i, 1, m)
	{
		int x = in();
		int y = in();
		G[x].emplace_back(y);
		d[x]++;
	}
	f[1] = 0;
	ll res = 0;
	f(i, 1, n)
	{
		f[i] = res;
		ll tmp = 0;
		for (auto I : G[i])
		{
			tmp = (tmp + res + 1 - f[I] + mod2) % mod2;
		}
		res = (res + 1 + tmp) % mod2;
	}
	cout << res << endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值