POJ 2,096 multiple systems to find the number of days expected of N bugs-expect DP

Source: Internet
Author: User

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

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.