Introduction to probability expectation

Posted by alecjw on Thu, 20 Jan 2022 07:55:41 +0100

Probability & expectation

Basic questions

1

1

Meaning:

You are in a maze with n fans in front of you, and each door has a number k;

If k is positive, you can walk through this door for k minutes,

If it is negative, go through this door for -k minutes and return to the maze;

The probability of going through each door is the same Ask the expectation of the time it takes to get out of the maze;

Idea:

Antecedent formula: it can be divided into positive and negative cases
E ( x ) = ∑ i = 1 k 1 1.0 / n ∗ a b s ( a i ) + ∑ i = 1 k 2 1.0 / n ∗ ( a b s ( b i ) + E ( x ) ) E(x) = \sum_{i=1}^{k_1}1.0/n * abs({a_i}) + \sum_{i=1}^{k_2}1.0/n * (abs({b_i}) + E(x)) E(x)=∑i=1k1​​1.0/n∗abs(ai​)+∑i=1k2​​1.0/n∗(abs(bi​)+E(x))
( a 1 , a 2 , a 3 , a 4 , . . . . a k 1 by place have just number , b 1 , b 2 , b 3 , b 4 , . . . . b k 2 by place have negative number ) ({a_1}, {a_2}, {a_3}, {a_4},..., {a_ {K1}} are all positive numbers, {b_1}, {b_2}, {b_3}, {b_4},..., {b_ {K2}} are all negative numbers) (a1, a2, a3, a4,... ak1 are all positive numbers, b1, b2, b3, b4,... bk2 are all negative numbers)
Simplification formula:
E ( x ) = ( ∑ i = 1 n a b s ( n u m i ) ) / k 1 E(x) = (\sum_{i=1}^{n}abs({num_i}))/{k_1} E(x)=(∑i=1n​abs(numi​))/k1​

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

int main()
{
    int T, cnt = 1;  cin >> T;
    while(T--)
    {
        int n;
        LL sum = 0, num = 0;
        cin >> n;
        for (int i = 1; i <= n; ++ i)
        {
            LL x;
            cin >> x;
            sum += abs(x);
            num += (x > 0 ? 1 : 0);
        }

        LL t = __gcd(sum, num);
        sum /= t, num /= t;
        printf("Case %d: ", cnt++);
        if (num == 0) cout << "inf" << endl;
        else 
            cout << sum << "/" << num << endl;
    }
    return 0;
}

2

2

Meaning:

There is a maze that is 1 × n grid, a person can take all the gold in this grid every time he arrives at a grid. He just starts to stand in the first grid, and then starts to roll dice to get the point x. he has to go from the current position to the position of adding x. if he finds that the position is greater than n, he will roll dice again until it meets. If he reaches the nth grid, he can end. Ask the man what he expects from the maze.

Idea:

Formulation: state transition equation
f ( x ) = ∑ i = 1 6 a i + x + 1 6 f ( x + i ) f(x) = \sum_{i=1}^{6}{a_{i+x}}+\frac{1}{6}f(x+i) f(x) = Σ i=16 ai+x + 61 f (x + I) (the probability changes when x + I > N, which needs special treatment)
Direct memory search

#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long LL;
const int N = 110;
int n;
double f[N];
int a[N];

double dp(int u)
{
    if (f[u] >= 0) return f[u];
    double res = a[u];
    for (int i = 1; i <= 6; ++ i)
        if (u + i <= n)
        {
            int g = min(6, n - u);
            res += 1.0 / g * dp(u + i);
        }
    return f[u] = res;
}

int main()
{
    int T, cnt = 1;  cin >> T;
    while(T--)
    {
        memset(f, -1, sizeof f);
        cin >> n;
        for (int i = 1; i <= n; ++ i) cin >> a[i];
        
        
        printf("Case %d: %.8f\n", cnt++, dp(1));
    }
    return 0;
}

3

3

Meaning:

Give you a number, N, divided by a factor of n every time with equal probability, and ask the expected number of times n becomes 1

Idea:

