SGU495Kids and Prizes(数学期望||概率DP||公式)
495. Kids and Prizes
Memory limit: 262144 kilobytes
output: standard
ICPC (International Cardboard Producing Company) is in the business of producing cardboard boxes. Recently the company organized a contest for kids for the best design of a cardboard box and selected M winners. There are N prizes for the winners, each one carefully packed in a cardboard box (made by the ICPC, of course). The awarding process will be as follows:
- All the boxes with prizes will be stored in a separate room.
- The winners will enter the room, one at a time.
- Each winner selects one of the boxes.
- The selected box is opened by a representative of the organizing committee.
- If the box contains a prize, the winner takes it.
- If the box is empty (because the same box has already been selected by one or more previous winners), the winner will instead get a certificate printed on a sheet of excellent cardboard (made by ICPC, of course).
- Whether there is a prize or not, the box is re-sealed and returned to the room.
The management of the company would like to know how many prizes will be given by the above process. It is assumed that each winner picks a box at random and that all boxes are equally likely to be picked. Compute the mathematical expectation of the number of prizes given (the certificates are not counted as prizes, of course).
The first and only line of the input file contains the values of N and M ().
The first and only line of the output file should contain a single real number: the expected number of prizes given out. The answer is accepted as correct if either the absolute or the relative error is less than or equal to 10 -9.
sample input |
sample output |
5 7 |
3.951424 |
sample input |
sample output |
4 3 |
2.3125 |
Online Contester Team © 2002 - 2010. All rights reserved. |
InputThe first and only line of the input file contains the values of N and M ().OutputThe first and only line of the output file should contain a single real number: the expected number of prizes given out. The answer is accepted as correct if either the absolute or the relative error is less than or equal to 10 -9.Sample Input
sample input |
sample output |
5 7 |
3.951424 |
sample input |
sample output |
4 3 |
2.3125 |
题意:
有n个奖品,m个人排队来选礼物,对于每个人,他打开的盒子,可能有礼物,也有可能已经被之前的人取走了。为最后m个人取走礼物的期望。
思路:
排队取,第1个人取到1个,dp[1]=1;后面的人dp[i]=p取到礼物盒子+dp取到礼物=(n-dp[i-1])/n + dp[i-1];
当然,也可以化简为公式 printf("%.10lf\n",n*1.0*(1-pow((n-1)*1.0/n,m)));
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<algorithm>
using namespace std;
double dp[];
int main()
{
int n,m,i;
while(~scanf("%d%d",&n,&m)){
dp[]=;
for(i=;i<=m;i++) dp[i]=dp[i-]+(n-dp[i-])/n;
printf("%.9lf\n",dp[m]);
}return ;
}
SGU495Kids and Prizes(数学期望||概率DP||公式)的更多相关文章
- UVa 11427 Expect the Expected (数学期望 + 概率DP)
题意:某个人每天晚上都玩游戏,如果第一次就䊨了就高兴的去睡觉了,否则就继续直到赢的局数的比例严格大于 p,并且他每局获胜的概率也是 p,但是你最玩 n 局,但是如果比例一直超不过 p 的话,你将不高兴 ...
- ZOJ3640Help Me Escape(师傅逃亡系列•一)(数学期望||概率DP)
Background If thou doest well, shalt thou not be accepted? and if thou doest not well, sin lieth at ...
- POJ3682King Arthur's Birthday Celebration(数学期望||概率DP)
King Arthur is an narcissist who intends to spare no coins to celebrate his coming K-th birthday. Th ...
- SGU495Kids and Prizes 数学期望
题意: 有n个奖品,m个人排队来选礼物,对于每个人,他打开的盒子,可能有礼物,也有可能已经被之前的人取走了,然后把盒子放回原处.为最后m个人取走礼物的期望. 题解: 本道题与之前的一些期望 DP 题目 ...
- HDU 3853 期望概率DP
期望概率DP简单题 从[1,1]点走到[r,c]点,每走一步的代价为2 给出每一个点走相邻位置的概率,共3中方向,不动: [x,y]->[x][y]=p[x][y][0] , 右移:[x][y ...
- 【BZOJ 3652】大新闻 数位dp+期望概率dp
并不难,只是和期望概率dp结合了一下.稍作推断就可以发现加密与不加密是两个互相独立的问题,这个时候我们分开算就好了.对于加密,我们按位统计和就好了;对于不加密,我们先假设所有数都找到了他能找到的最好的 ...
- 【BZOJ 3811】玛里苟斯 大力观察+期望概率dp+线性基
大力观察:I.从输出精准位数的约束来观察,一定会有猫腻,然后仔细想一想,就会发现输出的时候小数点后面不是.5就是没有 II.从最后答案小于2^63可以看出当k大于等于3的时候就可以直接搜索了 期望概率 ...
- 【NOIP模拟赛】黑红树 期望概率dp
这是一道比较水的期望概率dp但是考场想歪了.......我们可以发现奇数一定是不能掉下来的,因为若奇数掉下来那么上一次偶数一定不会好好待着,那么我们考虑,一个点掉下来一定是有h/2-1个红(黑),h/ ...
- BZOJ1415: [Noi2005]聪聪和可可 最短路 期望概率dp
首先这道题让我回忆了一下最短路算法,所以我在此做一个总结: 带权: Floyed:O(n3) SPFA:O(n+m),这是平均复杂度实际上为O(玄学) Dijkstra:O(n+2m),堆优化以后 因 ...
随机推荐
- 秒懂算法1——冒泡排序,及一种小改进(C#实现)
算法思路: 重复走访每两个相邻元素,比较大小交换位置,直至排序完成. 有兴趣电话可以看一下这个[冒泡排序踢踏舞]的视频,很形象的演示了排序过程,额呵呵~~ 性质: 冒泡排序是一种原地排序(只有常数个元 ...
- Java并发之synchronized深入
一句话总结synchronized: JVM会自动通过使用monitor来加锁和解锁,保证了同时只有一个线程可以执行指定代码,从而保证了线程安全,同时具有可重入和不可中断的性质. 一.synchron ...
- 【Linux】结合Python 简易实现监控公司网站,邮件发送异常
背景 由于一些原因,博主负责测试的网站的服务器切换到了香港,切换后出现了多次访问超时的情况 于是主动请缨写一个自动监测的脚本,本来准备完全使用shell来写,后来发现shell发送邮件只能在测试机之间 ...
- CSP(Content Security Policy) 入门教程
参考: http://www.ruanyifeng.com/blog/2016/09/csp.html https://developer.mozilla.org/en-US/docs/Web/HTT ...
- [spring mvc]Hello World入门
1.新建项目 File->New->Other,选择Dynamic web project: 项目建好之后,目录结构如下: 2.WEB-INF/web.xml 中配置 dispatcher ...
- Spring自定义注解扫描的实现
目标:实现自定义spring自动扫描注解.主要为后期实现分布式服务框架自动注解提供技术支持 技术分析:通过配置组件扫描标签使spring解析标签. 1. JewelScanBeanDefaultPar ...
- UVALive-3713 Astronauts (2-SAT)
题目大意:有三个任务A.B.C,n个已知年龄的人.A任务只能被年龄不小于平均年龄的人做,B任务只能被平均年龄以下的人做,C任务不限,相互讨厌的两个人不能做同一件任务,现在已知厌恶关系,求一种任务分配方 ...
- JQuery数字类型验证正则表达式
有朋友整了一些关于js与jquery的数字类型验证正则表达式代码,下面我给大家再整理一下. 这里包括了数字验证实现与测试实例了,大家可参考. js验证数字正则表达式 代码如下: //检测是否为数字和小 ...
- 在写一个iOS应用之前必须做的7件事(附相关资源)
本文由CocoaChina--不再犹豫(tao200610704@126.com)翻译 作者:@NIkant Vohra 原文:7 Things you must absolutely do befo ...
- 应用Fiddler对手机应用来抓包
Fiddler是一款非常流行并且实用的http抓包工具,它的原理是在本机开启了一个http的代理服务器,然后它会转发所有的http请求和响应,因此,它比一般的firebug或者是chrome自带的抓包 ...