POJ 2096 Collecting Bugs (expectation) __ probability DP

Source: Internet
Author: User

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



bugs in programs have n subsets, s kinds. The probability of each bug belonging to each subset is 1/n, and each bug belongs to the probability of each category as 1/S, asking each subset and the expectation that each category has bugs.


expect, set DP[I][J] to indicate that an existing bug is a subset of I, the expectation of J species, now known as dp[n][s] = 0,dp[i][j] can be pushed by the reverse:

Dp[i][j], the new bug belongs to the existing I subset J classification, the probability is i/n * J/S;

Dp[i][j+1], the new bug belongs to the existing I subset but does not belong to the existing J classification, the probability is i/n * (1-j/s);

Dp[i+1][j], the new bug does not belong to the existing I subset but belongs to the existing J classification, the probability is (1-i/n) *j/s;

Dp[i+1][j+1], the new bug does not belong to an existing I subset and does not belong to the existing J classification, the probability is (1-i/n) * (1-j/s);

therefore dp[i][j] = I/n*j/s*dp[i][j] + i/n * (1-j/s) *dp[i][j+1] + (1-i/n) *j/s*dp[i+1][j] + (1-i/n) * (1-j/s) *dp[i+1][j +1] + 1.


#include <stdio.h>
#include <iostream>
#include <map>
#include <set>
# Include <list>
#include <stack>
#include <vector>
#include <math.h>
# Include <string.h>
#include <queue>
#include <string>
#include <stdlib.h>
#include <algorithm>
//#define LL __int64
#define LL Long
#define EPS 1e-12
# Define PI ACOs ( -1.0)
using namespace std;
const int INF = 0X3F3F3F3F;
const int MAXN = 4010;
Double dp[1010][1010];

int main ()
{
	int n,s;
	while (~SCANF ("%d%d", &n,&s))
	{
		Dp[n][s] = 0;
		for (int i = n; I >= 0; i--)
		{for
			(int j = s; J >= 0; j--)
			{
				if (i = = N && j = =)
					Co Ntinue;
				DP[I][J] = (dp[i+1][j]* (n-i) *j + dp[i][j+1]*i* (s-j) + dp[i+1][j+1]* (n-i) * (S-J) +n*s)/(N*S-I*J) *1.0;
			}
		}
		printf ("%.4f\n", Dp[0][0]);
	return 0;
}


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.