Treap ambiguity P30477


Statement
 

pdf   zip

Roughly speaking, a treap is a BST (a binary search tree) and also a heap. Assume that we have nn different keys, and a fixed integer mm. Every key is independently associated with a priority (a random integer chosen uniformly from the interval [1,m][1, m]). Let xx be the key with the largest priority. Then, the treap for the set of pairs (key, priority) is the BST with xx at its root, the treap for the pairs with keys smaller than xx as its left subtree, and the treap for the pairs with keys larger than xx as its right subtree.

For instance, suppose that the keys are {40,41,42,49,70,80}\{ 40, 41, 42, 49, 70, 80 \} and m=1000m = 1000. Then, the pairs (key, priority) could be (40, 189), (41, 350), (42, 960), (49, 234), (70, 800) and (80, 780). In this case, we would have the treap to the left:

$$\pstree[levelsep=40pt,treesep=30pt]{\Tcircle{\mbox{\color{blue} \scriptsize 42} \, \mbox{\color{red} \tiny (960)}}}{ \pstree{\Tcircle{\mbox{\color{blue} \scriptsize 41} \, \mbox{\color{red} \tiny (350)}}} { \pstree{\Tcircle{\mbox{\color{blue} \scriptsize 40} \, \mbox{\color{red} \tiny (189)}}} {} \Tn } \pstree{\Tcircle{\mbox{\color{blue} \scriptsize 70} \, \mbox{\color{red} \tiny (800)}}} { \pstree{\Tcircle{\mbox{\color{blue} \scriptsize 49} \, \mbox{\color{red} \tiny (234)}}} {} \pstree{\Tcircle{\mbox{\color{blue} \scriptsize 80} \, \mbox{\color{red} \tiny (780)}}} {} } } \enspace \pstree[levelsep=40pt,treesep=30pt]{\Tcircle{\mbox{\color{blue} \scriptsize 3} \, \mbox{\color{red} \tiny (42)}}}{ \Tn \pstree{\Tcircle{\mbox{\color{blue} \scriptsize 5} \, \mbox{\color{red} \tiny (42)}}} {} } \qquad \pstree[levelsep=40pt,treesep=30pt]{\Tcircle{\mbox{\color{blue} \scriptsize 5} \, \mbox{\color{red} \tiny (42)}}}{ \pstree{\Tcircle{\mbox{\color{blue} \scriptsize 3} \, \mbox{\color{red} \tiny (42)}}} {} \Tn } \qquad \pstree[levelsep=40pt,treesep=30pt]{\Tcircle{\mbox{\color{blue} \scriptsize 3} \, \mbox{\color{red} \tiny (42)}}}{ \pstree{\Tcircle{\mbox{\color{blue} \scriptsize 2} \, \mbox{\color{red} \tiny (23)}}} {} \pstree{\Tcircle{\mbox{\color{blue} \scriptsize 5} \, \mbox{\color{red} \tiny (23)}}} {} }$$

If we only look at the keys (in blue), we have a BST. If we only look at the priorities (in red), we have a sort of a heap (the largest at the top; the same property holds recursively).

This definition is usually good enough in many practical situations. However, there is a caveat: With repeated priorities, we can have more than one possible treap. For example, the second and the third treaps above are possible for the pairs (3, 42) and (5, 42).

Note that we can have a unique treap with repeated priorities. Consider for instance the pairs (2, 23), (3, 42) and (5, 23). The only possible treap is the fourth above.

Given nn and mm, what is the probability that the treap is unique?

Input

Input consists of several cases, each with nn and mm. Assume 1n501 \le n \le 50 and 2m50002 \le m \le 5000.

Output

For every case, print the probability that the treap is unique with four digits after the decimal point. The input cases have no precision issues.

Public test cases
  • Input

    1 100
    2 2
    3 3
    50 5000
    

    Output

    1.0000
    0.5000
    0.3333
    0.9656
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Official solutions
    C++
    User solutions
    C++