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. iBatis与Hibernate有什么不同?

    相同点:屏蔽jdbc api的底层访问细节,使用我们不用与jdbc api打交道,就可以访问数据. jdbc api编程流程固定,还将sql语句与java代码混杂在了一起,经常需要拼凑sql语句,细节 ...

  2. struts2 模拟令牌机制防止表单重复提交

    web.xml: <?xml version="1.0" encoding="UTF-8"?><web-app version="3 ...

  3. 清理数据库errorlog

    C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\LOGERRORLOGERRORLOG.1ERRORLOG.2ERRORLOG.3ERRORLO ...

  4. go语言中通过http访问需要认证的api

    func main() { //生成client 参数为默认 client := &http.Client{} //生成要访问的url url := "https://api.XXX ...

  5. 慕课网access_token的获取(下)

    接口调用返回的结果是两个字段:access_token和expires_in,所以需要创建一个实体类封装这两个字段 运行测试类WeixinTest.java控制台打印出错误信息 重置开发者密钥试试看 ...

  6. Codeforces Beta Round #14 (Div. 2)

    Codeforces Beta Round #14 (Div. 2) http://codeforces.com/contest/14 A 找最大最小的行列值即可 #include<bits/s ...

  7. swift 官方文档

    swift 官方文档 https://swift.org/blog/

  8. [leetcode]299. Bulls and Cows公牛和母牛

    You are playing the following Bulls and Cows game with your friend: You write down a number and ask ...

  9. CentOS 7安装zabbix步骤

    Zabbix配置安装 1.前期准备: 我自己的基础环境:CentOS 7 + Mysql 5.6 可以根据官网介绍一步一步安装,官网地址:https://www.zabbix.com/ 图1: 然后点 ...

  10. 轮播图jQuery

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...