Favorite Dice (SPOJ-FAVDICE)期望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[n]=0;  

dp[i]=i/n*dp[i]+(n-i)/n*dp[i+1]+1;化简逆推即可。求的是dp[0];

AC代码:

#include <stdio.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include<iomanip>
const int maxn=1010;
const int inf=0x3f3f3f3f;
using namespace std;
double dp[maxn];
int main()
{
    int t,n;
    cin>>t;
    while(t--)
    {
        cin>>n;
        dp[n]=0;
        for(int i=n-1; i>=0; i--)
        {
            dp[i]=dp[i+1]+n/(n-(double)i);
        }
        cout<<fixed<<setprecision(2)<<dp[0]<<endl;
    }
    return 0;
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值