ZOJ 3640 Help Me Escape probability dp, zoj3640

Source: Internet
Author: User

ZOJ 3640 Help Me Escape probability dp, zoj3640

A vampire is trapped and has n ways to escape. Each road has a difficulty c []. His initial combat power is f. For the I-th Road, if f> c [I], he will spend t [I] days to go out. Otherwise, he will stay for one day, increase the fighting capacity of c [I], and then select another way to go out, the probability of each route is the same. Ask him about the days he expected to escape.

If dp [I] is set to indicate the expected value of escaping when the combat capability is I, the state equation can be introduced.

Dp [I] = 1/n * t [j] (c [j]> I ), dp [I] = 1/n * (1 + dp [I + c [j]) (c [j] <= I ).

Note that the final State is determined. When f> Max, the expected value is the average value of the total time, But I + c [j] may be greater than Max + 1, so the final state should be Max * 2. During initialization, you forget to multiply by 2.

In addition, t [I] is an integer.




Description

Background

If thou doest well, shalt thou not be accepted? And if thou doest not well, sin lieth at the door. And unto thee shall be his desire, and thou shalt rule over him.
And Cain talked with Abel his brother: and it came to pass, when they were in the field, that Cain rose up against Abel his brother, and slew him.
And the LORD said unto Cain, Where is Abel thy brother? And he said, I know not: Am I my brother's keeper?
And he said, What hast thou done? The voice of thy brother's blood crieth unto me from the ground.
And now art thou cursed from the earth, which hath opened her mouth to receive thy brother's blood from thy hand;
When thou tillest the ground, it shall not henceforth yield unto thee her strength; a fugitive and a vagabond shalt thou be in the earth.

-- Bible Chapter 4

Now Cain is unexpectedly trapped in a caveNPaths. Due to LORD's punishment, all the paths are zigzag and dangerous. The difficulty ofIthPath isCi.

Then we defineFAs the fighting capacity of Cain. Every day, Cain will be sent to one ofNPaths randomly.

Suppose Cain is in front ofIthPath. He can successfully takeTiDays to escape from the cave as long as his fighting capacityFIs largerCi. Otherwise, he has to keep trying day after day. However, if Cain failed to escape, his fighting capacity wowould increaseCiAs the result of actual combat. (A kindly reminder: Cain will never died .)

AsTi, We can easily draw a conclusion thatTiIs closely relatedCi. Let's use the following function to describe their relationship:

After D days, Cain finally escapes from the cave. Please output the expectation of D.

Input

The input consists of several cases. In each case, two positive integersNAndF(N≤ 100,F≤ 10000) are given in the first line. The second line between des N positive integersCi(Ci≤ 10000, 1 ≤I≤ N)

Output

For each case, you shoshould output the expectation (3 digits after the decimal point ).

Sample Input

3 11 2 3

Sample Output

6.889
#include<stdio.h>#include<iostream>#include<algorithm>#include<math.h>#include<cstring>using namespace std;double dp[1000010];int b[110];double a=(1+sqrt(5))*0.5;int main(){    int n,f;    while(~scanf("%d %d",&n,&f))    {        int Max=0;        for(int i=1; i<=n; i++)        {            scanf("%d",&b[i]);            Max=max(Max,b[i]);        }        memset(dp,0,sizeof(dp));        for(int i=2*Max; i>=f; i--)        {           dp[i]=0;            for(int j=1; j<=n; j++)            {                if(i>b[j])dp[i]+=(int)(a*b[j]*b[j]);                else dp[i]+=(1+dp[i+b[j]]);            }            dp[i]/=n;        }        printf("%.3lf\n",dp[f]);    }    return 0;}


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.