CF1175A.From Hero to Zero
入门
通过率:0%
时间限制:1.00s
内存限制:256MB
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.
给你两个整数 n 和 k。
每一步你可以执行以下操作之一:
- 将 n 减 1;
- 若 n 能被 k 整除,则将 n 除以 k。
例如,当 n=27 且 k=3 时,你可以执行如下步骤:27→26→25→24→8→7→6→2→1→0。
你需要计算从 n 到达 0 所需的最少步数。
输入格式
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).
第一行包含一个整数 t(1≤t≤100)—— 查询次数。
每个查询仅一行,包含两个整数 n 和 k(1≤n≤1018,2≤k≤1018)。
输出格式
For each query print the minimum number of steps to reach 0 from n in single line.
对于每个查询,在单独一行中输出从 n 到达 0 所需的最少步数。
输入输出样例
输入#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.
第一个测试用例的步骤为:59→58→57→19→18→6→2→1→0。
在第二个测试用例中,你需要将 n 除以 k 共 18 次,然后将 n 减 1。
输入解题思路,AI测评打分。不知道怎么写?