A. From Hero to Zero

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值