B. Maxim and Restaurant
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Maxim has opened his own restaurant! The restaurant has got a huge table, the table's length is p meters.

Maxim has got a dinner party tonight, n guests will come to him. Let's index the guests of Maxim's restaurant from 1 to n. Maxim knows the sizes of all guests that are going to come to him. The i-th guest's size (ai) represents the number of meters the guest is going to take up if he sits at the restaurant table.

Long before the dinner, the guests line up in a queue in front of the restaurant in some order. Then Maxim lets the guests in, one by one. Maxim stops letting the guests in when there is no place at the restaurant table for another guest in the queue. There is no place at the restaurant table for another guest in the queue, if the sum of sizes of all guests in the restaurant plus the size of this guest from the queue is larger than p. In this case, not to offend the guest who has no place at the table, Maxim doesn't let any other guest in the restaurant, even if one of the following guests in the queue would have fit in at the table.

Maxim is now wondering, what is the average number of visitors who have come to the restaurant for all possible n! orders of guests in the queue. Help Maxim, calculate this number.

Input

The first line contains integer n (1 ≤ n ≤ 50) — the number of guests in the restaurant. The next line contains integers a1, a2, ..., an(1 ≤ ai ≤ 50) — the guests' sizes in meters. The third line contains integer p (1 ≤ p ≤ 50) — the table's length in meters.

The numbers in the lines are separated by single spaces.

Output

In a single line print a real number — the answer to the problem. The answer will be considered correct, if the absolute or relative error doesn't exceed 10 - 4.

Examples
input

Copy
3
1 2 3
3
output

Copy
1.3333333333
Note

In the first sample the people will come in the following orders:

  • (1, 2, 3) — there will be two people in the restaurant;
  • (1, 3, 2) — there will be one person in the restaurant;
  • (2, 1, 3) — there will be two people in the restaurant;
  • (2, 3, 1) — there will be one person in the restaurant;
  • (3, 1, 2) — there will be one person in the restaurant;
  • (3, 2, 1) — there will be one person in the restaurant.

In total we get (2 + 1 + 2 + 1 + 1 + 1) / 6 = 8 / 6 = 1.(3).

题解:

令dp[i][j][k]表示前i个取j个和为k的期望

考虑转移到i的时候,无非是a[i]取或者不取

取的次数占总次数的比为c(i-1,j-1)/c(i,j)=j/i

所以不取的占比为1-j/i=(i-j)/i

故得到转移方程

dp[i][j][k]=j/i*dp[i-1][j-1][k-a[i]]+(i-j)/i*dp[i-1][j][k]

代码如下:

#include<bits/stdc++.h>
using namespace std; double dp[][][];
double fac[];
int n,p,a[]; int main()
{
dp[][][]=1.0;
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
scanf("%d",&p);
for(int i=;i<=n;i++)
{
for(int j=;j<=i;j++)
{
for(int k=;k<=p;k++)
{
dp[i][j][k]+=dp[i-][j][k]*(i-j)/i;
if(k-a[i]>=)
{
dp[i][j][k]+=dp[i-][j-][k-a[i]]*j/i;
}
}
}
}
double ans=0.0;
for(int i=;i<=n;i++)
{
for(int j=;j<=p;j++)
{
ans+=dp[n][i][j];
}
}
printf("%.5lf\n",ans);
}

