sgu 495. Kids 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 |
题意:
有n个奖品放在n个盒子,进行m次选择,每次只能选则一个盒子,如果选到含有奖品的话就把盒子里奖品拿走,盒子始终仍留着。问最终得到到奖品数的期望值。
分析:
一看见求期望想逆推,但是发现逆推没办法写初始条件,这是因为取的次数不少无限的,
只能取m次。
d[i]代表到第i个人取到奖品数的期望,显然d[i]等于上一个人取到的奖品数加上这个人取到的奖品数,
这个人取到的奖品数为1的概率为(n-d[i-1])/n,取到奖品数为0的概率为d[i-1]/n,即期望为
d[i-1]+1*(n-d[i-1])/n+0*d[i-1]/n。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <cmath>
#include <algorithm>
#define LL __int64
const int maxn = 1e5+;
using namespace std;
double d[maxn]; int main()
{
int n, m, i, j;
while(~scanf("%d%d", &n, &m))
{
d[] = ;
for(i = ; i <= m; i++)
d[i] = d[i-]+1.0*((double)n-d[i-])/(double)n;
printf("%.9lf\n", d[m]);
}
return ;
}
sgu 495. Kids and Prizes (简单概率dp 正推求期望)的更多相关文章
- hdu 3853 LOOPS (概率dp 逆推求期望)
题目链接 LOOPS Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others)Tota ...
- SGU 495 Kids and Prizes:期望dp / 概率dp / 推公式
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=495 题意: 有n个礼物盒,m个人. 最开始每个礼物盒中都有一个礼物. m个人依次随 ...
- SGU 495. Kids and Prizes
水概率....SGU里难得的水题.... 495. Kids and Prizes Time limit per test: 0.5 second(s)Memory limit: 262144 kil ...
- SGU 495. Kids and Prizes( 数学期望 )
题意: N个礼品箱, 每个礼品箱内的礼品只有第一个抽到的人能拿到. M个小孩每个人依次随机抽取一个, 求送出礼品数量的期望值. 1 ≤ N, M ≤ 100, 000 挺水的说..设f(x)表示前x ...
- hdu 4336 Card Collector (概率dp+位运算 求期望)
题目链接 Card Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- Aeroplane chess(简单概率dp)
Hzz loves aeroplane chess very much. The chess map contains N+1 grids labeled from 0 to N. Hzz start ...
- 495. Kids and Prizes
http://acm.sgu.ru/problem.php?contest=0&problem=495 学习:当一条路走不通,换一种对象考虑,还有考虑对立面. 495. Kids and Pr ...
- HDU 3853LOOPS(简单概率DP)
HDU 3853 LOOPS 题目大意是说人现在在1,1,需要走到N,N,每次有p1的可能在元位置不变,p2的可能走到右边一格,有p3的可能走到下面一格,问从起点走到终点的期望值 这是弱菜做的第 ...
- hdu 4576(简单概率dp | 矩阵优化)
艰难的一道题,体现出菜菜的我... 首先,先吐槽下. 这题到底出题人是怎么想的,用普通概率dp水过??? 那为什么我概率dp写的稍微烂点就一直tle? 感觉很不公平.大家算法都一致,因为我程序没有那 ...
随机推荐
- 关于JS APP
多屏screen, JS如何路由,如何换页,导航.通过JS来实现. 当前页面的逻辑通过JS来实现.HTML DOM, Event, Widget. 核心在于function. JS 不仅仅是DOM, ...
- EditorWindow edit ScriptableObject
using UnityEngine; [System.Serializable] public class Weapon { //[SerializeField] public string weap ...
- mysql去除重复查询的SQL语句基本思路
SELECT R.* FROM trans_flow R, (SELECT order_no, MAX(status_time) AS status_time FROM trans_flow GROU ...
- 表单很多数据项录入的时候,提交controller发生异常,数据回显。
1.添加的情况(Model传递Form Data) request.getSession().setAttribute("car", car); //抛出异常的时候,数据回显. 2 ...
- PHP 中 Date 函数与实际时间相差8小时的解决方法
PHP 中的 date() 函数显示的时间是格林威治时间,和北京时间正好相差8个小时,其他时间相关的函数,如 strtotime() 也有相同的问题,同样可以通过下面的方法来解决: 1. 修改php. ...
- 真机模拟器.a文件编译报错
- AsyncTask和Handler对比
AsyncTask和Handler对比 1 ) AsyncTask实现的原理,和适用的优缺点 AsyncTask,是android提供的轻量级的异步类,可以直接继承AsyncTask,在类中实现异步操 ...
- 离开csdn来到blog园
csdn里没有限制阅读访问的功能,所以我选择来到cnblog 但是不得不说,cnblog做的界面很丑,我个人很不喜欢,但是没办法
- Linux网络编程7——使用TCP实现双方聊天
思路 主线程负责发送消息,另一线程负责接收消息.服务端和客户端均是如此. 注意 当A方close掉用于通信的socket端口后,该端口是不会立即关闭的.因为此时可能B方的信息还没send完.因此,此时 ...
- 华为上机:Tom的生日礼物
Tom的生日礼物 描述: 四月一日快到了,Tom想了个愚人的好办法——送礼物.嘿嘿,不要想的太好,这礼物可没那么简单,Tom为了愚人,准备了一堆盒子,其中有一个盒子里面装了礼物.盒子里面可以再放零个或 ...