概率dp sgu495

Kids and Prizes

Time Limit:250MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Submit Status Practice SGU
495

Appoint description: 
System Crawler  (2014-10-25)

Description

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).

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.

Sample Input

sample input
sample output
5 7
3.951424
sample input
sample output
4 3
2.3125

题意:有n件奖品装在n个箱子里,M个人选择其中的一个箱子,每个人取出一个箱子,如果有奖品取出,后将箱子放回,后面的人继续取箱子.问最终取出箱子个数的期望。

另dp[i]表示第i个人拿到奖品的概率则,dp[1]=1;若i-1取到了奖品则i获得奖品的概率为dp[i-1]-1/n,否则i得到奖品的概率为dp[i-1].转移方程为dp[i]=dp[i-1]*(dp[i-1]-1/n)+(1-dp[i-1])*dp[i-1].、

/*************************************************************************
    > File Name: t.cpp
    > Author: acvcla
    > Mail: [email protected]
    > Created Time: 2014年10月21日 星期二 21时33分55秒
 ************************************************************************/
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<cstring>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<cstdlib>
#include<ctime>
#include<set>
#include<math.h>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 10;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
int n,m,f;
double dp[maxn];
int main(int argc, char const *argv[])
{
	while(~scanf("%d%d",&n,&m)){
		memset(dp,0,sizeof dp);
		dp[1]=1;
		double ans=1;
		for(int i=2;i<=m;i++){
			dp[i]=dp[i-1]*(dp[i-1]-1.0/n)+(1-dp[i-1])*dp[i-1];
			ans+=dp[i];
		}
		printf("%.10f\n",ans);
	}
	return 0;
}
时间: 2024-07-30 13:49:09

概率dp sgu495的相关文章

sgu495:概率dp / 推公式

概率题..可以dp也可以推公式 抽象出来的题目大意: 有 n个小球,有放回的取m次  问 被取出来过的小球的个数的期望 dp维护两个状态 第 i 次取出的是 没有被取出来过的小球的 概率dp[i] 和取出的是已经被取出来过的小球的概率np[i]; 如果第 i-1 次取出的是已经被取出来过的小球 那么第 i 次取出没有取出来过小球的概率即为 dp[i-1]: 反之则为 dp[i-1] - 1/n(没有取出来过的小球少了一个) 所以可以得到状态转移方程 dp[i]=dp[i-1]*(dp[i-1]-

SGU495 Kids and Prizes 概率DP,期望公式

题目 这题目首先进去以后,没地方提交,第一次做SGU的题目,只能在HUSTOJ上提交了 有n个盒子,里面有礼物的,m个人,每个人拿,拿过以后 把礼物取出来 把盒子放回去,求选中礼物数的期望 其实一开始就假设方程 dp[i]为 第i个人获得礼物的概率,但是状态转移方程不知道该怎么办,想了很久都没有办法, 其实首先边界为dp[1] = 1 第一个上来选的人肯定必中 接下来一个人的 则由两部分组成,dp[i] = (1 - dp[i - 1]) * dp[i - 1] + .........,因为上一

Codeforces 28C [概率DP]

/* 大连热身D题 题意: 有n个人,m个浴室每个浴室有ai个喷头,每个人等概率得选择一个浴室. 每个浴室的人都在喷头前边排队,而且每个浴室内保证大家都尽可能均匀得在喷头后边排队. 求所有浴室中最长队伍的期望. 思路: 概率dp dp[i][j][k]代表前i个浴室有j个人最长队伍是k的概率. 枚举第i个浴室的人数.然后转移的时候其实是一个二项分布. */ #include<bits/stdc++.h> using namespace std; int jilu[55]; double dp[

hdu 3076 ssworld VS DDD (概率dp)

///题意: /// A,B掷骰子,对于每一次点数大者胜,平为和,A先胜了m次A赢,B先胜了n次B赢. ///p1表示a赢,p2表示b赢,p=1-p1-p2表示平局 ///a赢得概率 比一次p1 两次p0*p1 三次 p0^2*p1,即A赢的概率为p1+p*p1+p^2*p1+...p^n*p1,n->无穷 ///即a_win=p1/(1-p);b_win=p2/(1-p); ///dp[i][j]表示a赢了j次,b赢了i次的概率 ///dp[i][j]=dp[i-1][j]*b_win+dp[

hdu 3853 概率DP 简单

http://acm.hdu.edu.cn/showproblem.php?pid=3853 题意:有R*C个格子,一个家伙要从(0,0)走到(R-1,C-1) 每次只有三次方向,分别是不动,向下,向右,告诉你这三个方向的概率,以及每走一步需要耗费两个能量,问你走到终点所需要耗费能量的数学期望: 回头再推次,思想跟以前的做过的类似 注意点:分母为0的处理 #include <cstdio> #include <cstring> #include <algorithm>

hdu4089(公式推导)概率dp

题意:有n人都是仙剑5的fans,现在要在官网上激活游戏,n个人排成一个队列(其中主角Tomato最初排名为m), 对于队列中的第一个人,在激活的时候有以下五种情况: 1.激活失败:留在队列中继续等待下一次激活(概率p1) 2.失去连接:激活失败,并且出队列然后排到队列的尾部(概率p2) 3.激活成功:出队列(概率p3) 4.服务器瘫:服务器停止服务了,所有人都无法激活了(概率p4) 求服务器瘫痪并且此时Tomato的排名<=k的概率. 解法:ans[i][j]表示i个人出于第j个位置要到目的状

poj3071(概率DP)

题意:淘汰赛制,2^n(n<=7)个队员.给出相互PK的输赢概率矩阵.问谁最有可能赢到最后. 解法:ans[i][j]表示第i个队员第j轮胜出的概率.赢到最后需要进行n场比赛.算出每个人赢到最后的ans[i][n].写出序号的二进制发现一个规律,两个队员i.j如果碰到,那么一定是在第get(i,j)场比赛碰到的.get(i,j)计算的是i和j二进制不同的最高位,这个规律也比较明显. 代码: /****************************************************

【Foreign】开锁 [概率DP]

开锁 Time Limit: 10 Sec  Memory Limit: 256 MB Description Input Output Sample Input 4 5 1 2 5 4 3 1 5 2 2 5 4 3 1 5 3 2 5 4 3 1 5 4 2 5 4 3 1 Sample Output 0.000000000 0.600000000 0.900000000 1.000000000 HINT Main idea 一个宝箱内有一个可以开启别的宝箱的钥匙,可以选择k个宝箱,询问能开

POJ 2151 Check the difficulty of problems (概率DP)

题意:ACM比赛中,共M道题,T个队,pij表示第i队解出第j题的概率 ,求每队至少解出一题且冠军队至少解出N道题的概率. 析:概率DP,dp[i][j][k] 表示第 i 个队伍,前 j 个题,解出 k 个题的概率,sum[i][j] 表示第 i 个队伍,做出 1-j 个题的概率,ans1等于, T个队伍,至少解出一个题的概率,ans2 表示T个队伍,至少解出一个题,但不超过N-1个题的概率,最后用ans1-ans2即可. 代码如下: #pragma comment(linker, "/STA