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. Javaweb连接数据库

    在JSP中使用JDBC驱动连接mysql数据库. 1: 下载mysql的Java连接程序 2: 解压目录下的mysql-connector-java-5.0.24-bin.jar文件就是连接MySql ...

  2. select 中添加option的注意

    在平时写JS中经常要给Select添加option,如果我们把option中的数据用一个字符串来表示: eg: var strOption='<option>1</option> ...

  3. 【Linux】svn环境配置

    Ubuntu 安装svn环境配置 1. 安装 sudo apt-get install subversion 安装过程需要数据[Y] 2. svn位置选择 安装完成之后,选择svn目录位置, 将其放在 ...

  4. chnagyong sql

    select gid,count(distinct mid) from members group by gid mysql> SELECT IFNULL(NULL,); mysql> 1 ...

  5. Java HashMap,LinkedHashMap,TreeMap

    Java为数据结构中的映射定义了一个接口java.util.Map;它有四个实现类,分别是HashMap Hashtable LinkedHashMap 和TreeMapMap主要用于存储健值对,根据 ...

  6. 安卓机在按HOME键时,UNITY触发的APPLICATION_PAUSE事件

    安卓机在按HOME键时,UNITY触发的APPLICATION_PAUSE事件 此时安卓程序会返回,在这一瞬间,程序可以通过SOCKET发送数据包给服务器告知, 经测试在这短暂的时间内,这个数据包能发 ...

  7. LeetCode包括main函数的答题框架(Java+Eclipse)

    http://zhangnai.xin/2016/09/20/LeetCode-Framework/ 目录结构: LeetCode ——项目名称,方便Eclipse内置Git对代码进行管理和多终端同步 ...

  8. C++ 输入cin 和输出cout

    C++输入cout与输出cin 输入和输出并不是C++语言中的正式组成成分.C和C++本身都没有为输入和输出提供专门的语句结构.输入输出不是由C++本身定义的,而是在编译系统提供的I/O库中定义的. ...

  9. php不重新编译,安装未安装过的扩展,如curl扩展

    假设我们的之前的php安装于/data/php下. 1.找到之前安装的PHP源码包,把它重新解压出来,进入到要安装的扩展目录. > cd /data/php-5.6.11/ext/curl (* ...

  10. C++的MFC 与 HTML 双向通讯

    C++中嵌入ie浏览器总结(1) - ie边框 及上下文菜单 最近项目中用html 来做界面,也就折腾了一下在wxwidget中嵌入浏览器的若干细节工作,mfc也基本是类似的,由于wxwidget中已 ...