Probabilistic expectation dp

Regarding probability dp, I have always been confused. Although I have attacked for exams before, I still didn't master it very well, so I decided to study it again and record the important things.

1.hdu4405

Description

Rolling dice on a \(1*n\) grid, starting from the \(0\) point, how many steps forward are thrown, and some grid points are directly connected, that is, if \(a\) , \(b \) is connected, and when it falls to the \(a\) point, it will fly directly to the \(b\) point. Seek to reach \(n\) or exceed \(n\) the expected number of dice rolls

Solution

This question helps to understand why expectations are often reversed.

If it is a forward push, for each point as the end point, there may be many starting points, so it is inconvenient to calculate, and even if the starting points of all the lines ending at this point are counted, the starting points come from these starting points. The paths are not all paths that can reach the point, because when the dice are rolled, it is possible to skip all the starting points and get to this point.

In reverse push, the point affected by each point is determined, so it is very easy to calculate.

In fact, there are many plans to reach each grid, but the plan to exit from each grid is certain.

For \(dp[i]=1+\sum dp[j]\) where \(j\) is a point that can be reached from \(i\) , why is this formula legal? Because the latter \(\sum\) calculates the previous number of steps, that is, the meaning it represents is the number of steps, and then add this \(1\) just fine.

code

2.hdu4418

Description

A person walks back and forth on the number line numbered \(0..n-1\) (assuming \(n\) is \(4\) , then its walking line is \(0, 1, 2, 3, 2, 1, 0, 1, 2, 3, 2, 1, ...\) ), take \(i\) steps with the probability of \(p[i]\) \((i∈[1 ,m], and ∑p[i]=1)\) , given \(n,m\) and the starting point and the end point, find out the expected number of steps this person takes to the end point.

Solution

This is written in great detail, so I'll just be lazy.
blog

code

3.hdu4336

Description

Inside each pack of snacks, there may or may not be a card. It is known that there are a total of n cards, and the i-th card may appear as \(p_i\) . Ask what is your expectation of the number of snacks you need to eat to collect all the cards.

Solution

Now simplify the problem, if there is only one kind of card, suppose you need to eat \(E\) package of snacks, then you can get
\[E=1*p+(1-p)*(E+1)\]

If the first pack of snacks has cards, then the expectation is \(1*p\) , if the first pack does not, then more packs of snacks are needed on the basis of this pack, and how many are needed, obviously is \(E\) , then the expectation is \(1*(E+1)\) .

Because \(n\) is very small, considering the state pressure, let \(E[i]\) be the expected number of steps required to collect all the cards in \(i\) after the cards in \(i\) have been collected. Then, according to the full expectation formula, we can get
\[E[i]=\sum_{j \notin i} p[j]*(1+E[i|(1<<j)])+(1-\sum_ {j \notin i}p[j])*(E[i]+1)\]

Another special case of \(n=2\)
For two cards
, let \(E(00)\) indicate that all cards have not been collected until all cards have been collected \((11)\) The expectation that you need to buy snacks
\ (E(10)\) indicates that the first type of card has been collected, until all cards are collected \((11)\) The expectation of faceted snacks is required

Then,
\(E(00)=p_1*(1+E(10))\) //Bought the first pack of snacks and touched the first card, the probability is \(p_1\) , and then you need \( E(10)\) Pack of snacks
\(+p_2*(1+E(01))\) //Bought the first pack of snacks and touched the second card, the probability is \(p_2\) , and then you need to \(E(01)\) Package of snacks
\(+(1-p1-p2)(1+E(00))\) //Bought the first package of snacks, there is nothing in it, the probability is \(1- p_1-p_2\) , then you need \(E(00)\) to pack instant noodles

\(E(01)=p1*(1+E(11)) +(1-p1-p2) *(1+E(01))\)
\(E(10)=p2*(1+E( 11))+(1-p1-p2)*(1+E(10))\)
\(E(11)=0\) //It has been collected, of course, you only need to buy 0 packs of snacks.

code

4.poj2151

Description

There are n teams and there are m questions in total. The Organizing Committee expects that each team should solve at least 1 problem, and the champion team should solve at least t problems. Given the probability of each team solving each problem, find the probability of meeting the expectations.

Solution

In fact, you can ignore the concept of the championship team and directly convert the problem into: each team solves at least one problem, and at least one team solves at least t problems.

It is a little troublesome to directly ask for it, so it is easy to come up with the idea of ​​a routine: ask for the probability that each team solves at least one problem \(p_1\) , and the number of problems solved by all teams is \(0 .. . The probability within the range of n-1\) \(p_2\) , and then using \(p_1-p_2\) is the answer.

