SPOJ1026 概率DP

 Favorite Dice 

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[i]:抛到i面的期望次数,dp[i]=dp[i-1]+n/(n-i+1)

代码:

 1 #include"bits/stdc++.h"
 2 
 3 #define db double
 4 #define ll long long
 5 #define vl vector<ll>
 6 #define ci(x) scanf("%d",&x)
 7 #define cd(x) scanf("%lf",&x)
 8 #define cl(x) scanf("%lld",&x)
 9 #define pi(x) printf("%d\n",x)
10 #define pd(x) printf("%f\n",x)
11 #define pl(x) printf("%lld\n",x)
12 #define rep(i, n) for(int i=0;i<n;i++)
13 using namespace std;
14 const int N   = 1e6 + 5;
15 const int mod = 1e9 + 7;
16 const int MOD = 998244353;
17 const db  PI  = acos(-1.0);
18 const db  eps = 1e-10;
19 const ll INF = 0x3fffffffffffffff;
20 int t;
21 db dp[N];
22 void cal(int n)
23 {
24     dp[0]=0;
25     for(int i=1;i<=1000;i++) dp[i]=dp[i-1]+1.0*n/(n-i+1);//抛出新一面的次数
26     printf("%.2f\n",dp[n]);
27 }
28 int main()
29 {
30     ci(t);
31     while(t--){
32         int n;
33         ci(n);
34         cal(n);
35     }
36     return 0;
37 }

 

 

转载于:https://www.cnblogs.com/mj-liylho/p/9535931.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值