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;
}