题目链接:LightOJ - 1248

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.5^2 + 0.5^3 + ...\)

\(= 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 ≤ 10^5)\).

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

Solution

题意

给定一个 \(n\) 面的骰子,每个面出现的概率相同,现在要所有的面都至少出现一次,求投掷次数的期望。

思路

期望DP

期望DP一般是倒推的。

设 \(dp[i]\) 为已经出现了 \(i\) 个面,还需要投掷次数的期望值。

那么每次投掷只有两种情况:出现已经出现过的面、出现未出现的面。前者概率为 \(\frac{i}{n}\),后者概率为 \(\frac{n - i}{n}\)。

则状态转移方程为 \(dp[i] = (dp[i] + 1) * \frac{i}{n} + (dp[i + 1] + 1) * \frac{n - i}{n}\)

化简得 \(dp[i] = dp[i + 1] + \frac{n}{n - i}\)

\(dp[n] = 0\),倒推即可。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10; double dp[maxn]; int main() {
int T;
scanf("%d", &T);
int kase = 0;
while(T--) {
memset(dp, 0, sizeof(dp));
int n;
scanf("%d", &n);
dp[n] = 0;
for(int i = n - 1; i >= 0; --i) {
dp[i] = dp[i + 1] + 1.0 * n / (n - i);
}
printf("Case %d: %.10lf\n", ++kase, dp[0]);
}
return 0;
}

其实是满足几何分布的。

第一个出现的面可以是 \(1, 2, 3, ..., n\),有 \(n\) 个。

如果第一个出现的面是 \(1\),那么第二个出现的面可以是 \(2, 3, ..., n\),有 \(n - 1\) 个。

...

第一个面第一次出现的概率为 \(p_1 = \frac{n}{n}\)

第二个面第一次出现的概率为 \(p_2 = \frac{n - 1}{n}\)

第三个面第一次出现的概率为 \(p_3 = \frac{n - 2}{n}\)

...

第 \(i\) 个面第一次出现的概率为 \(p_i = \frac{n - i + 1}{n}\)

几何分布的期望 \(E(X) = \frac{1}{p}\)

所以所有面至少出现一次的期望为 \(\sum_{i=1}^n \frac{1}{p_i} = \sum_{i=1}^n \frac{n}{n - i + 1}\)。

注:几何分布指在 \(n\) 次伯努利试验中,试验 \(k\) 次才得到第一次成功的机率。

#include <bits/stdc++.h>
using namespace std; int main() {
int T;
scanf("%d", &T);
int kase = 0;
while(T--) {
int n;
scanf("%d", &n);
double ans = 0.0;
for(int i = 1; i <= n; ++i) {
ans += n * 1.0 / i;
}
printf("Case %d: %.10lf\n", ++kase, ans);
}
return 0;
}

Reference

LightOJ - 1248 Dice (III) (概率期望+几何分布)

LightOJ 1248 Dice (III) (期望DP / 几何分布)的更多相关文章

  1. LightOJ - 1248 Dice (III) —— 期望

    题目链接:https://vjudge.net/problem/LightOJ-1248 1248 - Dice (III)    PDF (English) Statistics Forum Tim ...

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

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1248 题意:有一个 n 面的骰子,问至少看到所有的面一次的所需 掷骰子 的 次数的期望 ...

  3. LightOJ 1248 Dice (III) (水题,期望DP)

    题意:给出一个n面的色子,问看到每个面的投掷次数期望是多少. 析:这个题很水啊,就是他解释样例解释的太...我鄙视他,,,,, dp[i] 表示 已经看到 i 面的期望是多少,然后两种选择一种是看到新 ...

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

    学习博客:戳这里 题意:有一个 n 面的骰子,问至少看到所有的面一次的所需 掷骰子 的 次数的期望: 第一个面第一次出现的概率是p1 n/n; 第二个面第一次出现的概率是p2 (n-1)/n; 第三个 ...

  5. LightOJ 1248 Dice (III) 概率

    Description Given a dice with n sides, you have to find the expected number of times you have to thr ...

  6. LightOJ 1248 Dice (III)

    期望,$dp$. 设$dp[i]$表示当前已经出现过$i$个数字的期望次数.在这种状态下,如果再投一次,会出现两种可能,即出现了$i+1$个数字以及还是$i$个数字. 因此 $dp[i]=dp[i]* ...

  7. 1248 - Dice (III)

    1248 - Dice (III)   PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 32 MB Given ...

  8. [LOJ 1248] Dice (III)

    G - Dice (III) Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Descri ...

  9. lightoj 1248-G - Dice (III) (概率dp)

    题意:给你n个面的骰子,问扔出所有面的期望次数. 虽然这题挺简单的但还是要提一下.这题题目给出了解法. E(m)表示得到m个不同面的期望次数. E(m+1)=[((n-m)/n)*E(m)+1]+(m ...

随机推荐

  1. spring boot 尚桂谷学习笔记08 Docker ---Web

    ------Docker------ 简介:Docker是一个开元的应用容器引擎,性能非常高 已经安装好的软件打包成一个镜像放到服务器中运行镜像 MySQL容器,Redis容器...... Docke ...

  2. [BOI 2008]Elect 选举

    题目描述 N个政党要组成一个联合内阁,每个党都有自己的席位数. 现在希望你找出一种方案,你选中的党的席位数要大于总数的一半,并且联合内阁的席位数越多越好. 对于一个联合内阁,如果某个政党退出后,其它党 ...

  3. Deepin环境下安装科学研究版Python和Pytorch--防网卡

    Deepin环境下安装科学研究版Python和Pytorch--防网卡 由于本教程所引起的一切损失作者概不负责,本教程不使用pip和conda命令,因此下载好包后配合U盘可以给某个机器进行离线安装 · ...

  4. JNDI 笔记

    原理:         在DataSource中事先建立多个数据库连接,保存在数据库连接池中.当程序访问数据库时,只用从连接池中取空闲状态的数据库连接即可,访问结束,销毁资源,数据库连接重新回到连接池 ...

  5. StarUml3.10 Mac 注册key 破解

    /Applications/StarUML.app/Contents/Resources StarUML是用nodejs写的.确切的说是用Electron前端框架写的.新版本中所有的starUML源代 ...

  6. pandas认识

    import numpy as np import pandas as pd # pandas 主要是用来进行数据处理的库, # 里面不仅包含了数据处理.甚至还有 统计分析.相关计算,其内部封装了nu ...

  7. 如何使用 VLD 检测程序中的内存泄漏?

    下载地址:https://kinddragon.github.io/vld/ 下载 windows 安装包,进行安装即可,它会给你设置好 vs 的环境变量,使用时,直接在 vs ide 中包含即可. ...

  8. Centos 7安装的一些事项

    一.Wifi无法连接 ip addr 显示:unmanaged, plugin missing 先连有线网yum install -y NetworkManager-wifi systemctl re ...

  9. 十、设计模式之代理(Proxy)模式

    什么是代理模式 代理模式是对象的结构模式,为其他对象提供一种对象以控制对这个对象的访问. 代理模式的结构图如下:(源自大话设计模式)   Subject:定义了RealSubject和Proxy的公共 ...

  10. Spring整合Struts2的配置与测试

    整合目的 让Spring的IOC容器管理Struts2的Action 整合步骤 1.新建一个Web项目 2.加入Spring的jar包和添加Spring的配置文件 3.在Web.xml中配置Conte ...