# 淘汰概率

9

`p = 2/3 * (1 - p) + 1/3 * 0`。解决，我们得到`p = 2/5`。输出应为`2/5 3/5``0.4 0.6`

``````0.1 0.2 0.3 0.4 --> 0.01 0.12 0.25 0.62
0.99 0.99 --> 0.5 0.5 (it's not exact, but if you round to two decimal places, you get 0.5 and 0.5)
``````

4

## CJam（84 80个字符* 0.7 = 56）

``````{_,({_,,{_2\$m<(;(+Q0\)\++m>\}%)_(+.{X2\$-*_@+/}1\{1\$*\1\$-}%)1\-f/.f*:.+}{,da}?}:Q
``````

### 解剖

``````{                   e# Define a recursive function Q
_,({              e# If we have more than one person left in the line...
_,,{            e#   Map each i from 0 to n-1...
_2\$m<         e#     Rotate a copy of the probabilities left i times to get [p_i p_{i+1} ... p_{n-1} p_0 ... p_{i-1}]
(;(+          e#     i fails to defend, leaving the line as [p_{i+2} ... p_{n-1} p_0 ... p_{i-1} p_{i+1}]
Q             e#     Recursive call
0\)\++        e#     Insert 0 for the probability of i winning and fix up the order
m>\           e#     Rotate right i times and push under the list of probabilities
}%
)               e#   Stack: [probs if 0 knocked out, probs if 1 knocked out, ...] [p_0 p_1 ...]
_(+.{           e#   Duplicate probs, rotate 1, and pointwise map block which calculates f(a,b)
X2\$-*_@+/     e#     f(a,b) = (1-p_a)*p_b / (p_a + (1-p_a)*p_b)  TODO is the d necessary?
}
1\{1\$*\1\$-}%    e#   Lift over the list of f(a,b) a cumulative product to get the weights  TODO is the d necessary?
)1\-f/          e#   Normalise the weights
.f*             e#   Pointwise map a multiplication of the probabilities for each case with the corresponding weight
:.+             e#   Add the weights across the cases
}{,da}?           e# ...else only one left, so return [1.0]
}:Q
``````