Poj 2096 (expectation for DP)

Source: Internet
Author: User
Tags integer numbers
A-collecting bugs Time limit:10000 ms Memory limit:64000kb 64bit Io format:% I64d & % i64usubmit statusappoint description: System crawler)


Ivan is fond of collecting. unlike other people who collect Post stamps, coins or other material stuff, he collects software bugs. when Ivan gets a new program, he classifies all possible bugs into N categories. each day he discovers exactly one bug in the program and adds information about it and its category into a spreadsheet. when he finds bugs in all bug categories, he callthe program disgusting, publishes this spreadsheet on his home page, and forgets completely about the program.
Two companies, macrosoft and microhard are in tight competition. microhard wants to decrease sales of one macrosoft program. they hire Ivan to prove that the program in question is disgusting. however, Ivan has a complicated problem. this new program has s subcomponents, and finding bugs of all types in each subcomponent wocould take too long before the target cocould be reached. so Ivan and microhard agreed to use a simpler criteria --- Ivan shocould find at least one bug in each subsystem and at least one bug of each category.
Macrosoft knows about these plans and it wants to estimate the time that is required for Ivan to call its program disgusting. it's important because the company releases a new version soon, so it can correct its plans and release it quicker. nobody wocould be interested in Ivan's opinion about the reliability of the obsolete version.
A bug found in the program can be of any category with equal probability. similarly, the bug can be found in any given subsystem with equal probability. any particle bug cannot belong to two different categories or happen simultaneously in two different subsystems. the number of bugs in the program is almost infinite, so the probability of finding a new bug of some category in some subsystem does not reduce after finding any number of bugs of that category in that subsystem.
Find an average time (in days of Ivan's work) required to name the program disgusting.


Input file contains two integer numbers, N and S (0 <N, S <= 1 000 ).


Output the expectation of the Ivan's working days needed to call the program disgusting, accurate to 4 digits after the decimal point.

Sample Input

1 2

Sample output


Question and analysis:
From: http://blog.csdn.net/morgan_xww/article/details/6774708
DP to find the expected question.
  • A software has s subsystems, which produces n kinds of bugs.
  • Someone finds a bug in a single day. It is a bug that occurs in a subsystem.
  • Find all the N bugs, and each subsystem finds the bugs, so that the expected number of days is required.
  • Note that the number of bugs is infinite, so the probability of a bug appearing in a subsystem is 1/s,
  • The probability of a certain type is 1/N.
  • Solution:
  • DP [I] [J] indicates that the I-type bug has been found, and coexist in the expectation of the number of days to reach the target State in the J subsystems.
  • Obviously, DP [N] [s] = 0 because the target has been reached. DP [0] [0] is the answer we need.
  • The DP [I] [J] status can be converted into the following four types:
  • DP [I] [J] finding a bug is one of the found I bugs and J subsystems.
  • DP [I + 1] [J] finding a bug is a new bug, but it is a found J seed system.
  • DP [I] [J + 1] finding a bug is a found I bug, but a new subsystem.
  • DP [I + 1] [J + 1] discovering a bug is a new bug and a new sub-system.
  • The above four probabilities are:
  • P1 = I * j/(n * s)
  • P2 = (n-I) * j/(n * s)
  • P3 = I * (S-j)/(n * s)
  • P4 = (n-I) * (S-j)/(n * s)
  • Also, the expectation can be divided into the weighted sum of multiple sub-expectations and the weight is the probability of sub-expectations, that is, E (AA + BB + ...) = AE (A) + be (B) +...
  • Therefore:
  • DP [I, j] = p1 * DP [I, j] + p2 * DP [I + 1, J] + P3 * DP [I, J + 1] + P4 * DP [I + 1, J + 1] + 1;
  • Sorted:
  • DP [I, j] = (1 + p2 * DP [I + 1, J] + P3 * DP [I, j + 1] + P4 * DP [I + 1, J + 1])/(1-p1)
  • = (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)


 1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cstdio> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 #include<map> 9 #include<vector>10 #include<set>11 12 #define N 100513 #define M 10000014 #define inf 100000000715 #define mod 100000000716 #define mod2 10000000017 #define ll long long18 #define maxi(a,b) (a)>(b)? (a) : (b)19 #define mini(a,b) (a)<(b)? (a) : (b)20 21 using namespace std;22 23 int n;24 int s;25 double dp[N][N];26 double p1,p2;27 28 void ini()29 {30     memset(dp,0,sizeof(dp));31     p1=(1.0)*1/n;32     p2=(1.0)/s;33    // printf(" %.4f %.4f\n",p1,p2);34 }35 36 void solve()37 {38     int i,j;39     for(i=n;i>=0;i--){40         for(j=s;j>=0;j--){41             if(i==n && j==s) continue;42             dp[i][j]=1+dp[i+1][j]*p1*(n-i)*p2*j+dp[i][j+1]*p1*i*p2*(s-j)43                 +dp[i+1][j+1]*p1*(n-i)*p2*(s-j);44             dp[i][j]/=(1-p1*i*p2*j);45         }46     }47 48    // for(i=n;i>=0;i--){49     //    for(j=s;j>=0;j--){50     //        printf(" i=%d j=%d dp=%.4f\n",i,j,dp[i][j]);51     //    }52     //}53 }54 55 void out()56 {57     printf("%.4f\n",dp[0][0]);58 }59 60 int main()61 {62     //freopen("data.in","r",stdin);63     //freopen("data.out","w",stdout);64     //scanf("%d",&T);65    // for(int cnt=1;cnt<=T;cnt++)66    // while(T--)67     while(scanf("%d%d",&n,&s)!=EOF)68     {69         ini();70         solve();71         out();72     }73     return 0;74 }


Poj 2096 (expectation for 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.