DUKKAR - Dukkar and Pikka

no tags 

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

hide comments
Francky: 2014-03-10 07:18:03

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
Jumpy: 2014-03-10 07:18:03

seems you and dukkar did well best wishes CHAPATTA

anurag garg: 2014-03-10 07:18:03

AC at first attempt
good question though

knb_dtu: 2014-03-10 07:18:03

@NISHANT RAJ:-AC in first go :)

Last edit: 2014-03-09 19:10:38
Bhavik: 2014-03-10 07:18:03

nice:)


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