Test instructions
have s system, n kinds of bugs, Xiao Ming every day to find a bug, may be any one system, may be any kind of bug, that is, a system bug probability is 1/s, is a certain bug probability is 1/n.
Ask him to find the bug,n of the s system, the desired number of days.
Analysis
Calculate expected e=∑ all possible days * probabilities
Find s system n bug, need minimum max (s,n) days, and the possible number of days is infinite, so the calculation is very complex, complex to calculate.
So consider DP, expecting e=∑ (expectation of all possible scenarios that can be shifted yesterday to the present state +1) * Probability =∑ (expectations of all possible scenarios that can be transferred to the present state yesterday) * Probability +1
At first I want to use dp[i][j] to indicate that I have found a bug,j system to find the bug, the expected number of days, but this can not be launched, by "I-1,j" "I,j-1" "I,j" "i-1,j-1" four states Push, it takes 1 days of probability we know, But the probability of these four cases plus not equal to 1, that is, there are 2 days 3 days ... Situation, the probabilities are complicated to calculate more complex (go back to the complexity above)
So to Dp[i][j] indicates that I have found a bug,j system to find the bug, the desired number of days away from the target.
OK, if we find a bug tomorrow.
It can be transferred here:
DP[I+1][J] does not belong to I species, belongs to one of the J systems. The probability is p1= (n-i)/n* j/s.
Dp[i][j+1] belongs to one of I, does not belong to J system. Probability is p2= (s-j)/s* i/n
Dp[i+1][j+1] does not belong to I species, does not belong to the J system. Probability is p3= (n-i)/n* (s-j)/s
Dp[i][j] belongs to one of I, belonging to one of the J systems. Probability is p4=i/n* j/s
That is, for example, to find a new species, known system bugs, tomorrow to reach the target of the expected number of days is dp[i+1][j], that is today is still poor dp[i+1][j]+1 days.
DP[I][J] is that it found no use of the bug, that tomorrow wasted, that today is still poor dp[i][j]+1 days. today and tomorrow's dp[i][j] are the same.
P1+p2+p3+p4=1, so here's the formula.
DP[I][J]=DP[I+1][J]*P1+DP[I][J+1]*P2+DP[I+1][J+1]*P3+DP[I][J]*P4 +1
Move items (I can hardly understand why I pushed myself and then move items like this ...) Then it becomes: dp[i][j]= (n*s + (n-i) *j*dp[i+1,j] + i* (s-j) *dp[i,j+1] + (n-i) * (S-J) *dp[i+1,j+1])/(N*S-I*J)
We know that dp[n][s]=0, is already reaching the target, still need 0 days. And then reverse push.
Code
#include <cstdio> #define N 1005double d[n][n];int Main () { int n,s; scanf ("%d%d", &n,&s); for (int i=n, i>=0; i--) for (int j=s; j>=0; j--) { if (i!=n| | J!=s) d[i][j]= (d[i+1][j]* (n-i) *j+d[i][j+1]*i* (s-j) +d[i+1][j+1]* (n-i) * (S-J) +n*s)/(N*S-I*J); } printf ("%.4f", D[0][0]); return 0;}
"POJ 2096" collecting Bugs probability expectation DP