You are given an integer nn and an integer kk.
In one step you can do one of the following moves:
decrease nn by 11;
divide nn by kk if nn is divisible by kk.
For example, if n=27n=27 and k=3k=3 you can do the following steps: 27→26→25→24→8→7→6→2→1→027→26→25→24→8→7→6→2→1→0.
You are asked to calculate the minimum number of steps to reach 00 from nn.
Input
The first line contains one integer tt (1≤t≤1001≤t≤100) — the number of queries.
The only line of each query contains two integers nn and kk (1≤n≤10181≤n≤1018, 2≤k≤10182≤k≤1018).
Output
For each query print the minimum number of steps to reach 00 from nn in single line.
Example
input
2
59 3
1000000000000000000 10
output
8
19
Note
Steps for the first test case are: 59→58→57→19→18→6→2→1→059→58→57→19→18→6→2→1→0.
In the second test case you have to divide nn by kk 1818 times and then decrease nn by 11.
题意:给一个数n,问n变成0需要的最小步数,能如果能整除k就整除,否则减1;
不会取模运算真的很麻烦,数稍微大一点就TLE。
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxx = 1e5+10;
int main()
{
int t;
cin>>t;
while(t--)
{
long long int n,k;
cin>>n>>k;
long long int count = 0;
while(n!=0)
{
if(n%k!=0)
{
count +=(n%k);
n -=n%k; //不能整除时,减去多余的
}
else
{
n = n/k;
count++;
}
}
printf("%lld\n",count);
}
return 0;
}