ThrowBall at Fair
Submissions: 238   Accuracy:


  Difficulty: Medium   Marks: 4

Recently you went to a fair. There were a lots of rides and stalls. You enjoyed the fair a lot, but the most entertaining part was to play ThrowBall. In ThrowBall, you need to pay Rs 100 as the fee, and then throw N identical balls into K distinct boxes, such that there is at least one ball in each box. You tried your best but couldn’t throw all balls properly, so you lost your money. But the shopkeeper was generous, he gave you a last chance. He asked you a question, which if solved correctly will give your money back.

The question is What was the distinct number of ways in which you could throw those balls into the boxes?

As the answer could be very large, print it modulo M, where M is a prime number.

The first line of input contains an integer T, denoting the number of test cases. The only line of each test case contains 3 space seperated integers N,K and M as described above.

For each test case output a single integer corresponding to the answer of the question asked by the shopkeeper, in new line.

1<= T <=100
1<= N <= 1018
1<= K <= 1018
1<= M <= 106


13 9 7
5 5 7


In first test case distinct number of ways of throwing 13 identical balls into 9 distinct boxes is  495.
So the answer modulo 7 is 5.

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Tanuj Yadav
Author: tanujyadav97

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

Need help with your code? Please use, generate link and share the link here.

to report an issue on this page.