[CF518D] Ilya and Escalator

[CF518D] Ilya and Escalator - 概率dp

Description

有 N 个人,每秒最前面的人 p 个概率被弹出,否则不动,求 T 秒后队列中的人数期望。

Solution

(f[i][j]) 表示过了 i 秒后,有 j 个人上了电梯的概率

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

#define int long long
const int N = 2005;
double f[N][N];

signed main()
{
    ios::sync_with_stdio(false);

    int n;
    double p;
    int t;

    cin >> n >> p >> t;

    f[0][0] = 1;
    for (int i = 0; i <= t; i++)
        for (int j = 0; j <= n; j++)
        {
            if (j < n)
                f[i + 1][j + 1] += p * f[i][j];
            else
                f[i + 1][j] += p * f[i][j];
            f[i + 1][j] += (1 - p) * f[i][j];
        }

    double ans = 0;
    for (int i = 1; i <= n; i++)
        ans += f[t][i] * i;

    cout << fixed << setprecision(12) << ans;
}
原文地址:https://www.cnblogs.com/mollnn/p/14414706.html