It is impossible to set up such a \(dp\) equation, set \(dp[i][j][k]\) as the \(i\)th team, and currently have achieved the \(j\)th question, The probability of solving a total of \(k\) problems. The shift is also obvious:
\[dp[i][j][k]=dp[i][j-1][k-1]*p[i][j]+dp[i][j-1] [k]*(1-p[i][j])\]

然后
\[p_1=\prod_{i=1}^{n}(\sum_{j=1}^{m}dp[i][m][j])\]

\[p_2=\prod_{i=1}^{n}(\sum_{j=1}^{t-1}dp[i][m][j])\]

\[Ans=p_1-p_2\]

code

5.hdu4035

Description

There are n rooms connected by n-1 tunnels, which actually form a tree, starting from node 1 and starting to walk, there are 3 possibilities at each node i:
1. Killed, Go back to node 1 (probability is ki)
2. Find the exit and walk out of the maze (probability is ei)
3. There are m edges connected to this point, walk one randomly
Find: the expected value of the number of edges to go out of the maze.

Solution

For the small data of this question, I originally wanted to use Gaussian elimination to do it, but I don't know why there is a problem.
code

Let E[i] denote the expected number of edges to go out of the maze at node i. E[1] is what is required.

E 结 点
\ E (E [i] = ki * E [1] + ei * 0 + (1-ki-ei) * (E [father [i]] + 1); \)
\ (= ki * E [1] + (1-ki-ei) * E [father [i]] + (1-ki-ei); \)

Non-leaf node: (m is the number of edges connected to the node)
\(E[i] = ki*E[1] + ei*0\)
\(+ (1-ki-ei)/m*( E [father[i]]+1 + ∑( E[child[i]]+1 ) );\)
\(= ki*E[1] + (1-ki-ei)/m*E[father[i ]]\)
\(+ (1-ki-ei)/m*∑(E[child[i]]) + (1-ki-ei);\)

For each node: \(E[i] = Ai*E[1] + Bi*E[father[i]] + Ci;\)

For non-leaf node i, let j be the child node of i, then
\(∑(E[child[i]]) = ∑E[j]\)
\(= ∑(Aj*E[1] + Bj *E[father[j]] + Cj)\)
\(= ∑(Aj*E[1] + Bj*E[i] + Cj)\)

Top-level formula child acquisition
\ ((1- (1-ki-ei) / m * ∑Bj) * E [i] = \)
\ ((ki + (1-ki-ei) / m * ∑Aj) * E [1] + (1-ki-ei) / m * E [father [i]] + \)
\ ((1-ki-ei) + (1-ki-ei) / m * ∑Cj; \ )

I 可 得 A
(Ai = (ki + (1-ki-ei) / m * ∑Aj) / (1 - (1-ki-ei) / m * ∑Bj); \)
\ (Bi = (1- ) ki-ei) / m / (1 - (1-ki-ei) / m * ∑Bj); \)
\ (Ci = ((1-ki-ei) + (1-ki-ei) / m * Cj) \)
\ (/ (1 - (1-ki-ei) / m * ∑Bj); \)


Opposite Kanako conclusion \ (Ai = ki; \)
\ (Bi = 1 --ki --ei ; \)
\ (Ci = 1 --ki --ei; \)

Start from the leaf node until A1, B1, C1 are calculated;

\(E[1] = A1*E[1] + B1*0 + C1;\)
So
\(E[1] = C1 / (1 - A1);\)
If A1 approaches 1, there is no solution. ..

code

6.hdu4089

Description

There are n people queuing up to activate the game on the official website. Tomato is the mth.
For the first person in the queue. There are the following situations:
1. The activation fails, stay in the queue and wait for the next activation (probability is p1)
2. Lose the connection, exit the queue, and then queue at the end of the queue (probability is p2)
3. Activation succeeds, leave the queue (probability For p3)
4. The server is paralyzed, the server stops activating, and no one can activate it.
Find the probability that Tomato's position in the queue is <= k when the server is down

Solution

Let \(dp[i][j]\) be a total of \(i\) people in the queue at that time, Tomato is ranked \(j\) , and his ranking is less than or equal to \(k\) when the system is paralyzed The probability. It needs to be understood here that this does not mean that the system is paralyzed at this moment, but after several transformations under the current state, the ranking is less than or equal to \(k\) when paralyzed .
Then the final answer we need is \(dp[n][m]\)

The \(dp\) equation is obvious:
\[j==1:dp[i][1]=p_1*dp[i][1]+p_2*dp[i][i]+p_4*1\]

\[2 \leq j \leq k:dp[i][j]=p_1*dp[i][j]+p_2*dp[i][j-1]\]

\[+p_3*dp[i-1][j-1]+p_4*1\]

\[k<j \leq i :dp[i][j]=p_1*dp[i][j]+p_2*dp[i][j-1]+p_3*dp[i-1][j-1]\]

For the second equation, a little explanation.
According to the full probability formula
\[P(A)=\sum P(B_i)*P(A|B_i)\]

