SGU 495 Kids and Prizes:期望dp / 概率dp / 推公式
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=495
题意:
有n个礼物盒,m个人。
最开始每个礼物盒中都有一个礼物。
m个人依次随机选一个盒子,如果有礼物就拿走,然后放回空盒子。
问你所有人得到总礼物数的期望。
题解:
三种做法:期望dp,概率dp,推公式
一、期望dp
表示状态:
dp[i] = 该第i个人拿箱子时的总礼物的期望
找出答案:
ans = dp[m]
如何转移:
对于第i个人,拿到礼物或没拿到。
(1)φ(没拿到) = dp[i] P(没拿到) = dp[i]/n
(2)φ(拿到) = dp[i]+1 P(拿到) = (n-dp[i])/n
综上:dp[i+1] = dp[i] * dp[i]/n + (dp[i]+1) * (n-dp[i])/n
边界条件:
dp[0] = 0
还没开始拿的时候礼物数为0
二、概率dp
表示状态:
dp[i] = 第i个人拿到礼物的概率
找出答案:
ans = ∑ dp[i]
每个人得到礼物的概率 * 得到礼物的数量(为1) 之和。
如何转移:
对于第i个人,拿到礼物或没拿到。
(1)没拿到:dp[i+1]依然等于dp[i],没拿到礼物的概率为1-dp[i].
(2)拿到:dp[i+1] = dp[i] - 1/n,拿到的概率为dp[i].
综上:dp[i+1] = dp[i] * (1 - dp[i]) + (dp[i] - 1/n) * dp[i]
边界条件:
dp[0] = 1
所有盒子里都有礼物,第0个人一定拿到礼物。
三、推公式
m个人是独立的。
对于每个礼物不被人选中的概率为((n-1)/n)^m
那么不被选中的礼物数的期望就是 n*((n-1)/n)^m
所以答案就是 n-n*((n-1)/n)^m
AC Code(expectation):
// state expression:
// dp[i] = expectation
// i: considering ith person
//
// find the answer:
// ans = dp[m]
//
// transferring:
// dp[i+1] = dp[i] * dp[i]/n + (dp[i]+1) * (n-dp[i])/n
//
// boundary:
// dp[0] = 0
#include <iostream>
#include <stdio.h>
#include <string.h>
#define MAX_M 100005 using namespace std; int n,m;
double dp[MAX_M]; int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<m;i++)
{
dp[i+]=dp[i]*dp[i]/n+(dp[i]+1.0)*(n-dp[i])/n;
}
printf("%.10f\n",dp[m]);
}
AC Code(probability):
// state expression:
// dp[i] = probability
// i: ith person got a gift
//
// find the answer:
// sigma dp[i]
//
// transferring:
// dp[i+1] = dp[i] * (1 - dp[i]) + (dp[i] - 1/n) * dp[i]
//
// boundary:
// dp[0] = 1
#include <iostream>
#include <stdio.h>
#include <string.h>
#define MAX_M 100005 using namespace std; int n,m;
double ans=;
double dp[MAX_M]; int main()
{
scanf("%d%d",&n,&m);
dp[]=;
for(int i=;i<m;i++)
{
dp[i+]=dp[i]*(1.0-dp[i])+(dp[i]-1.0/n)*dp[i];
ans+=dp[i];
}
printf("%.10f\n",ans);
}
AC Code(公式):
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h> using namespace std; int n,m; int main()
{
scanf("%d%d",&n,&m);
printf("%.10f\n",n-n*pow((n-1.0)/n,m));
}
SGU 495 Kids and Prizes:期望dp / 概率dp / 推公式的更多相关文章
- sgu 495. Kids and Prizes (简单概率dp 正推求期望)
题目链接 495. Kids and Prizes Time limit per test: 0.25 second(s)Memory limit: 262144 kilobytes input: s ...
- SGU 495. Kids and Prizes( 数学期望 )
题意: N个礼品箱, 每个礼品箱内的礼品只有第一个抽到的人能拿到. M个小孩每个人依次随机抽取一个, 求送出礼品数量的期望值. 1 ≤ N, M ≤ 100, 000 挺水的说..设f(x)表示前x ...
- SGU 495. Kids and Prizes
水概率....SGU里难得的水题.... 495. Kids and Prizes Time limit per test: 0.5 second(s)Memory limit: 262144 kil ...
- 495. Kids and Prizes
http://acm.sgu.ru/problem.php?contest=0&problem=495 学习:当一条路走不通,换一种对象考虑,还有考虑对立面. 495. Kids and Pr ...
- 【整理】简单的数学期望和概率DP
数学期望 P=Σ每一种状态*对应的概率. 因为不可能枚举完所有的状态,有时也不可能枚举完,比如抛硬币,有可能一直是正面,etc.在没有接触数学期望时看到数学期望的题可能会觉得很阔怕(因为我高中就是这么 ...
- POJ2096Collecting Bugs(数学期望,概率DP)
问题: Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other material ...
- 期望与概率dp
概率与期望dp 定义: 概率:事件A发生的可能性,计作P(A) 期望:事件A结果的平均大小,记住E(x) E(x)=每种结果的大小与其概率的乘积的和 注意计算概率时需要考虑是否要用容斥原理 期望d ...
- 动态规划之经典数学期望和概率DP
起因:在一场训练赛上.有这么一题没做出来. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6829 题目大意:有三个人,他们分别有\(X,Y,Z\)块钱 ...
- 【SGU】495. Kids and Prizes
http://acm.sgu.ru/problem.php?contest=0&problem=495 题意:N个箱子M个人,初始N个箱子都有一个礼物,M个人依次等概率取一个箱子,如果有礼物则 ...
随机推荐
- imagemagick imagick
imagemagick#图像处理软件 安装解压 ./configure make make install imagick#是php图像扩展模块 调用imagemagick处理图像 安装解压/opt/ ...
- HBase中Region, store, storefile和列簇的关系
转自:http://zhb-mccoy.iteye.com/blog/1543492 The HRegionServer opens the region and creates a correspo ...
- DML过程中记录错误日志
当你插入几百万数据时,由于有几条脏数据而导致插入失败,是不是很恼火.10g R2之后有个新功能.将插入过程中失败的记录插入到还有一张表中. SQL> drop table test purge; ...
- FZU2125:简单的等式
Problem Description 如今有一个等式例如以下:x^2+s(x,m)x-n=0. 当中s(x,m)表示把x写成m进制时,每一个位数相加的和.如今,在给定n,m的情况下,求出满足等式的最 ...
- php设计模式中的类型安全 指--只接受特定的对象 ---以避免发生错误
在百度百科中---类型安全代码指访问被授权可以访问的内存位置
- Qt中的对象类型转换
char * 与 const char *的转换 char *ch1="hello11"; const char *ch2="hello22"; ch2 = c ...
- servletResponse writer输出数据
package response; import java.io.IOException;import java.io.PrintWriter; import javax.servlet.Servle ...
- 【问】Windows下C++局部变量在内存中的分布问题
原本是为了看看C++对象模型中子对象赋值给一个父对象和父类型指针指向的域时,到底会不会切割,就打开codebloks写了下面的代码,编译器选的是GNU. #define DEBUG(X) std::c ...
- Content encoding error问题解决方法
A few people have been experiencing the following error. UPDATE: The reason for it happening is beca ...
- LoadRunner hits per second 深入理解
Hits per Second Graph The Hits per Second graph shows the number of HTTP requests made by Vusers to ...