DUKKAR - Dukkar and Pikka

Pika and Dukkar are roomie. Pika is a nerd and likes to play with mathematics! Pika's favourite topic is pascal triangle and he proclaims that he can solve any problem related to this. So Dukkar decides if it's really true!

Dukkar gives a number N and a prime number P. N is the Nth row of pascal triangle starting with 0. Dukkar asks Pika to find how many numbers in nth row are divisible by P. Since the number can be very large so, Pika has to write a program. Since end sem are coming and Pika has to top in his batch so he asks you for help. Can you help Pika?

pascal triangle

Input

The first line of the test file will contain T (T < 100000) where T is the no. of test cases. Each of the next T lines will contain two integers N (0 <= N <= 1018)  and P (2 <= P <= 105) as defined above.

Output

For each test case print on each line K the number of numbers divisible by P on Nth row of the pascal triangle.

Example

Input:
2
2 2
7 7

Output:
1
6

Added by:NISHANT RAJ
Date:2014-03-09
Time limit:0.100s-0.800s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:own

hide comments
2014-03-10 07:18:03 Francky
Please tell what is the distribution mode for P. Is it uniform in the range, or log-uniform? I think it's important before starting to code the problem. Thanks for that.
--
Moreover time limit is ridiculous ; please allow a minimum for slow languages. Unless the problem can be hidden. It won't allow bad complexity submissions with fast languages.

Last edit: 2014-03-09 21:49:59
2014-03-10 07:18:03 Jumpy
seems you and dukkar did well best wishes CHAPATTA
2014-03-10 07:18:03 anurag garg
AC at first attempt
good question though
2014-03-10 07:18:03 knb_dtu
@NISHANT RAJ:-AC in first go :)

Last edit: 2014-03-09 19:10:38
2014-03-10 07:18:03 Bhavik
nice:)
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.