Dice Rolls

2.6

11 votes
Easy-Medium
Problem

You have an unbiased dice which you want to keep rolling until you get N consecutive even numbers. You've rolled the dice M times and surprisingly, all rolls resulted in even numbers. What is the expected number of additional rolls needed until you get N consecutive even numbers?

Input:
The first line contains the number of cases T. Each of the next T lines contains two numbers N and M.

Output:
Output T lines containing the answer for the corresponding test case. Print the answer rounded to exactly 2 decimal places.

Constraints:
1 <= T <= 100
1 <= N <= 1000
0 <= M <= N

Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation

If N = 2 and M = 0, you need to keep rolling the dice until you get 2 consecutive even numbers. It is not hard to show that on average, 6 dice rolls are needed.
If N = 2 and M = 1, you need 2 consecutive even numbers and already have 1. You need to roll once more no matter what. In that first roll, if you get an even number, you are done. Otherwise, you need to start over, as the consecutive counter resets, and you need to keep rolling the dice until you get N=2 consecutive even numbers. The expected number of dice rolls is thus 1 + (0.5 * 0 + 0.5 * 6) = 4.0

If N = 3 and M = 3, you already have got 3 even numbers, so you do not need any more rolls.

Editor Image

?