SGU495_Kids and Prizes_概率DP

题目大意
n个装有物品的盒子,m个人,依次以等概率选择盒子从中取出物品并放回盒子,求取出物品个数的期望。

思路
一类期望题,前面的操作的结果会对此次的操作产生影响,对于连续和离散的随机变量可分别用积分和求和来求期望。
用到期望的性质公式: E(A + B) = E(A) + E(B)
A: 前 i - 1 次取出物品的期望
B: 第i次期望

dp[i] = dp[i - 1] + Σp(x) * (n - x) / x,  p(x)  前 i - 1 次取得x的概率.
dp[i] = 1 + (1 - 1 / n) * dp[i - 1].

可以O(n)的复杂度求解,也可算通项以O(1)求解

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;
int m, n;

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
    #endif // ONLINE_JUDGE
    scanf("%d %d", &n, &m);
    double pre = 1;
    for (int i = 2; i <= m; i++)
    {
        double tmp = 1 + (1 - 1 / (double)n) * pre;
        pre = tmp;
    }
    printf("%.10f\n", pre);

    return 0;
}

猜你喜欢

转载自blog.csdn.net/Anna__1997/article/details/79479456
sgu