CF1175A.From Hero to Zero
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given an integer n and an integer k .
In one step you can do one of the following moves:
- decrease n by 1 ;
- divide n by k if n is divisible by k .
For example, if n=27 and k=3 you can do the following steps: 27→26→25→24→8→7→6→2→1→0 .
You are asked to calculate the minimum number of steps to reach 0 from n .
输入格式
The first line contains one integer t ( 1≤t≤100 ) — the number of queries.
The only line of each query contains two integers n and k ( 1≤n≤1018 , 2≤k≤1018 ).
输出格式
For each query print the minimum number of steps to reach 0 from n in single line.
输入输出样例
输入#1
2 59 3 1000000000000000000 10
输出#1
8 19
说明/提示
Steps for the first test case are: 59→58→57→19→18→6→2→1→0 .
In the second test case you have to divide n by k 18 times and then decrease n by 1 .