#P5279. Mahjong Expected Winning Turn

    ID: 18512 Type: Default 1000ms 256MiB

Mahjong Expected Winning Turn

Mahjong Expected Winning Turn

A special Mahjong set has n (n ≥ 5) different kinds of tiles numbered from 1 to n, and each kind appears 4 times. A winning hand (or "hu") is defined on 14 tiles by either of the following two conditions:

  • The 14 tiles can be partitioned into 5 groups: one pair (two identical tiles) and four melds. A meld is either three identical tiles (a triplet) or three consecutive tiles (a sequence). Formally, a meld is either of the form \(i,i,i\) for some 1 ≤ i ≤ n, or \(i,i+1,i+2\) for some 1 ≤ i ≤ n-2.
  • The 14 tiles can be partitioned into 7 pairs of identical tiles, with the pairs having 7 different numbers.

In this problem a player (named QianTiao) initially draws 13 tiles. Then the remaining \(4n-13\) tiles are randomly shuffled. For a given permutation \(P\) of the remaining tiles, define \(S_i\) to be the set consisting of the 13 initial tiles together with the first \(i\) tiles of \(P\). The weight of permutation \(P\) is the smallest index \(i\) such that in \(S_i\) one can choose a subset of exactly 14 tiles that form a winning hand. (In other words, it is the earliest turn on which the hand could "hu".) It can be shown that when n ≥ 5 the full set \(S_{4n-13}\) always contains some winning 14-tile subset so that the weight is well–defined.

Your task is to compute the expected weight (i.e. the expected number of extra draws needed after the initial 13) taken over all equally–likely orderings of the remaining tiles.

Note: If the initial 13 tiles already have less than 14 tiles then they obviously are not winning. Only when additional tiles are drawn one by one does the possibility of a winning 14–tile hand arise. Also, when more than 14 tiles have been accumulated, you may choose any 14 of them to test whether a winning hand can be formed.

inputFormat

The first line contains an integer n (n ≥ 5), the number of distinct tiles. The second line contains 13 space–separated integers, each between 1 and n, representing the initially drawn 13 tiles.

outputFormat

Output a single number – the expected number of extra draws needed such that the 14–tile winning hand appears. The answer is considered correct if its absolute or relative error does not exceed 10−6.

sample

9
1 1 1 1 2 3 4 5 6 7 8 9 9
1.000000