Column formula:
f ( n ) = 1 + 1 m f ( a 1 ) + 1 m f ( a 2 ) + 1 m f ( a 3 ) + . . . + 1 m f ( a m ) f(n) = 1+\frac{1}{m}f({a_1})+\frac{1}{m}f({a_2})+\frac{1}{m}f(a_3)+...+\frac{1}{m}f(a_m) f(n)=1+m1​f(a1​)+m1​f(a2​)+m1​f(a3​)+...+m1​f(am​)
( a 1 , a 2 , . . . . a m yes n of m individual about number , And a 1 < a 2 < a 3 < . . . . < a m , so a 1 = 1 , a m = n ) , f ( a 1 ) = f ( 1 ) = 0 , f ( a m ) = f ( n ) ({a_1},{a_2},....{a_m} is the M divisor of N, and {a_1} < {a_2} < {a_3} <... < {a_m}, so {a_1} = 1, {a_m} = n), f ({a_1}) = f (1) = 0, f ({a_ {m}) = f (n) (a1, a2,... Am is the m divisor of N, and a1 < a2 < A3 <... < am, so a1 = 1,am = n),f(a1) = f(1)=0,f(am) = f(n)
Simplification formula:
f ( n ) = 1 + 1 m f ( a 2 ) + 1 m f ( a 3 ) + . . . + 1 m f ( a m − 1 ) + 1 m f ( n ) f(n) = 1+\frac{1}{m}f({a_2})+\frac{1}{m}f(a_3)+...+\frac{1}{m}f(a_{m-1})+\frac{1}{m}f(n) f(n)=1+m1​f(a2​)+m1​f(a3​)+...+m1​f(am−1​)+m1​f(n)
Finding the divisor number m of n by trial division
Direct memory search

#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long LL;
const int N = 100010;
const double eps = 1e-7;
int n;
double f[N];

int get_num(int x)
{
    int res = 1;
    for (int i = 2; i <= x/i; ++ i)
        if (x % i == 0)
        {
            int s = 0;
            while(x % i == 0) x /= i, s ++ ;
            res *= (s + 1);
        }
    if (x > 1) res *= 2;
    return res;
}

double dp(int x, int u)
{
    //cout << x << " " << u << endl;
    if (x == 1) return 0;
    if (f[x] >= 0) return f[x];
    f[x] = 1;
    
    for (int i = 2; i <= x/i; ++ i)
        if (x % i == 0)
        {
            if (i * i != x) f[x] += 1.0 / u * dp(i, get_num(i));
            f[x] += 1.0 / u * dp(x / i, get_num(x / i));
        }
    
    return f[x] = f[x] * u / (u - 1);
}

int main()
{
    int T, cnt = 1;  scanf("%d", &T);
    memset(f, -1, sizeof f);
    while(T--)
    {
        scanf("%d", &n);
        
        printf("Case %d: %.8f\n", cnt++, dp(n, get_num(n)));
    }
    return 0;
}

4

4

Meaning:

There are N banks. The i-th bank has mi Yuan. The probability of being caught robbing the i-th bank is pi. Give a risk rate P and ask how much money you can rob at most when the probability of being caught is less than or equal to P.

Idea:

Dp status indicates: f[i, j] indicates the maximum probability of not being caught robbing j yuan in the current ith bank
Is it like the 01 backpack template question, that's it
Problem: the probability of being caught should be less than or equal to P -- > the probability of not being caught should be greater than or equal to P
Then the probability of being caught every time i rob the bank is equal to the current probability of being caught ∗ ( 1 − p i ) *({1-{p_i}}) ∗(1−pi​)
Then the state transition equation is obtained
f [ i , j ] = m a x ( f [ i − 1 , j ] , f [ i − 1 , j − w i ] ∗ ( 1 − p i ) ) f[i, j] = max(f[i - 1, j], f[i - 1,j - {w_i}] * (1 - {p_i})) f[i,j]=max(f[i−1,j],f[i−1,j−wi​]∗(1−pi​))

#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long LL;
const int N = 110;
double P, p[N];
int w[N];
double f[N * N];

int main()
{
    int T, cnt = 1;  cin >> T;
    while(T--)
    {
        int n, m = 0;
        cin >> P >> n;
        P = 1 - P;
        for (int i = 1; i <= n; ++ i) cin >> w[i] >> p[i], m += w[i];
        
        int res = 0;
        memset(f, 0, sizeof f);
        f[0] = 1;
        for (int i = 1; i <= n; ++ i)
        {
            for (int j = m; j >= 0; -- j)
            {
                f[j] = max(f[j], f[j - w[i]] * (1 - p[i]));
                if (f[j] >= P) res = max(res, j);
            }
        }
        
        
        printf("Case %d: %d\n", cnt++, res);
    }
    return 0;
}

5

5

Meaning:

Given the number of days in a year, find the minimum number of people who must be invited to the party, so that the probability of at least two people having the same birthday is at least 0.5.

Idea:

Set a year n days
On the contrary, the second person's birthday is different from that of the previous person

#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long LL;
const int N = 110;
double P;

int main()
{
    int T = 0, cnt = 1;  cin >> T;
    while(T--)
    {
        double p = 1;
        int res = 1, n;
        cin >> n;
        while(p > 0.5) p *= (1.0 * n - res) / n, res ++ ;
        res -- ;

        printf("Case %d: %d\n", cnt++, res);
    }
    return 0;
}

To be continued...

Topics: ICPC