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)