Among them, \(B_i\) are mutually exclusive
. When the first event occurs, the probability of this happening is \(p_1\) , and after this event, the probability of reaching the goal is obviously \(dp[i] [j]\) Then the two can be multiplied.
When the second event occurs, since the first person in the sequence went to the last one, the probability after this event is \(dp[i][j-1]\) , because the total number of people has not changed Yes, Tomato certainly wasn't the first, and he moved up one more place in the rankings.
When the third event occurs, the first person goes out, and the total number and ranking change at the same time, so when the fourth event occurs in \(dp[i][j]\)
, because the target state has been reached , so obviously the probability after this happens is \(1\)

Let \(P_2=p_2/(1-p_1)\) , \(P_3=p_3/(1-p_1)\) , \(P_4=p_4/(1-p_1)\)

Then simplify:
\[j==1:dp[i][1]=P_2*dp[i][i]+P_4\]

\[2 \leq j \leq k:dp[i][j]=P_2*dp[i][j-1]+P_3*dp[i-1][j-1]+P_4\]

\[k<j \leq i:dp[i][j]=P_2*dp[i][j-1]+P_3*dp[i-1][j-1]\]

There's a bit of trouble here, because if you need to find \(dp[i][1]\) , you have to find \(dp[i][i]\) first . Because the latter part of each formula has nothing to do with \(dp[i]\) , it can be calculated first.

\[j==1:dp[i][1]=P_2*dp[i][i]+c[j]\]

\[2 \leq j \leq k:dp[i][j]=P_2*dp[i][j-1]+c[j]\]

\[k<j \leq i:dp[i][j]=P_2*dp[i][j-1]+c[j]\]

Push the push formula, you can easily get the method to quickly get \(dp[i][i]\) , then get \(dp[i][1]\) , and finally push it back.
Finally, to judge a special case, when \(p_4<0\) , just output \(0\) directly .

code

7.poj2096

Description

A software has s subsystems, which will generate n kinds of bugs
. Someone finds a bug in one day. This bug belongs to a subsystem and belongs to a category
. The probability of each bug belonging to a subsystem is 1/s, and the probability of belonging to a category is 1/n
asks the expectation of the number of days that each subsystem finds bugs when n types of bugs are found.

Solution

dp[i][j] indicates that i bugs and j system bugs have been found, and the expected number of days to reach the target state is
dp[n][s]=0; the required answer is dp[0][0];

Transfer:
\(dp[i][j]\) , found a bug belonging to existing i categories and j systems. The probability is \((i/n)*(j/s)\)
\(dp[i][j+1]\) , it is found that a bug belongs to the existing classification and does not belong to the existing system. The probability is \((i/n)*(1-j/s)\)
\(dp[i+1][j]\) , it is found that a bug belongs to the existing system, not to the existing classification, the probability For \((1-i/n)*(j/s)\)
\(dp[i+1][j+1]\) , it is found that a bug does not belong to the existing system and does not belong to the existing classification , the probability is
\((1-i/n)*(1-j/s)\)

8.poj3744

Description

On a road, the starting point is at 1. There are mines at N points, \(1 \leq N \leq 10\) . The coordinate range of the mine point: \([1,100000000]\)
There is a probability of p to advance 1 step each time, and a probability of 1-p to advance 2 steps. Ask the probability of passing the road smoothly. Just don't go where there are mines.

Solution

Let \(dp[i]\) be the probability of reaching the point \(i\) .
Then obviously we can get:
\[dp[i]=p*dp[i-1]+(1-p)*dp[i-2]\]

But since the range of coordinates is very large, matrix optimization is required.

Set the coordinates of all mines to be stored in the \(x\) array, and the matrix is ​​\(S\)
Use \(n\) points to divide the road into \(n+1\) segments
\(x[0]=0\ )
\(x[0]+1...x[1]\)
\(x[1]+1...x[2]\)
...
\(x[n-1]+1.. .x[n]\)
There is only one mine per segment, so we only ask for the probability of passing each segment. Multiplication principle Multiplication is the answer. For each segment, the probability of passing the segment is equal to 1 - the probability of stepping on a mine at the end of the segment.
Then
\[dp[x[1]]=S^{x[1]-x[0]-1}\]

\[dp[x[1]+1]=1-dp[x[1]]=1-S^{x[1]-x[0]-1}\]

The probability of successfully passing the next paragraph is
\[dp[x[2]+1]=dp[x[1]+1]*(1-S^{x[2]-x[1]-1})\]

\[=(1-S^{x[1]-x[0]-1})*(1-S^{x[2]-x[1]-1})\]

所以
\[dp[x[n]+1]=\prod_{i=1}^{n}(1-S^{x[i]-x[i-1]-1})\]

The final matrix optimization is just
fine

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325708612&siteId=291194637