Algorithm study notes: probability/expectation DP

Algorithm study notes: probability/expectation DP

1 Introduction

Probability/Expectation DP is a kind of DP used to calculate probability or expectation.

In fact, I think this kind of DP is to calculate the expectation, after all, the probability can be regarded as the expectation with a cost of 1.

Readers who have not learned expectations can read this article: Algorithm Study Notes: Probability and Expectation

For probability/expectation DP, the most important thing is the expectation equation.

Let's look at an example question below.

2. Examples

CF1265E Beautiful Mirrors

Take this question as an example to explain in detail the general routine of expecting DP.

For convenience, consider pi p_i directly belowpi It's probability.

First set the state.

Expectation DP Set state: f [i] = expectation from 1 to i (number of days/number of steps/cost/...) F[i]= expectation from 1 to i (number of days/number of steps/cost/... )f[i]=From 1 to i of the period look ( Days Number / Step Number / generations price / . . . ) .

For this question, fi f_ifiFrom the first mirror to the iiI look forward to the number of days in all mirrors.

Expected DP transfer: use fi − 1, fi f_{i-1},f_ifi1,fi List the expectation equation.

Listing the expectation equation is the point! Can't make a mistake! Don't get it wrong

  1. No. iiI failed to ask for the day, and start from the beginning.
    At this time, the probability is1 − pi 1-p_i1pi, The number of consumed days is fi − 1 + 1 + fi f_{i-1}+1+f_ifi1+1+fi, So the probability multiplied by the cost is (1 − pi) (fi − 1 + 1 + fi) (1-p_i)(f_{i-1}+1+f_i)(1pi)(fi1+1+fi)
  2. No. iiThe inquiry succeeded in i days.
    At this time, the probability ispi p_ipi, The number of days spent fi − 1 + 1 f_{i-1}+1fi1+1 , so the probability multiplied by the cost ispi (fi − 1 + 1) p_i(f_{i-1}+1)pi(fi1+1)

综上,有 f i = ( 1 − p i ) ( f i − 1 + 1 + f i ) + p i ( f i − 1 + 1 ) f_i=(1-p_i)(f_{i-1}+1+f_i)+p_i(f_{i-1}+1) fi=(1pi)(fi1+1+fi)+pi(fi1+1)

However pi p_ipi It's just a probability, you need to divide by 100 in the real code.

After dividing by 100, the final solution is fi = 100 (fi − 1 + 1) pi f_i=\dfrac{100(f_{i-1}+1)}{p_i}fi=pi100(fi1+1), Linear recursion is enough, don’t forget the inverse element.

Summarize the general routine of expecting DP:

  1. Set state fi f_ifiFrom 1 to iii 's expected number of days/steps/...
  2. List about fi − 1 f_{i-1}fi1Japanese fi f_ifi The expectation equation.
  3. Solve the equation, and then recurse linearly.
  4. If there is a special initial value, add it.

A few points to note:

  1. It is hoped that the equation will not be wrong, and every situation must be considered.
  2. Don't solve the equation wrong
  3. The initial value cannot be missed.

Example code:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
const int MAXN = 2e5 + 10, P = 998244353;
int n, p[MAXN];
LL f[MAXN];

int read()
{
    
    
	int sum = 0, fh = 1; char ch = getchar();
	while (ch < '0' || ch > '9') {
    
    if (ch == '-') fh = -1; ch = getchar();}
	while (ch >= '0' && ch <= '9') {
    
    sum = (sum << 3) + (sum << 1) + (ch ^ 48); ch = getchar();}
	return sum * fh;
}

LL ksm(LL a, LL b)
{
    
    
	LL ans = 1 % P;
	for (; b; b >>= 1)
	{
    
    
		if (b & 1) ans = ans * a % P;
		a = a * a % P;
	}
	return ans;
}

int main()
{
    
    
	n = read();
	for (int i = 1; i <= n; ++i) p[i] = read();
	for (int i = 1; i <= n; ++i) f[i] = 100ll * (f[i - 1] + 1) % P * ksm(p[i], P - 2) % P;
	printf("%lld\n", f[n]);
	return 0;
}

3. Practice questions

For the practice questions of probability/expected DP, please go to DP algorithm summary & topic training 1 (probability/expected DP & digital DP) .

Guess you like

Origin blog.csdn.net/BWzhuzehao/article/details/113766660