RANAGIC - Random Magic Trick


A standard deck of cards has 52 cards in total, 4 suits - Hearts, Diamonds, Clubs, and Spades and each suit has 13 cards (2, 3, 4, 5, 6, 7, 8, 9, 10, Jacks, Queens, Kings and Aces). Oompa Loompa is an amateur magician who loves card magics. As you know, many magic tricks are rather simple once you know the secret behind them. Oompa Loompa's favorite trick is the random magic trick. The trick goes on like this. At first, he takes a deck of cards consisting of some suits, and each suit containing some cards, and randomly shuffles them. The magician's trick here is to show there are at least 2 consecutive Kings as well as 2 consecutive Queens in the shuffled deck. As you can see, this trick relies on pure luck, and may be unsuccessful if the magician's luck runs out :)

Performing the trick with a non-standard deck of cards, with any number of suits and any number of cards in each suit, what is the probability of performing a successful magic trick?

Note:
  • Assume that the deck is shuffled uniformly randomly.
  • Assume that any suit contains at least two cards, and every suit contains the king card and the queen card.


  • A suit consists of distinct cards numbered from 1 to C, and the cards numbered 1 and 2 are Kings and Queens respectively. Note that any card is uniquely identified by its suit and card number.

    Input

    The first line contains T denoting the number of test cases. Each of the next T lines consists of two space-separated integers S and C, denoting the number of suits and the number of cards in each suit respectively.

    Constraints

  • 1 ≤ T ≤ 1692
  • 1 ≤ S ≤ 36
  • 2 ≤ C ≤ 48

    Output

    For each test case, print the probability of the magician performing a successful magic trick. An absolute error less than 10-6 will be ignored.

    Sample Input

    2
    2 2
    2 3
    

    Sample Output

    0.333333333333
    0.133333333333
    

    Explanation

    In the first test case, there are 4 cards in the deck (2 suits with 2 cards each). This means that there can be 4! different permutations of the shuffled cards. Only 8 of them contain at least one pair of adjacent Kings and Queens. The probability of a successful magic trick is, therefore, 8/24 = 1/3

    Reference

    HackerRank - Random Magic


  • Added by:sgtlaugh
    Date:2020-02-22
    Time limit:3.5s
    Source limit:5000B
    Memory limit:1536MB
    Cluster: Cube (Intel G860)
    Languages:All
    Resource:Own problem, used in HackerRank