Educational Codeforces Round 66 (Rated for Div. 2) A

A. From Hero to Zero

题目链接:http://codeforces.com/contest/1175/problem/A

题目

ou 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.

input

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).

output

For each query print the minimum number of steps to reach 0
from n in single line

Example

intput

2
59 3
1000000000000000000 10

output

8

19

题意

给你两个数n,k,你需要将n每次经过以下两个**步骤之一**从而得到0,输出变换次数:
要么n=n-1,要么将n=n/k(前提n能整除k)。

思路

范围太大,暴力绝对TLE,尝试就是浪费生命!
巧办法:
n%k!=0时,变换的步数就是n%k的值,此时当前n=(n-减掉该步数)
n%k==0时,变换的步数就是1,此时当前n=n/k,
n为0结束,将步数累加输出即可。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+7;
int main()
{
    int T;
    cin>>T;
    while(T--) {
        ll n, k;
        cin >> n >> k;
        ll result = 0;
        while (n != 0) {
            ll book = n % k;
            if (book != 0) {
                result += book;
                n = n - book;
            } else {
                result += 1;
                n = n / k;
            }
        }
        cout << result << endl;
    }
return 0;
}

原文地址:https://www.cnblogs.com/Vampire6/p/10990374.html

时间: 2024-08-30 08:39:24

Educational Codeforces Round 66 (Rated for Div. 2) A的相关文章

Educational Codeforces Round 66 (Rated for Div. 2)

a题教育我:不手写2个测试用例,就不要写代码,谢谢. b题真的恶心,判断溢出自己之前从没思考过的问题.最后用了很尴尬的判断,a=b+c的时候,三个数不能>=(1<<32),不能小于0,不知道为什么,也不想知道. c题是思维gap的味道,也靠推导能力,看你有没有思维jump的能力.用到了我特别喜欢的一种思考模式:设想答案长什么样子.答案肯定是中点离着两边一样远,才能最小化d(k),然后这个区间必然是覆盖了k个点,所以排序后固定长度的滑窗走起- d题.原来每次打比赛之前,我都在本子上手写两条

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai. You can move these disks

Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations

原文链接:https://www.cnblogs.com/xwl3109377858/p/11405773.html Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations You are given a sequence of n pairs of integers: (a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is

Educational Codeforces Round 36 (Rated for Div. 2) 题解

Educational Codeforces Round 36 (Rated for Div. 2) 题目的质量很不错(不看题解做不出来,笑 Codeforces 920C 题意 给定一个\(1\)到\(n\)组成的数组,只可以交换某些相邻的位置,问是否可以将数组调整为升序的 解题思路 首先如果每个数都能通过交换到它应该到的位置,那么就可以调整为升序的. 但实际上交换是对称的,如果应该在的位置在当前位置前方的数都交换完成,那么整体就是排好序的,因为不可能所有不在相应位置的数都在相应位置的后方.

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://codeforces.com/contest/985/problem/E Description Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome w

Educational Codeforces Round 55 (Rated for Div. 2)

Educational Codeforces Round 55 (Rated for Div. 2) 链接 A Vasya and Book 傻逼题..注意判边界. #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<set> #include<map> #include<vector> #include<cm

Educational Codeforces Round 57 (Rated for Div. 2)

get人生第二场CF! 成绩:(exACM) rank858 AC3/7 Penalty57 rating1648(+52) 题目:Educational Codeforces Round 57 (Rated for Div. 2) 错题题解: D. Easy Problem E. The Top Scorer F. Inversion Expectation G. Lucky Tickets 原文地址:https://www.cnblogs.com/xht37/p/10198321.html

Educational Codeforces Round 58 (Rated for Div. 2)(待更新)

get人生第七场CF! 成绩:(exACM) rank AC3/7 Penalty104 rating() 题目:Educational Codeforces Round 58 (Rated for Div. 2) 错题题解: C. Division and Union 原文地址:https://www.cnblogs.com/xht37/p/10260260.html