|All-time Popular Problems|
|Sort a stack|
|Delete Middle of Linked List|
|BFS traversal of graph|
|Largest subarray of 0's and 1's|
|Immediate Smaller Element|
|Delete nodes having greater value on right|
|Node at a given index in linked list|
|Two numbers with sum closest to zero|
|Print all nodes that don’t have sibling|
For a positive, non-zero integer N, find the minimum count of numbers of the form X^(i-1), where X is a given non-zero, non-negative integer raised to the power i-1 (1<=i<=12), so that they sum up to form the number N exactly.
First line of input is an integer T denoting the number of test cases. For each test case, there are further two lines of input. First line comprises the integer N and second line comprises the integer X, for which any value of i can be taken in its power to satisfy the condition.
The only line of output is the minimum number of values of X^(i-1), with any value of i in the range 1 to 12, which sum up to the number N exactly. If a particular value of X^(i-1) is taken up two or more times, the count considered will be two or more respectively.
In the first test case, N=10 and X=2, which means the sum of 10 has to be made out of possible values of 2^(i-1).
1+1+1+1+1+1+1+1+1+1=10 , count is 10 here. (1=2^(1-1))
1+1+1+1+1+1+1+1+2=10 , count is 9 here. (2=2^(2-1))
1+1+1+1+1+1+4=10, count is 7 here. (4=2^(3-1))
1+1+8=10, count here is 3. (8=2^(4-1))
2+8=10, count is 2 here, which is minimum of all. Therefore, the output is 2. Similarly, all other results are to be found.
Data type int will be insufficient to store a value as large as 99999999, therefore you can use long int or long long int instead.