# 概率/数学期望总结

$$E=\sum_{i=1}^np_ix_i$$

$1->2->4:14$
$1->2->5:11$
$1->3->6:19$
$1->3->7:20$

$$E=5 \times 1+6 \times {1 \over 2}+2 \times {1 \over 2}+7 \times {1 \over 4}+4 \times {1 \over 4}+8 \times {1 \over 4}+9 \times {1 \over 4}=16$$

$$E(aA+bB+\cdots) = aE(A) + bE(B) + \cdots$$

#### Description

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 calls the 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 would take too long before the target could be reached. So Ivan and Microhard agreed to use a simpler criteria — Ivan should 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 would 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 particular 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

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

#### Output

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

1 2

#### Sample Output

3.0000

$f[i][j]$状态可以转化成以下四种：

$f[i][j]$发现一个bug属于已经找到的i种bug和j个子系统中
$f[i+1][j]$发现一个bug属于新的一种bug，但属于已经找到的j种子系统
$f[i][j+1]$发现一个bug属于已经找到的i种bug，但属于新的子系统
$f[i+1][j+1]$发现一个bug属于新的一种bug和新的一个子系统

$p1 = {i\times j \over n\times s}$
$p2 = {(n-i)\times j \over n\times s}$
$p3 = {i\times (s-j) \over n\times s}$
$p4 = {(n-i)\times (s-j) \over n\times s}$

$$f[i][j]={i\times j \over n\times s} \times f[i][j]+{(n-i)\times j \over n\times s} \times f[i+1][j]+{i\times (s-j) \over n\times s}\times f[i][j+1]+{(n-i)\times (s-j) \over n\times s} \times f[i+1][j+1]+1$$

$$f[i][j]-{i\times j \over n\times s} \times f[i][j]={(n-i)\times j \over n\times s} \times f[i+1][j]+{i\times (s-j) \over n\times s}\times f[i][j+1]+{(n-i)\times (s-j) \over n\times s} \times f[i+1][j+1]+1$$

$$f[i][j]=[{(n-i)\times j \over n\times s} \times f[i+1][j]+{i\times (s-j) \over n\times s}\times f[i][j+1]+{(n-i)\times (s-j) \over n\times s} \times f[i+1][j+1]+1] \div (1-{i\times j \over n\times s})$$

$$f[i][j]=[(n-i)\times j \times f[i+1][j]+i\times (s-j)\times f[i][j+1]+(n-i)\times (s-j) \times f[i+1][j+1]+n\times s] \div (n\times s-i\times j)$$

bzoj3450

#### Description

Sevenkplus闲的慌就看他打了一盘，有些地方跟运气无关要么是o要么是x，有些地方o或者x各有50%的可能性，用?号来表示。

4

????

4.1250

n<=300000

luogu2473

1 2
1 0
2 0

1.500000

6 6
12 2 3 4 5 0
15 5 0
-2 2 4 5 0
-11 2 5 0
5 0
1 2 4 5 0

10.023470

#### 说明

1 <= k <= 100, 1 <= n <= 15，分值为[-10^6,10^6]内的整数。

cogs1498

4

1/2 1

1/2 2

0/1 10

1/2 3

Case #1: 2

Case #2: 2

Case #3: 1

Case #4: 2

#### 【提示】

1<=N<=3000,0<=p< 1

p的分母不超过1000。

1<=n<=100。

$f[i][j]$表示进行了i局一共赢了j局，且枚举结束后获胜比例都不超过p的概率。

$$f[i][j]=f[i-1][j] \times (1-p)+f[i-1][j-1] \times p {{j \over i}<=p}$$

x=1时结束的概率为$q_s$
x=2时结束的概率为$(1-q_s)\times q_s$
x=3时结束的概率为$(1-q_s)^2 \times q_s$

$$E=q_s+2\times (1-q_s)\times q_s+3\times (1-q_s)^2 \times q_s+\cdots$$

$$s={E \over q_s}=1+2\times (1-q_s)+3\times (1-q_s)^2+\cdots$$

$$(1-q_s) \times s=(1-q_s)+2\times (1-q_s)^2+3\times (1-q_s)^3+\cdots$$

$$s-(1-q_s)\times s=1+(1-q_s)+(1-q_s)^2+\cdots$$

$$q_s\times s=E=1+(1-q_s)+(1-q_s)^2+(1-q_s)^3+\cdots$$

$$E={1\over q_s}$$

3 3
2 3
1 2
1 3

3.333

#### 说明

$$p(x,y)={p[x]\over d[x]}+{p[y] \over d[y]}$$

$p[i]$表示这个点经过的概率，$d[i]$表示这个点的度。

$$p[i]=\sum_{(i,j)\in G} {p[j]\over d[j]}$$

$$这个点的概率\times 1-\sum所有相邻的点转移过来的概率=0$$

bzoj3270

2 1 1 2

1 2

0.5

0.5

#### Sample Output

0.500000 0.500000

#### HINT

$$f$$

×