CF1175A.From Hero to Zero

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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: 2726252487621027 \rightarrow 26 \rightarrow 25 \rightarrow 24 \rightarrow 8 \rightarrow 7 \rightarrow 6 \rightarrow 2 \rightarrow 1 \rightarrow 0 .

You are asked to calculate the minimum number of steps to reach 00 from nn .

输入格式

The first line contains one integer tt ( 1t1001 \le t \le 100 ) — the number of queries.

The only line of each query contains two integers nn and kk ( 1n10181 \le n \le 10^{18} , 2k10182 \le k \le 10^{18} ).

输出格式

For each query print the minimum number of steps to reach 00 from nn in single line.

输入输出样例

  • 输入#1

    2
    59 3
    1000000000000000000 10
    

    输出#1

    8
    19
    

说明/提示

Steps for the first test case are: 5958571918621059 \rightarrow 58 \rightarrow 57 \rightarrow 19 \rightarrow 18 \rightarrow 6 \rightarrow 2 \rightarrow 1 \rightarrow 0 .

In the second test case you have to divide nn by kk 1818 times and then decrease nn by 11 .

首页