codeforces 261B Maxim and Restaurant(概率DP)的更多相关文章

  1. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  2. CodeForces 261B Maxim and Restaurant 解法汇总

    题意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a3-+ai,问满足Si<=p的i的最大值的期望.(p<=50) 这道题在网上有一些不同 ...

  3. CodeForces 540D--Bad Luck Island(概率DP)

    貌似竟然是我的第一道概率DP.. 手机码代码真不舒服.... /************************************************ Memory: 67248 KB Ti ...

  4. codeforces 148D Bag of mice(概率dp)

    题意:给你w个白色小鼠和b个黑色小鼠,把他们放到袋子里,princess先取,dragon后取,princess取的时候从剩下的当当中任意取一个,dragon取得时候也是从剩下的时候任取一个,但是取完 ...

  5. CodeForces 24D Broken robot (概率DP)

    D. Broken robot time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  6. 【CodeForces 261B】Maxim and Restaurant(DP,期望)

    题目链接 第一种解法是$O(n^3*p)$的:f[i][j][k]表示前i个人进j个人长度为k有几种方案(排列固定为123..n时).$f[i][j][k]=f[i-1][j][k]+f[i-1][j ...

  7. Codeforces 148D Bag of mice 概率dp(水

    题目链接:http://codeforces.com/problemset/problem/148/D 题意: 原来袋子里有w仅仅白鼠和b仅仅黑鼠 龙和王妃轮流从袋子里抓老鼠. 谁先抓到白色老师谁就赢 ...

  8. CodeForces 148D-Bag of mice(概率dp)

    题意: 袋子里有w个白球b个黑球,现在两个人轮流每次取一个球(不放回),先取到白球的获胜,当后手取走一个球时,袋子里的球会随机的漏掉一个,问先手获胜的概率. 分析: dp[i][j]表示袋子中i个白球 ...

  9. Codeforces 1151F Sonya and Informatics (概率dp)

    大意: 给定01序列, 求随机交换k次后, 序列升序的概率. 假设一共$tot$个$0$, 设交换$i$次后前$tot$个数中有$j$个$0$的方案数为$dp[i][j]$, 答案即为$\frac{d ...

随机推荐

  1. 集成学习算法总结----Boosting和Bagging(转)

    1.集成学习概述 1.1 集成学习概述 集成学习在机器学习算法中具有较高的准去率,不足之处就是模型的训练过程可能比较复杂,效率不是很高.目前接触较多的集成学习主要有2种:基于Boosting的和基于B ...

  2. jquery 判断checkbox是否被选中问题

    1.jquery库2以上 $("#checkbox_check").click(function(){ alert($(this).prop("checked" ...

  3. Promise/Deferred

    [fydisk] 1.$.get('/api').success(onSuccess).error(onError).comlete(onComplete); 2.对同一事件加入多个Handler. ...

  4. Web开发: servlet的session为null?

    servlet的session(会话)显示为null,一般是web.xml中配置不对或者在浏览器输入的url不正确造成的. web.xml配置如下: <servlet> <servl ...

  5. Chip Factory(01字典树)

    Chip Factory http://acm.hdu.edu.cn/showproblem.php?pid=5536 Time Limit: 18000/9000 MS (Java/Others)  ...

  6. maven不存在jar包解决

    win7环境 下载:https://maven.apache.org/download.cgi 提取文件,并cmd 转到bin目录 假设要添加的jar包是jbarcode-0.2.8.jar, 可执行 ...

  7. 349. Intersection of Two Arrays 是否重复

    不重复的: 方法一 [一句话思路]:排序之后用归并. [一刷]: 根据返回方法的类型来判断corner case.判断空.0数组的corner case还不一样,得分开写 由于先排序了,nums1[i ...

  8. Notepad++正则表达式格式 Editplus使用正则表达式[转]

          使用正则表达式可以很好地完成很多繁琐耗时的工作,以下抄录editplus正则表达式的使用,同样适用于notepad++:表达式 说明 \t 制表符. \n 新行. . 匹配任意字符. | ...

  9. jquery 处理重新绑定插件的方法

    比如有一个slide的jquery插件,页面打开就对dom进行了绑定. <div class="expert"> <div class="expert- ...

  10. discuz模板引擎语法

    论坛的首页模板:forum/discuz.htm 版块的内容模板:forum/forumdisplay.htm 主题的查看模板:forum/viewthread.htm 帖子的内容模板:forum/p ...