Under construction

Still being built. Things may change

Fork

Time limit0.15sMemory limit1024 MB

Summary
Given P, N, L, and R, find the minimum expected number of key presses to finish typing a fixed N-character string into two random shells.
Level

Hard8 of 10

Topics
Dynamic programming, Probability
Solved
No attempts yet

Problem

Luca opened a Python shell and typed os.fork(), which spawned a second shell. After that, each time Luca pressed any key, it would randomly go to one of the two shells. Each shell has an input string (shown in the terminal), which is edited by the key presses going to that shell. Luca also sees the terminal, so he knows which shell's input string was affected when he presses a key.

His keyboard has NN keys with distinct characters on them and Backspace. When a character key press goes to a shell, the character is appended to the end of its input string. When a Backspace key press goes to a shell, the last character of its input string is erased. If the shell's input string is empty, nothing happens to it, though Luca still sees that the Backspace went there. Each key press has probability PP of going to the left shell and probability 1−P1 - P of going to the right one.

Luca wants to type a fixed string a1a2⋯aNa_1 a_2 \cdots a_N of NN distinct characters into both shells. He has already typed LL correct characters into the left shell and RR into the right one, so the strings in the two shells are a1a2⋯aLa_1 a_2 \cdots a_L and a1a2⋯aRa_1 a_2 \cdots a_R.

For example, consider P=0.3P = 0.3, N=2N = 2 (the string could be ab), L=0L = 0 and R=1R = 1. A possible sequence of events is:

StepKeySideLeft shellRight shell
0a
1bRightab
2aRightaba
3aLeftaaba
4bRightaabab
5BackspaceRightaaba
6BackspaceLeftaba
7BackspaceLeftaba
8BackspaceRightab
9aLeftaab
10bRightaabb
11bLeftababb
12BackspaceRightabab

In this case, typing ab into both shells took 12 key presses.

An incorrect character is a character in one of the two shells that must be deleted at some point before the full string is typed into both shells. Only such characters count as incorrect.

Luca has decided that he will never press Backspace unless at least one of the shells contains an incorrect character. He has also decided that he will never press a key that always produces an incorrect character. Luca wants the strategy with the minimum expected number of key presses.

Write a program fork.cpp that solves this problem.

Input

The first and only line of standard input contains PP, NN, LL, and RR.

Output

Print the answer on the first and only line of standard output. Print it with at least 12 digits of precision. You can use:

std::cout << std::setprecision(12) << ans << std::endl;

Constraints

0≤L,R≤N≤2×1070 \le L, R \le N \le 2 \times 10^7

0.1≤P≤0.90.1 \le P \le 0.9

Examples1

  1. Example 1

    Input
    0.3 2 0 1
    
    Expected output
    16.7142857142857