Probability statistics (DP)

Source: Internet
Author: User

narrative description of the problem
Generates N-month ∈[a,b] random integers. and output them to the x probability.
Input Format
The input line is followed by a four integer n. A,b,x, separated by a space.
output Format
The output line includes a decimal place and a probability of x. Keep four decimal places after decimal point
Example Input
2 1 3 4
Example Output
0.3333
data size and conventions
For 50% of data, n≤5.
For 100% of data, n≤100,b≤100.
The following:
This is a typical complete backpack, but in the process of doing some twists and turns.
DP[I][J]: The probability of getting J after selecting I number.
Dp[i][j]=dp[i-1][z]*1/sum;
In each case, a 1/sum is multiplied, and the relationship between the situation and the situation is sums.


(The first thing I want to do is to get the X number/total number), and then I found out it was too big. 100^100. So using DP to express the probability better, the rest is a complete backpack.
Code implementation:

#include <bits/stdc++.h>#define MAXusing namespace STD;DoubleDp[max][max*max];DoubleSumintN,a,b,x;intMain () {scanf("%d%d%d%d", &n,&a,&b,&x); sum = b-a+1;memset(DP,0,sizeof(DP)); for(inti =0; i < N; i++) { for(intj = A; J <= B; J + +) {if(i = =0) dp[i][j]=1/sum;Else{ for(intz =0; Z <= X; z++) {if(dp[i-1][Z]! =0) dp[i][z+j]+= (dp[i-1][z]*1/sum); }            }        }    }printf("%.4lf\n", dp[n-1][x]);}

Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.

Probability statistics (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.