[lLOJ 1248] Dice (III)

G - Dice (III)

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Description

Given a dice with n sides, you have to find the expected number of times you have to throw that dice to see all its faces at least once. Assume that the dice is fair, that means when you throw the dice, the probability of occurring any face is equal.

For example, for a fair two sided coin, the result is 3. Because when you first throw the coin, you will definitely see a new face. If you throw the coin again, the chance of getting the opposite side is 0.5, and the chance of getting the same side is 0.5. So, the result is

1 + (1 + 0.5 * (1 + 0.5 * ...))

= 2 + 0.5 + 0.52 + 0.53 + ...

= 2 + 1 = 3

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case starts with a line containing an integer n (1 ≤ n ≤ 105).

Output

For each case, print the case number and the expected number of times you have to throw the dice to see all its faces at least once. Errors less than 10-6 will be ignored.

Sample Input

5

1

2

3

6

100

Sample Output

Case 1: 1

Case 2: 3

Case 3: 5.5

Case 4: 14.7

Case 5: 518.7377517640

故设dp[i]为在已经扔出了i个不同面的情况下扔出n个不同面的期望次数,dp[n]=0,答案为dp[0]
则\(dp[i]=dp[i]*\frac{i}{n}+dp[i+1]*\frac{n-i}{n}+1\)

移项得:\(dp[i]=dp[i+1]+\frac{n}{n-i}\)

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
#define N 100010

int main()
{
    int T,n,iCase=1;
    double dp[N];
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        dp[n]=0;
        for(int i=n-1;i>=0;i--) dp[i]=dp[i+1]+n*1.0/(n-i);
        printf("Case %d: %.7f\n",iCase++,dp[0]);
    }
    return 0;
}
时间: 2024-10-17 16:12:51

[lLOJ 1248] Dice (III)的相关文章

LightOj 1248 - Dice (III)(几何分布+期望)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1248 题意:有一个 n 面的骰子,问至少看到所有的面一次的所需 掷骰子 的 次数的期望: 第一个面第一次出现的概率是p1 n/n; 第二个面第一次出现的概率是p2 (n-1)/n; 第三个面第一次出现的概率是p3 (n-2)/n; ... 第 i 个面第一次出现的概率是pi (n-i+1)/n; 先看一下什么是几何分布: 几何分布: 在第n次伯努利试验中,试验 k 次才得到第一次成功

1248 - Dice (III)

  PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 32 MB Given a dice with n sides, you have to find the expected number of times you have to throw that dice to see all its faces at least once. Assume that the dice is fair, that m

light oj 1248 - Dice (III)(期望)

Given a dice with n sides, you have to find the expected number of times you have to throw that dice to see all its faces at least once. Assume that the dice is fair, that means when you throw the dice, the probability of occurring any face is equal.

LightOJ 1248 - Dice (III) 给一个质地均匀的n的骰子, 求投掷出所有点数至少一次的期望次数。(概率)

题意:http://www.lightoj.com/volume_showproblem.php?problem=1248 投掷出第一个未出现的点数的概率为n/n = 1, 因为第一次投掷必然是未出现的. 第二个未出现的点数第一次出现的概率为 (n - 1) / n,因为有一个已经投掷出现过. 第i个未出现的点数第一次出现的概率为 (n - i) / i, 这满足几何分布. 其期望E = 1/p 所以期望为n *(1 + 1 / 2 + 1 / 3 + ... 1 / n). #include<

LightOJ 1248 Dice (III)

期望,$dp$. 设$dp[i]$表示当前已经出现过$i$个数字的期望次数.在这种状态下,如果再投一次,会出现两种可能,即出现了$i+1$个数字以及还是$i$个数字. 因此 $dp[i]=dp[i]*i/n+dp[i+1]*(n-i)/n+1$,即$dp[i]=dp[i+1]+n/(n-i)$,$dp[n]=0$,推出$dp[0]$即可. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio

LightOJ - 1248 Dice (III) 期望 + dp

题目大意:给出一个n面的色子,问看到每个面的投掷次数期望是多少 解题思路:水题啊,竟然被卡了那么久,也是醉了,给题目所给的那个样例误导了...不怪那个 怪自己太弱了,还得继续训练啊... 设dp[i]表示扔到i个不同面的期望,那么 dp[i + 1] = i / n * dp[i + 1] + (n - i) / n * dp[i] + 1 整理得 dp[i + 1] = n / (n - i) + dp[i] + 1 #include<cstdio> #define maxn 100010

Light OJ 1248 - Dice (III) 概率DP

n个面的骰子 求每个面至少扔到一次的期望值 设dp[i]为已经扔了i个不同面的期望值 dp[n] = 0 求dp[0] 因为dp[i]为还需要扔i个不同的面 每次可能扔中已经扔过的面或者没有扔到过的面2中情况 所以dp[i] = (i/n)*dp[i] + (n-i)/n*dp[i+1] +1 等号2边都有dp[i] 移项得dp[i] = dp[i+1]+n/(n-i) #include <cstdio> #include <cstring> #define imax 100005

【非原创】LightOj 1248 - Dice (III)【几何分布+期望】

学习博客:戳这里 题意:有一个 n 面的骰子,问至少看到所有的面一次的所需 掷骰子 的 次数的期望: 第一个面第一次出现的概率是p1 n/n; 第二个面第一次出现的概率是p2 (n-1)/n; 第三个面第一次出现的概率是p3 (n-2)/n; ... 第 i 个面第一次出现的概率是pi (n-i+1)/n; 先看一下什么是几何分布: 几何分布: 在第n次伯努利试验中,试验 k 次才得到第一次成功的机率为p.详细的说是:前k-1次皆失败,第k次成功的概率为p. 几何分布的期望E(X) = 1/p;

F - Dice (III) LightOJ - 1248

F - Dice (III) LightOJ - 1248 题目描述: 掷出有n面的色子的全部面,求他的期望. 分析: 期望dp,假设掷出i面,dp[i]表示掷出i面的期望.每次会掷出2种情况:1.掷出不同的面,转移到i+1面,概率为(n-i)/n.2.掷出相同的,状态还是i面,概率为i/n. 且花费为1(每次要投一次).dp方程:dp[i]=( dp[i+1]+1 ) * (n-i)/n+( dp[i]+1 ) *i/n,化简为dp[i]=dp[i+1]+1+i/(n-i).(期望dp要逆推)