GCPC11A - Faculty Dividing Powers

no tags 

Fred Faculty and Paul Power love big numbers. Day after day Fred chooses a random integer n and he computes n!. His friend Paul amuses himself by computing several powers of his randomly chosen integer k like k2, k3 and so on. On a hot summer day, Fred and Paul got really, really bored, so they decided to play a joke on their buddy Dave Divider. Fred chooses a random integer n while Paul chooses a random integer k. They want Dave to find the biggest integer i such that ki divides n! without a remainder, otherwise they will throw a cake in Dave's face. Because Dave does not like cakes in his face, he wants you to help him finding that integer i.

Input

The first line contains the number of test cases t (1 ≤ t ≤ 100). Each of the following t lines contains the two numbers n,k (2 ≤ n ≤ 1018, 2 ≤ k ≤ 1012) separated by one space.

Output

For each test case, print the maximum integer i on a separate line.

Example

Input:
2
5 2
10 10

Output:
3
2

Be careful with overflows in this problem (use 64 bit integers, avoid multiplications which will lead to overflow).


hide comments
Shubham Jadhav: 2020-07-14 21:48:28

Thanks for all the hints @Adrian

nadstratosfer: 2018-02-25 06:16:58

Watch out when using the spoiler for spoilerization, k appears to slightly exceed 10^12.

[Lakshman]: 2016-10-29 08:49:23

@amartyadav : writing answer at once or writing answer after each test case will not affect the Judge verdict .

amartyadav: 2016-10-28 23:17:36

@Adrian. Just one question. Does the program have to print all the outputs at last together? I programmed in a manner where outputs will be given for every test case as soon as the first test case input is given.
I guess this is why I'm always getting a wrong answer. Because it works perfectly right on my system with the test cases.

ashish22_dwd: 2016-10-09 00:11:44

Finally.. AC

rainy jain : 2016-05-24 08:32:22

@Adrian can you please give any hint why I'm getting WA id:16979406.

Last edit: 2016-05-24 08:33:21
Rishav Goyal: 2014-04-05 16:27:44

awesome !!
check a*b overflow, if a > (2^63-1)/b then overflow.!!
to increase the accuracy, make b small.

SWOOSH!!!: 2013-07-08 13:10:07

@Adrian, my solution gives the correct output for n=10^18,k=2 and n=10^18,k=10^12 but getting WA!!!...Submission id 9624812

Last edit: 2013-07-08 13:33:59
uli: 2013-04-11 07:02:58

@Adrian: Can you please give me a clue why 9081770 does return NZEC?

bashrc is back: 2012-04-22 02:08:40

similar problem with reduced constraints http://www.spoj.pl/problems/OPC5/


Added by:Adrian Kuegel
Date:2011-07-05
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:German Collegiate Programming Contest 2011 (Authors: Christopher Dennl and Thomas Fersch)