SP1026 FAVDICE - Favorite Dice
\(\large{题目链接}\)
\(\\\)
\(\Large\textbf{Solution: } \large{1.如果目前我们已经有i种数字,那么下一次投掷有\dfrac {n-i}{n}的概率得到目前没有的数字,所以期望的步数是\dfrac {n}{n-i},那么期望总步数即为\sum \limits^{n}_{i=1}\dfrac {n}{n-i}=\sum \limits^{n}_{i=1}\dfrac {n}{i}。\\ 2.考虑dp。设f_i表示已经有了i种数字还需要的步数,那么f_n = 0,所以考虑倒推。\\再投掷一次有两种可能,一种是得到一个未得到的数字,一种是已经得到的,所以容易写出递推式:f_i = f_i + \dfrac {i}{n}f_i + \dfrac {n-i}{n}f_{i + 1} + 1,\\将上面的式子化简即为f_i = f_{i + 1} + \dfrac {n}{n-i}。}\)
\(\\\)
\(\Large\textbf{Code: }\)\
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
double f[N];
int read() {
int x = 0;
char c = getchar();
while (!isdigit(c)) c = getchar();
while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
return x;
}
double sol(int n) {
f[n] = 0;
for (int i = n - 1; i >= 0; --i) f[i] = f[i + 1] + 1.0 * n / (n - i);
return f[0];
}
int main() {
int t, n;
t = read();
while (t--) {
n = read();
printf("%.2lf\n", sol(n));
}
return 0;
}