POJ 2096:collecting Bugs probability DP seeking expectation

Source: Internet
Author: User

Collecting Bugs Topic Connection:

http://poj.org/problem?id=2096

Test instructions

Ivan likes to collect bugs, he will find a bug every day, found this bug has a property and belongs to a subsystem, bug total n Properties, subsystem total s (0<n, s≤1000), find Ivan set up n bug and each subsystem has the expectation of bug.

Exercises

The first one asks for the desired question, so that dp[i][j] indicates that the system already has all the J bugs of I system and expects to get the number of days of all bugs, so dp[n][s]=0, and dp[0][0] is the answer.

DP[I][J] to the next day there will be four cases dp[i][j] (found an existing bug and the bug belongs to the subsystem has a bug), Dp[i+1][j] (found a new bug but the bug belongs to the subsystem has a bug), dp[i][j+1] (an existing bug was found, but the system of bugs exists before this bug), Dp[i+1][j+1] (a new bug has been found and the subsystem to which the bug belongs has not yet been bug).

The probabilities of obtaining these four cases are:

p1=i*j/(n*s);

P2= (n-i) *j/(n*s);

p3=i* (S-J)/(n*s);

p4= (n-i) * (s-h)/(n*s);

By the expected linear nature can know dp[i][j]=p1*dp[i][j]+p2*dp[i+1][j]+p3*dp[i][j+1]+p4*dp[i+1][j+1]; because it is the next day, so Dp[i][j] also add 1.

              

Code
#include<stdio.h>Constint N=1002;Double DP[N][n],p1, p2, p3, p4Ns;IntMain(){int n, S;While(~scanf("%d%d", &n, &s)){NS=n*s;dp[n][s]=0.0;For(int I=n; I>=0;--I.)For(Int J=s; j>=0;--j)If(I!=n|| J!=s){P1=i*j/(NS);p 2= (n-I.) *j/(NS);p 3=i* (S-j)/(NS);p 4= (n-I.) * (s-j)/(NS); Dp[I][j]=(1.0+p2*dp[I+1][j]+p3*dp[i][j< Span class= "Sh_number" >+1]+p4*dp[i+1][j+1])/(1.0-p1} printf ( "%.4f \n ",dp[< Span class= "Sh_number" >0][0]); }}          

  

POJ 2096:collecting Bugs probability DP seeking expectation

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.