Under construction

Still being built. Things may change

Dividing the Digits of an Integer in Base k

Interview

Time limit1sMemory limit512 MB

Summary
Convert n to base k, split its digits at each 0, and print the sum of the distinct nonempty pieces in base k.
Level

Easy3 of 10

Topics
Implementation, Math
Solved
No attempts yet

Problem

You are given positive integers nn and kk. Let aa be nn written in base kk. Split each digit of aa at the digit 0, and let bb be the set of the resulting numbers. A piece that is empty because of consecutive zeros is not included in bb. Print the sum of the numbers in bb in base kk. For example, if n=19n = 19 and k=2k = 2, then a=100112a = 100112, bb contains 1 and 11, and 1+11=12=110021 + 11 = 12 = 1100_2, so the output is 1100.

Input

The first line contains the positive integers nn and kk, separated by a space.

Output

On the first line, print the sum of the numbers in bb written in base kk.

Constraints

1≤n≤1,000,0001 \le n \le 1{,}000{,}000, 2≤k≤102 \le k \le 10

Examples4

  1. Example 1

    Input
    437674 3
    
    Expected output
    22101
    
  2. Example 2

    Input
    29 3
    
    Expected output
    10
    
  3. Example 3

    Input
    11 3
    
    Expected output
    10
    
  4. Example 4

    Input
    3 3
    
    Expected output
    1