Test instructions: There is a system, Xiao Ming to find a bug, he can find and find only one bug every day, require gathered up n different bug, and each system to find at least one bug, to complete the task of the expected number of days
Analysis:
Or expect the DP frame:
DP[I][J]: The current state (Find J system, find the bug in i) the expected number of days to reach the target State, it can be directly reached the state has these kinds:
1.J system, I kind of bug;
2.J system, i+1 kinds of bug;
3.j+1 system, I kind of bug;
4.j+1 a system, I+1 bug
So the equation: dp[i][j]=i/n*j/s*dp[i][j]+ (n-i)/n*j/s*dp[i+1][j]+i/n* (s-j)/s*dp[i][j+1]+ (n-i)/n* (s-j)/s*dp[i+1][j+1]+ 1.0 (Don't forget the Last + 1)
Initialize Direct memset (dp,0,sizeof (0))
The implementation is pushed backwards from the back, but is from the largest value of the previous one.
Result output DP[0][0].
Code:
#include <iostream> #include <cstdio> #include <cstring>using namespace Std;int n,s;double dp[1005][ 1005];int Main () {Cin>>n>>s;memset (dp,0,sizeof (DP)); for (int. i=n;i>=0;i--) {for (int j=s;j>=0;j--) { if (i!=n| | J!=s) {dp[i][j]= (double (s-j)) *i/n/s*dp[i][j+1]+ (double (n-i) *j)/n/s*dp[i+1][j]+ (double (n-i) * (s-j))/n/s*dp[i+1][j +1]+1;dp[i][j]/=1.0-(double (i*j))/n/s;}}} printf ("%.4f\n", Dp[0][0]);}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 2,096 multiple systems to find the number of days expected of N bugs-expect DP