You are given a fair coin and two integers
and .
You repeatedly flip the coin and perform the following operation based on the result:
- If it lands on heads, then will increase by . However, if then will not change.
- If it lands on tails, will decrease by .
You stop as soon as the value of becomes .
Find the expected number of coin flips.
Note that it is guaranteed that under the given constraints, the answer is an integer and does not exceed
Input Format
- The first line contains a single integer — the number of test cases. Then the test cases follow.
- The first and only line of each test case contains two space separated integers and .
Output Format
For each testcase, output the expected number of coin flips.
Constraints
Sample Input 1
4
1 3
2 2
12 12
568 57800
Sample Output 1
0
2
132
65223144
Explanation
In the first test case, is already . So, the expected number of coin flip in this case is .
In the second test case, if the coin lands on head, will not change because is equal to here. Whenever the coin lands on tail, will decrease by and the game ends as becomes . The expected number of coin flips to get the first tail is . So, the answer for the case is .
No comments:
Post a Comment