#P3898. Maximizing XOR Expectation

    ID: 17146 Type: Default 1000ms 256MiB

Maximizing XOR Expectation

Maximizing XOR Expectation

We are given an integer n and a probability p. A secret integer x is uniformly randomly chosen from the range $[0,n)$.

However, x is sometimes encrypted. In fact, with probability p the number is not encrypted, and with probability 1-p it is encrypted.

We adopt the following strategy to choose an integer y from $[0,n)$:

  • If x is not encrypted, we choose y that maximizes $x \oplus y$ (the bitwise XOR of x and y).
  • If x is encrypted, we choose y uniformly at random from $[0,n)$.

Your task is to compute the expected value of $x \oplus y$ under the above strategy.

Note: All formulas must be given in LaTeX format. For instance, the bitwise XOR is denoted by \(\oplus\).

inputFormat

The input consists of a single line containing an integer n and a floating-point number p separated by a space.

\(n\) represents the upper bound of the range \([0,n)\), and \(p\) is the probability (with \(0 \le p \le 1\)) that x is not encrypted.

outputFormat

Output a single floating point number representing the expected value of \(x \oplus y\). Your answer will be accepted if its absolute or relative error does not exceed \(10^{-6}\).

sample

5 0.5
4.260000