Under construction

Still being built. Things may change

Coin tossing

Time limit2sMemory limit512 MB

Summary
Given head counts from tossing two coins whose head probabilities are independent uniform on [0,1], compute the probability that the first coin's probability is smaller.
Level

Medium7 of 10

Topics
Probability, Math, Dynamic programming, Implementation
Solved
No attempts yet

Problem

A machine makes coins that land heads with probability pp and tails with probability 1−p1-p. Here pp is a number between 0 and 1, and the machine has no setting for it. The machine picks pp at random while it makes a coin, so pp is uniform on the interval [0,1][0, 1].

The machine made two coins. Coin 1 has probability pp and coin 2 has probability qq. Both pp and qq are uniform on [0,1][0, 1] and independent of each other, and there is no way to read off their actual values.

Coin 1 was tossed n1n_1 times and came up heads m1m_1 times. Coin 2 was tossed n2n_2 times and came up heads m2m_2 times. Given these observations, write a program that computes the probability that p<qp < q.

Input

The first line contains the number of test cases TT (1≤T≤1000001 \le T \le 100000).

Each of the next TT lines holds one test case: n1n_1, m1m_1, n2n_2, m2m_2, separated by spaces. (1≤n1,n2≤10001 \le n_1, n_2 \le 1000, 0≤m1,m2≤500 \le m_1, m_2 \le 50, m1≤n1m_1 \le n_1, m2≤n2m_2 \le n_2)

Output

For each test case, print the probability that p<qp < q on its own line. Round at the seventh decimal place and print exactly six digits after the decimal point. For example, print 0.800000 when the probability is 45\frac{4}{5}.

Examples4

  1. Example 1

    Input
    4
    2 1 4 3
    8 4 16 8
    2 0 6 1
    2 0 2 1
    
    Expected output
    0.714286
    0.500000
    0.533333
    0.800000
    
  2. Example 2

    Input
    1
    1 0 1 0
    
    Expected output
    0.500000
    
  3. Example 3

    Input
    4
    1 1 1 0
    1 0 1 1
    1 1 1 1
    1 0 1 0
    
    Expected output
    0.166667
    0.833333
    0.500000
    0.500000
    
  4. Example 4

    Input
    4
    1000 0 1000 50
    1000 50 1000 0
    1000 0 1 0
    1 0 1000 0
    
    Expected output
    1.000000
    0.000000
    0.998006
    0.001994