Codeforces 261b Maxim and restaurant (probability DP)

Source: Internet
Author: User
B. Maxim and restauranttime limit per Test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

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

Maxim has got a dinner party tonight,NGuests will come to him. Let's index the guests of Maxim's restaurant from 1N. Maxim knows the sizes of all guests that are going to come to him.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 leader 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 largerP. 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 wowould 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 possibleN! Orders of guests in the queue. Help Maxim, calculate this number.

Input

The first line contains integerN(1 digit ≤ DigitNLimit ≤ limit 50)-the number of guests in the restaurant. The next line contains IntegersA1,A2 ,...,AN(1 digit ≤ DigitAILimit ≤ limit 50)-The guests 'sizes in meters. The third line contains integerP(1 digit ≤ DigitPLimit ≤ limit 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 minutes-since 4.

Examplesinputcopy
3
1 2 3
3
Outputcopy
1.3333333333
Note

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

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

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

 

Question:

The DP [I] [J] [k] indicates the expectation that J and K are obtained for the first I.

When I is considered to be transferred, it is nothing more than a [I]

The ratio of the number of times is C (I-1, J-1)/C (I, j) = J/I

Therefore, the proportion not counted is 1-j/I = (I-j)/I

Therefore, the transfer equation is obtained.

DP [I] [J] [k] = J/I * DP [I-1] [J-1] [k-A [I] + (I-j) /I * DP [I-1] [J] [k]

 

The Code is as follows:

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

 

Codeforces 261b Maxim and restaurant (probability DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.