SGU-495 Kids and Prizes(概率DP)
来源:互联网 发布:js正则判断数字范围 编辑:程序博客网 时间:2024/05/22 17:38
495. Kids and Prizes
http://acm.sgu.ru/problem.php?contest=0&problem=495
Time limit per test: 0.25 second(s)
Memory limit: 262144 kilobytes
Memory limit: 262144 kilobytes
input: standard
output: standard
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.
Input
The first and only line of the input file contains the values of N and M ().Output
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.Example(s)
sample input
sample output
5 7
3.951424
sample input
sample output
4 3
2.3125
题目大意:有m个人和n个装有奖品的盒子,每个人随机取走一个盒子,若存在奖品则取走,然后放回,否则直接放回,求拿走奖品的个数的期望?
方法一:概率DP (O(m))
最终期望是每个人拿到奖品的概率之和,考虑同时只存在一个人拿到盒子
第i个人拿到奖品的概率与前一个人拿到奖品的概率有关,所以设dp[i]表示第i个拿盒子的人拿到奖品的概率
则dp[i]可由两个状态转移而来:
①第i-1个拿盒子的人拿到奖品,概率为dp[i-1],则第i个拿盒子的人拿到奖品的概率为:dp[i-1]-1/n
②第i-1个拿盒子的人没有拿到奖品,概率为1-dp[i-1],则第i个拿盒子的人拿到奖品的概率为:dp[i-1]
可得状态转移方程为:dp[i]=(1-dp[i-1])*dp[i-1]+dp[i-1]*(dp[i-1]-1/n);
#include <cstdio>using namespace std;int n,m;double dp[100005],nn,ans;int main() { while(2==scanf("%d%d",&n,&m)) { dp[1]=ans=1; nn=1.0/n; for(int i=2;i<=m;++i) { dp[i]=(1-dp[i-1])*dp[i-1]+dp[i-1]*(dp[i-1]-nn); ans+=dp[i]; } printf("%.9lf\n",ans); } return 0;}
方法二:二项分布 (O(1))
又看到一种O(1)的算法,但是其成立的前提是m个人没有拿取到某个奖品的是相互独立的(感觉不同时取的话,第一个人必定会取到一个奖品,则后面的人取到该奖品的概率就为0了)
对于每个奖品来说,不被拿走的概率为:(n-1)/n,则不被所有人拿走的期望为:((n-1)/n)^m
则:奖品不被拿走的期望为:n*((n-1)/n)^m,所以奖品被拿走的期望为:n-n*((n-1)/n)^m
#include <cstdio>#include <cmath>using namespace std;int n,m;int main() { while(2==scanf("%d%d",&n,&m)) { printf("%.9lf\n",n-n*pow(1.0-1.0/n,m)); } return 0;}
0 0
- SGU 495 Kids and Prizes(概率dp)
- Sgu 495 Kids and Prizes (概率dp)
- sgu 495 Kids and Prizes 概率dp
- SGU 495 Kids and Prizes 概率dp
- SGU 495 Kids and Prizes(概率dp)
- [概率dp] sgu 495 Kids and Prizes
- SGU 495 Kids and Prizes (概率DP)
- SGU 495 Kids and Prizes (概率DP)
- SGU-495 Kids and Prizes(概率DP)
- SGU 495 Kids and Prizes (概率期望DP)
- SGU 495 Kids and Prizes(概率)
- SGU 495. Kids and Prizes(概率dp)
- SGU 495 Kids and Prizes (概率DP入门)
- sgu 495 Kids and Prizes 求概率
- SGU 495 Kids and Prizes 概率 二项分布
- [ACM] SGU 495 Kids and Prizes (概率,期望,二项分布)
- SGU 495 Kids and prizes(概率题,二项分布)
- SGU 495 Kids and Prizes(期望DP || 数学规律)
- Error running app: Instant Run requires 'Tools | Android | Enable ADB integration' to be enable
- Redis+Spring缓存实例(windows环境,附实例源码及详解)
- 六、树和二叉树--(0)什么是树
- 安装并运行Elasticsearch
- 百练+BFS+图上visit标记嘛,因为是最短嘛,防止重复嘛
- SGU-495 Kids and Prizes(概率DP)
- leetcode——142——Linked List Cycle II
- 【DP】探索数字迷塔
- linux 硬盘分区,分区,删除分区,格式化,挂载,卸载笔记
- Android手机客户端与服务器端的信息通信交互
- yaf框架控制器中的几个函数
- visio2013产品密钥
- BZoj 1016: [JSOI2008]最小生成树计数【最小生成树】
- 城市中的原始人(二):本能大脑