Http://poj.org/problem? Id = 2096
Program bugs include N Subsets and S types. Each bug belongs to each subset with a probability of 1/N, and each bug belongs to each species with a probability of 1/S. It is expected that every subset has a bug.
Expectation. If DP [I] [J] is set, the existing bug belongs to the subset of I and the expectation of J categories. The final state is known as DP [N] [s] = 0, DP [I] [J] can be reversed:
DP [I] [J]. The new bug belongs to J categories of the existing I subsets, with the probability of I/N * j/s;
DP [I] [J + 1]. The new bug belongs to the existing subset of I but does not belong to the existing J categories, the probability is I/N * (1-J/s );
DP [I + 1] [J]. The new bug does not belong to an existing subset of I but belongs to an 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 subset of I or belongs to an 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 = 0x3f3f3f; const int maxn = 4010; double DP [1010] [1010]; int main (){ Int N, S; while (~ Scanf ("% 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 = s) continue; 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 ;}
Poj 2096 collecting bugs (expected)