LightOJ 1248 Dice (III) (期望DP / 几何分布)
题目链接: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) (期望DP / 几何分布)的更多相关文章
- LightOJ - 1248 Dice (III) —— 期望
题目链接:https://vjudge.net/problem/LightOJ-1248 1248 - Dice (III) PDF (English) Statistics Forum Tim ...
- LightOj 1248 - Dice (III)(几何分布+期望)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1248 题意:有一个 n 面的骰子,问至少看到所有的面一次的所需 掷骰子 的 次数的期望 ...
- LightOJ 1248 Dice (III) (水题,期望DP)
题意:给出一个n面的色子,问看到每个面的投掷次数期望是多少. 析:这个题很水啊,就是他解释样例解释的太...我鄙视他,,,,, dp[i] 表示 已经看到 i 面的期望是多少,然后两种选择一种是看到新 ...
- 【非原创】LightOj 1248 - Dice (III)【几何分布+期望】
学习博客:戳这里 题意:有一个 n 面的骰子,问至少看到所有的面一次的所需 掷骰子 的 次数的期望: 第一个面第一次出现的概率是p1 n/n; 第二个面第一次出现的概率是p2 (n-1)/n; 第三个 ...
- LightOJ 1248 Dice (III) 概率
Description Given a dice with n sides, you have to find the expected number of times you have to thr ...
- LightOJ 1248 Dice (III)
期望,$dp$. 设$dp[i]$表示当前已经出现过$i$个数字的期望次数.在这种状态下,如果再投一次,会出现两种可能,即出现了$i+1$个数字以及还是$i$个数字. 因此 $dp[i]=dp[i]* ...
- 1248 - Dice (III)
1248 - Dice (III) PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 32 MB Given ...
- [LOJ 1248] Dice (III)
G - Dice (III) Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Descri ...
- lightoj 1248-G - Dice (III) (概率dp)
题意:给你n个面的骰子,问扔出所有面的期望次数. 虽然这题挺简单的但还是要提一下.这题题目给出了解法. E(m)表示得到m个不同面的期望次数. E(m+1)=[((n-m)/n)*E(m)+1]+(m ...
随机推荐
- Python中生成器和yield语句的用法详解
Python中生成器和yield语句的用法详解 在开始课程之前,我要求学生们填写一份调查表,这个调查表反映了它们对Python中一些概念的理解情况.一些话题("if/else控制流" ...
- 转 cpu高 问题分析定位
文章来源: http://www.blogjava.net/hankchen/archive/2012/08/09/377735.html 一个应用占用CPU很高,除了确实是计算密集型应用之外,通常原 ...
- HDU 1387 Team Queue( 单向链表 )
Team Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- HDU 1398 Square Coins(DP)
Square Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- 如何在嵌套的app中运用vue去写单页面H5
本文主要介绍移动端.为了避免移动端兼容出现各种奇奇怪怪的bug,所以秉承着能不用复杂的语法就不用,尽量用最基础的语法. 可用惯了各种ES6语法的童鞋们,写原生真是头疼,再加上各种领导催工期,肯定是内心 ...
- 消息 245,级别 16,状态 1,第 1 行 在将 varchar 值 '2,8' 转换成数据类型 int 时失败。
错误问题: 消息 245,级别 16,状态 1,第 1 行在将 varchar 值 '2,8' 转换成数据类型 int 时失败. ps: 这是在后台分配菜单权限这个功能时出现的问题 一,解决方法: 将 ...
- 华为要卖5G技术,虽然我和华为没有一点关系,但是我也很呵呵
http://www.sohu.com/a/340555529_166680 老任头,竟然说出了这样的话,要卖5G技术给西方,然后塑造对手. 按照老任头的脾气,老任头应该不至于胡说八道这样的话,但是呢 ...
- 【学习总结】Python-3-多个变量赋值
菜鸟教程-Python3-基本数据类型 同时为多个变量赋值的两种格式: 连等:看起来可能错误但事实上Python可以这样的.... 一团变量对应一团值:比较常见又省事的格式 END
- 常看 Shell: 文本文件操作
文件显示和信息 wc wc 可以用于统计文件的行数和单词数. nl nl 在文件的每行内容前面加上行号. 基于行的操作 grep grep 用于筛选匹配特定字符的行. grep "Hello ...
- testNG 并发测试
invocationCount是并发数,threadPoolSize是线程数,当线程是1的时候就是依次执行n次,当线程是并发次数时,就是同时执行n次 @Test public void abc ...