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
",x)
10 #define pd(x) printf("%f
",x)
11 #define pl(x) printf("%lld
",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
",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