All problems

Problem 238

Convert to Base B

Easy · 10 pts · Math, String

Problem

You are given a non-negative integer N and a base B between 2 and 36. Print N written in base B. Digit values 0 to 9 are written as usual, and values 10 to 35 are written as the uppercase letters A to Z (A = 10, B = 11, ..., Z = 35). Zero is written as 0 in every base. Repeatedly take N mod B as the next digit, from the least significant upwards, and divide N by B until it reaches 0. Then print the digits in reverse order. The numbers in this problem can be larger than a 32-bit int can hold (about 2.1 × 10^9), so use a 64-bit integer type: long long in C++, long in Java (Python needs nothing special).

Input

Two integers N and B.

Output

Print N in base B.

Constraints

0 <= N <= 10^18; 2 <= B <= 36

Example input

255 16

Example output

FF

Explanation

255 divided by 16 is 15 with remainder 15, and 15 divided by 16 is 0 with remainder 15. Both digits are 15, written as F, so 255 in base 16 is FF.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Count Digit One Next: Nth Ugly Number