题意
传送门 P4550 收集邮票
题解
随机变量 x i x_i xi 为已取得 i i i 种邮票的状态,到取得所有种类邮票的状态,还需要取邮票的次数。则最终需要花费 ∑ i = 1 x 0 i = x 0 ( x 0 + 1 ) / 2 \sum\limits_{i=1}^{x_0}i=x_0(x_0+1)/2 i=1∑x0i=x0(x0+1)/2 元。需要求解 E ( x i ) , E ( x i 2 ) E(x_i), E(x_i^2) E(xi),E(xi2),分别设为 f ( i ) , g ( i ) f(i),g(i) f(i),g(i)。考虑从当前状态购买一张邮票的状态转移,根据递归的思路得到 E ( x i ) = i n E ( x i + 1 ) + n − i n E ( x i + 1 + 1 ) E(x_i)=\frac{i}{n}E(x_i+1)+\frac{n-i}{n}E(x_{i+1}+1) E(xi)=niE(xi+1)+nn−iE(xi+1+1) E ( x i 2 ) = i n E [ ( x i + 1 ) 2 ] + n − i n E [ ( x i + 1 + 1 ) 2 ] E(x_i^2)=\frac{i}{n}E[(x_i+1)^2]+\frac{n-i}{n}E[(x_{i+1}+1)^2] E(xi2)=niE[(xi+1)2]+nn−iE[(xi+1+1)2] 即 f ( i ) = f ( i + 1 ) + n n − i f(i)=f(i+1)+\frac{n}{n-i} f(i)=f(i+1)+n−in g ( i ) = 2 i n − i f ( i ) + n n − i + g ( i + 1 ) + 2 f ( i + 1 ) g(i)=\frac{2i}{n-i}f(i)+\frac{n}{n-i}+g(i+1)+2f(i+1) g(i)=n−i2if(i)+n−in+g(i+1)+2f(i+1)
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10005;
int N;
double f[maxn], g[maxn];
int main()
{
scanf("%d", &N);
for (int i = N - 1; i >= 0; --i)
{
f[i] = f[i + 1] + 1.0 * N / (N - i);
g[i] = 2.0 * i / (N - i) * f[i] + 1.0 * N / (N - i) + g[i + 1] + 2 * f[i + 1];
}
printf("%.2f\n", (f[0] + g[0]) / 2);
return 0;
}