FAVDICE - Favorite Dice
no tags
BuggyD loves to carry his favorite die around. Perhaps you wonder why it's his favorite? Well, his die is magical and can be transformed into an N-sided unbiased die with the push of a button. Now BuggyD wants to learn more about his die, so he raises a question:
What is the expected number of throws of his die while it has N sides so that each number is rolled at least once?
Input
The first line of the input contains an integer t, the number of test cases. t test cases follow.
Each test case consists of a single line containing a single integer N (1 <= N <= 1000) - the number of sides on BuggyD's die.
Output
For each test case, print one line containing the expected number of times BuggyD needs to throw his N-sided die so that each number appears at least once. The expected number must be accurate to 2 decimal digits.
Example
Input: 2 1 12 Output: 1.00 37.24
解决方案:
N 个面的骰子显示 i 个面需要丢掷次数的期望为 dp [i-1] + N / ( N - i ) (一共有 N 个面,还剩下 N - i 个面).
参考代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
double n;
while(t--&&cin>>n)
{
double dp[1010]={0.00,1.00};
for(int i=2;i<=n;i++)
{
dp[i]=dp[i-1]+n/(n+1-i);
}
printf("%.2lf\n",dp[(int)n]);
}
return 0;
}