[BZOJ1426]收集邮票(概率期望dp)

题面

https://darkbzoj.tk/problem/1426

题解

记\(Pr(i,j)\)为买了i次,正好买到j种票的概率。(“正好”的含义是:第i次刚好解锁一种新的票)

则所求答案为\(\sum_{x}{\frac{x(x+1)}{2}}Pr(x,n)\)。

将其拆开,设\(\sum_x{x^2}Pr(x,i)=f_2[i],\sum_x{xPr(x,i)}=f_1[i]\)。

\[f_1[i+1]=\sum\limits_{x}xPr(x,i+1) \]

\[=\sum\limits_{x}x{\sum\limits_{j=0}^{x-1}}Pr(j,i){\frac{i^{x-j-1}*(n-i)}{n^{x-j}}} \]

\[={\sum\limits_{j=0}^{+\infty}}Pr(j,i)\sum\limits_{x=j+1}^{+\infty}x{\frac{i^{x-j-1}*(n-i)}{n^{x-j}}} \]

\[={\sum\limits_{j=0}^{+\infty}}Pr(j,i)\sum\limits_{d=1}^{+\infty}(d+j){\frac{i^{d-1}*(n-i)}{n^{d}}} \]

\[=\sum\limits_{j=0}^{+\infty}Pr(j,i){\times}{\frac{n-i}{i}}\sum\limits_{d=1}^{+\infty}(d+j)(\frac{i}{n})^{d} \]

\[=\sum\limits_{j=0}^{+\infty}Pr(j,i){\times}{\frac{n-i}{i}}(j\sum\limits_{d=1}^{+\infty}(\frac{i}{n})^{d}+\sum\limits_{d=1}^{+\infty}d(\frac{i}{n})^d) \]

\[=\sum\limits_{j=0}^{+\infty}Pr(j,i){\times}{\frac{n-i}{i}}(j*{\frac{i}{n-i}}+{\frac{ni}{(n-i)^2}}) \]

\[=\sum\limits_{j=0}^{+\infty}Pr(j,i)(j+{\frac{n}{(n-i)}}) \]

由于\(\sum_{j=0}^{+\infty}Pr(j,i)j=f_1[i]\)以及\(\sum_{j=0}^{+\infty}Pr(j,i)=1\),得到

\[f_1[i+1]=f_1[i]+{\frac{n}{n-i}} \]

f2也类似:

\[f_2[i+1]=\sum\limits_{x}x^2Pr(x,i+1) \]

\[=\sum\limits_{x}x^2{\sum\limits_{j=0}^{x-1}}Pr(j,i){\frac{i^{x-j-1}*(n-i)}{n^{x-j}}} \]

\[={\sum\limits_{j=0}^{+\infty}}Pr(j,i)\sum\limits_{x=j+1}^{+\infty}x^2{\frac{i^{x-j-1}*(n-i)}{n^{x-j}}} \]

\[={\sum\limits_{j=0}^{+\infty}}Pr(j,i)\sum\limits_{d=1}^{+\infty}(d+j)^2{\frac{i^{d-1}*(n-i)}{n^{d}}} \]

\[=\sum\limits_{j=0}^{+\infty}Pr(j,i){\times}{\frac{n-i}{i}}\sum\limits_{d=1}^{+\infty}(d^2+2dj+j^2)(\frac{i}{n})^{d} \]

\[=\sum\limits_{j=0}^{+\infty}Pr(j,i){\times}{\frac{n-i}{i}}(j^2\sum\limits_{d=1}^{+\infty}(\frac{i}{n})^{d}+ 2j\sum\limits_{d=1}^{+\infty}{d(\frac{i}{n})^d}+\sum\limits_{d=1}^{+\infty}d^2(\frac{i}{n})^d) \]

\[=\sum\limits_{j=0}^{+\infty}Pr(j,i){\times}{\frac{n-i}{i}}(j^2{\frac{i}{n-i}}+ 2j{\frac{ni}{(n-i)^2}}+\sum\limits_{d=1}^{+\infty}d^2(\frac{i}{n})^d) \]

其中\(\sum_{d=1}^{+\infty}d^2(\frac{i}{n})^d\)可以用扰动法求解:

\[S=\sum\limits_{d=1}^{+\infty}d^2(\frac{i}{n})^d \]

\[=\sum\limits_{d=0}^{+\infty}(d+1)^2(\frac{i}{n})^{d+1} \]

\[=\frac{i}{n}\sum\limits_{d=0}^{+\infty}(d^2+2d+1)(\frac{i}{n})^d \]

\[=\frac{i}{n}(\sum\limits_{d=0}^{+\infty}d^2(\frac{i}{n})^d+2\sum\limits_{d=0}^{+\infty}d(\frac{i}{n})^d+\sum\limits_{d=0}^{+\infty}(\frac{i}{n})^d) \]

\[=\frac{i}{n}(S+2\times\frac{ni}{(n-i)^2}+\frac{n}{n-i}) \]

所以算出\(S=\frac{ni^2+n^2i}{(n-i)^3}\)。代入原式,可得

\[f_2[i+1]=\sum\limits_{j=0}^{+\infty}Pr(j,i){\times}{\frac{n-i}{i}}(j^2{\frac{i}{n-i}}+ 2j{\frac{ni}{(n-i)^2}}+\frac{ni^2+n^2i}{(n-i)^3}) \]

\[=\sum\limits_{j=0}^{+\infty}Pr(j,i)(j^2+\frac{2jn}{n-i}+\frac{ni+n^2}{(n-i)^2}) \]

\[=f_2[i]+\frac{2n}{n-i}f_1[i]+\frac{ni+n^2}{(n-i)^2} \]

所以有了下面两个式子:

\[\begin{cases} f_1[i+1]=f_1[i]+{\frac{n}{n-i}} \\ f_2[i+1]=f_2[i]+\frac{2n}{n-i}f_1[i]+\frac{ni+n^2}{(n-i)^2} \end{cases} \]

就可以展开递推求解\(f_1,f_2\)。最后的答案就是\(\frac{f_1[n]+f_2[n]}{2}\)。

代码

此题笔头工作很多,代码几乎没有

#include<bits/stdc++.h>

using namespace std;

#define rg register
#define In inline

const int N = 1e4;

In double sqr(double x){
	return x * x;
}

int n;
double f1[N+5],f2[N+5];

int main(){
	cin >> n;
	for(rg int i = 0;i < n;i++){
		f1[i+1] = f1[i] + 1.0 * n / (n - i);
		f2[i+1] = f2[i] + f1[i] * 2 * n / (n - i) + (n * i + n * n) / sqr(n - i);
	}
	printf("%.2f\n",0.5 * (f1[n]+f2[n]));
	return 0;
}
上一篇:斐波那契数列——(顺推法)


下一篇:算法教程-动态规划