Panda and Combination
Practice
3.6 (15 votes)
Mathematics
Easy Medium
Open
Approved
Greedy algorithm
Mathamatics
Problem
58% Success 6189 Attempts 30 Points 5s Time Limit 256MB Memory 1024 KB Max Code

Panda loves solving problems which are deemed impossible by his fellow classmates. The current problem which he is working on is to express a number N as sum of powers of number X (Not necessarily distinct) such that the number of powers of number X used should be minimum.

Note: The powers of a number can be 0, 1, 2, 3, 4, ...

Input Format:
The first line will contain T, the number of test cases. Then, T lines follow, each containing 2 space separated integers N and M.

Output Format:
For each test case, output the minimum number of such numbers (powers of M) which can be summed up to produce N.

Constraints:

  • Subtask 1: (20 points)
    1 <= T <= 103
    1 <= N, M <= 103

  • Subtask 2: (80 points)
    1 <= T <= 105
    1 <= N, M <= 1014

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
Tags:
Easy-Medium
Points:30
13 votes
Tags:
Basic MathEasy-MediumMathematicsMathematics
Points:30
9 votes
Tags:
MathC++Basic Math