Niuke.com topic Probability dp

concept:

Probability
Expectation: Mathematical expectation (mean) (or mean, also referred to as expectation) is the probability of each possible outcome in an experiment multiplied by the sum of its outcomes, which is one of the most basic mathematical characteristics. It reflects the average value of random variables.

Introduce:

Two players gamble, they both have the same probability of winning. The rules of the game are that the winner is the first to win three games, and the winner can get
a reward of 100 francs. When the game progressed to the fourth round, A won two rounds and B won one
round. At this time, the game was suspended due to some reason, so how to distribute the 100 francs is more fair?

answer:

The probability of B’s winning is 1/4, and the probability of A’s being 3/4, so according to this

Happy Running NC15532

Title:

Xiao Ming needs to run clockwise in the playground to clock in. There are two clock-in points A and B on the playground. He needs to clock in at point A first, and then at point B (even if point B is before point A), and the end can be completed after finishing the card. After running, his starting point and the positions of points A and B are random. Tell you the length of the playground X meters, and find the probability that he needs to run more than K meters.
Input K first, then input X
input:

3 
2 2
4 3
2 1

• Output:

0.50
0.22
0.00

answer:

Fix the starting point and discuss in
different categories. Case 1:
When A is before B: The distance run is less than or equal to one circle at this time.
List the relevant formulas:
K <X
A <B
B> = K
A <= x
B <= x The
probability is shown in the figure The middle shade occupies the entire square
, that isInsert picture description here

Insert picture description here
Case 2:
A is after B:
Insert picture description here
Case 3: When
K == X, (in fact, the probability of B before A) is 0.5

Code:

poj2096 NC106693 Collecting Bugs

Title:

The meaning of the question:
A software has s subsystems, which will produce n kinds of bugs.
Someone finds a bug in a day. This bug belongs to a certain kind of bug and occurs in a certain subsystem.
Seeking to find all n kinds of bugs, and find bugs in each subsystem, so the expected number of days required.
It should be noted that the number of bugs is infinite, so if a bug is found, the probability of appearing in a certain subsystem is 1/s, and
the probability of belonging to a certain type is 1/n.
• (0 <n, s <= 1 000)
• Input:
• 1 2
• Output:
• 3.00000

answer:

• f[i][j] means that there are i types of bugs that have been found, belonging to j systems, and the expected number of days after finding the required bugs.
• Known: f[n][s]=0, because the goal has been reached, and the required answer is f[0][0]
dp[i][j] The state can be transformed into the following four states:
dp[i] [j] A bug is found to belong to the i bugs that have been found and
dp[i+1][j] in j subsystems. A bug is found to be a new kind of bug, but it belongs to the found j seed system
dp[i][ j+1] A bug is found to belong to the i bugs that have been found, but belongs to the new subsystem
dp[i+1][j+1] A bug is found to belong to a new kind of bug and a new subsystem
or more than four The probabilities are:
p1 = i * j / (n * s)
p2 = (ni) * j / (n * s)
p3 = i * (sj) / (n * s)
p4 = (ni)* (sj ) / (n * s) And
because E(aA+bB+…) = aE(A) + bE(B),
dp[i,j] = p1 * dp[i,j] + p2 * dp[i+1 ,j] + p3 * dp[i,j+1] + p4 * dp[i+1,j+1] + 1;
dp[i,j] = (1 + p2 * dp[i+1,j] + p3* dp[i,j+1] + p4 * dp[i+1,j+1] )/( 1-p1)
= ( n * s + (n-i) * j * dp[i+1,j] + i*(s-j) * dp[i,j+1] + (n-i) * (s-j) * dp[i+1 , j+1] )/( n * s - i * j )

Code:

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

const int N=1010;

double dp[N][N];

int main(){
    
    

    //freopen("input.txt","r",stdin);

    int n,s;
    while(~scanf("%d%d",&n,&s)){
    
    
        dp[n][s]=0;
        for(int i=n;i>=0;i--)
            for(int j=s;j>=0;j--){
    
    
                if(i==n && j==s)
                    continue;
                dp[i][j]=(i*(s-j)*dp[i][j+1]+(n-i)*j*dp[i+1][j]+(n-i)*(s-j)*dp[i+1][j+1]+n*s)/(n*s-i*j);
            }
        printf("%.4f\n",dp[0][0]);
    }
    return 0;
}

NC210477 with rich man

Title:

• Xiao Ming is playing a game with a rich man. Specifically, this game has n reward points, and each reward point has a certain reward point.
At first he was in position 1. Every time he throws a sieve with 6 sides. If it reaches x and Xiao Ming is now standing at i, Xiao Ming will move forward x steps to reach i+x.
• If Xiao Ming throws x and i+x is greater than n, then Xiao Ming will throw again until i+x is in the right place.
Finally, if Xiao Ming reaches the position n, then Xiao Ming will end the game. Now Xiao Ming wants to know what score he expects.

answer:

• f[i] represents the expectation of the gold obtained from i to n,
• i+6 <= n means that it will not go out: f[i] = a[i] + ∑(1/6 * f[j] ) (i+1<=j<=i+6)
• i+6>nf[i] = (f[i + 1] +… + f[n]) / (n-i).

Code:

NC210481 Sieve Game

Title:

• King Gigi is playing a mobile game. The rules of this mobile game are very simple. At the beginning, you will get three sieves. The three sieves have k1, k2, and k3 sides, which means that you can throw out the numbers between [1,k1],[1,k2],[1,k3].
• Score the beginning of 0, every throw will throw sieve x, y, z three numbers, but this game is unique in that every open
office will be given three numbers a, b, c, if satisfied x=a,y=b,z=c, then your score will be cleared, otherwise your score will add x+y+z. Now King Gigi wants to know how many throws are needed to make his score greater than n. • 0≤n≤500

answer:

• Let f[i] denote the expectation of reaching the target state when i is reached, pk is the probability of throwing k points, and p0 is the probability of returning to 0. This is preprocessed first.
Then f[i]=∑(pk * f [i+k])+f[0] * p0+1
• f[i]=∑(pk * f[i+k])+f[0] * p0+1
• Each state is summed with f[0 ] Is related, and f[0] is what we are looking for, which is a constant
• Let f[i]=A[i] * f[0]+B[i];
• Substitute the right side of the above equation to get:
• f[i ]=∑(pk * A[i+k] * f[0]+pk * B[i+k])+f[0] * p0+1=
• (∑(pk* A[i+k]) +p0)f[0]+∑(pk * B[i+k])+1;
• So A[i]=(∑(pk* A[i+k])+p0) B[i]=∑ (pk * B[i+k])+1
• First recursively find A[0] and B[0], then f[0]=B[0]/(1-A[0]);

Code:

NC210487 Canteen

Title:

Insert picture description here

• 1≤k≤m≤n≤2000

answer:

• Let f[i][j] denote i individual queuing, Tomato ranks in the jth position, and the probability of reaching the target state (j<=i)
• f[n][m] is what you want
• j==1: f[i][1]=p1 * f[i][1]+p2 * f[i][i]+p4;
• 2<=j<=k: f[i][j]=p1 * f [i][j]+p2 * f[i][j-1]+p3 * f[i-1][j-1]+p4;
• k<j<=i: f[i][j] =p1 * f[i][j]+p2 * f [i][j-1]+p3 * f [i-1][j-1];

Code:

Guess you like

Origin blog.csdn.net/qq_35975367/article/details/108431531