Chef has 3 decks consisting of N cards each, numbered from 1 to N. He draws out 1 card from each deck randomly with each card having an equal probability of being drawn.
Chef drew cards numbered A and B from the first 2 decks respectively. Now he wonders what is the probability that he will end up with a funny hand after drawing the third card.
A funny hand is when 3 consecutive numbered cards are present in your hand. Please help Chef calculate the probability of ending up with a funny hand after drawing the last card.
If the final probability of ending up with a funny hand is P. You need to print P⋅N, it can be shown that this value is an integer.
For a particular test case, three integers N, A and B are taken as inputs. The cards in a deck are numbered from 1 to N and, A, B are the numbers on cards drawn from the first 2 decks.
For possibility of forming a funny hand 3 cases are possible:
- The third drawn card number is the smallest and A, B are consecutive. In this case the third number takes the value which is minimum of A and B minus 1.
- The third drawn card number is the largest and A, B are consecutive. In this case the third number takes the value which is maximum of A and B plus 1.
- The third drawn card number lies between A and B and all three are consecutive.
Also the values on cards are between 1 and N so smallest number in a consecutive sequence can be 1 and the largest being N.
All the above possibilities need to be checked to find the total possible cases for forming a funny hand.
O(1) for each test case.