QOJ.ac

QOJ

Límite de tiempo: 1 s Límite de memoria: 128 MB Puntuación total: 100

#16249. Random Tree

Estadísticas

A binary tree with $n$ leaf nodes can be generated in the following way. Initially, there is only a root node. First, expand the root node ("expand" in this problem means adding a left child and a right child to a leaf node):

Then, choose one of the two leaf nodes to expand with equal probability, resulting in one of the following two trees:

Afterward, in each step, choose one of the leaf nodes in the current binary tree to expand with equal probability. Repeat this operation until $n$ leaf nodes are generated. For example, a binary tree with 5 leaf nodes might be generated through the following steps:

For a binary tree with $n$ leaf nodes generated in this way, find (1) the expected value of the average depth of the leaf nodes; (2) the expected value of the tree depth. The depth of the root node is defined as 0.

Input

The input consists of a single line containing two positive integers $q$ and $n$, representing the problem ID and the number of leaf nodes, respectively.

Output

The output consists of a single line containing a real number $d$, rounded to 6 decimal places. If $q = 1$, $d$ represents the expected value of the average depth of the leaf nodes; if $q = 2$, $d$ represents the expected value of the tree depth.

Examples

Input 1

1 4

Output 1

2.166667

Note

The expected value is the sum of the values of a random variable multiplied by their probabilities: if a random variable $X$ can take values $x_1, x_2, \dots, x_n$ with probabilities $p_1, p_2, \dots, p_n$ respectively, then the expected value of $X$ is: $$E(X) = \sum_{i=1}^{n} p_i x_i$$

In this problem, according to the generation method of the binary tree, when $n = 4$, the probability of each of the first four trees in the figure below being generated is $1/6$, and the probability of the last tree being generated is $1/3$. Their average leaf depths are $9/4, 9/4, 9/4, 9/4, 2$, respectively. Therefore, the expected value of the average leaf depth is: $$E = \frac{1}{6} \times \frac{9}{4} + \frac{1}{6} \times \frac{9}{4} + \frac{1}{6} \times \frac{9}{4} + \frac{1}{6} \times \frac{9}{4} + \frac{1}{3} \times 2 = \frac{13}{6}$$

Their tree depths are $3, 3, 3, 3, 2$, respectively. Therefore, the expected value of the tree depth is: $$E = \frac{1}{6} \times 3 + \frac{1}{6} \times 3 + \frac{1}{6} \times 3 + \frac{1}{6} \times 3 + \frac{1}{3} \times 2 = \frac{8}{3}$$

Input 2

2 4

Output 2

2.666667

Input 3

1 12

Output 3

4.206421

Input 4

2 12

Output 4

5.916614

Data Scale

Test Case ID $q$ $n$
1, 2 $q = 1$ $2 \le n \le 10$
3, 4, 5 $q = 1$ $2 \le n \le 100$
6, 7 $q = 2$ $2 \le n \le 10$
8, 9, 10 $q = 2$ $2 \le n \le 100$

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.