Favorite Dice(期望dp)

网友投稿 260 2022-08-30

Favorite Dice(期望dp)

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个面的筛子,问要使得每个面都朝上一次,需要投掷多少次。

思路:

期望dp一般逆推。

一般都是dp[i]表示已经出了i个面,距离n个面还差 的期望。

第i次投掷可以落在前i个面,概率为i/n,也可以落在另外的(n-i)个面,概率是(n-i)/n。

每次投掷都百分百的增加投掷次数1.

dp[i]=i/n*dp[i]+(n-i)/n*dp[i+1]+1.

化简的dp[i]=dp[i+1]+n/(n-i).

代码:

#include using namespace std;double dp[1100];int main(){ int t; scanf("%d",&t); while(t--) { memset(dp,0,sizeof(dp)); double n; scanf("%lf",&n); dp[(int)n]=0; for(int i=n-1;i>=0;i--) { dp[i]=dp[i+1]+n/(n-(double)i); } printf("%lf\n",dp[0]); } return 0;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:重金营销的领克销量平平,质量问题频发!(领克销量如何)
下一篇:POJ 3687 Labeling Balls (拓扑排序)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~