Skills101
← All coding problems

Convert Between Number Bases

easyMath & Number Theory+10 XPTime limit 2s

Convert numbers from one base to another. Digits above 9 are written with capital letters: A = 10, B = 11, …, Z = 35. For example FF in base 16 is 255 in base 10 and 11111111 in base 2.

Input:

  • line 1: q
  • next q lines: x b1 b2 — a number x written in base b1, to be rewritten in base b2

Output: q lines — each number in base b2, using capital letters and no leading zeros (zero is printed as 0).

Constraints: 1 ≤ q ≤ 10⁴, 2 ≤ b1, b2 ≤ 36, every digit of x is valid in base b1, x has no leading zeros, and its value is at most 10¹⁸.

Think about it: go through base 10 in the middle. Read x left to right with value = value · b1 + digit. Then write the value in base b2 by taking value % b2 repeatedly (those are the digits from right to left).

Sample input 1

3
FF 16 10
255 10 2
101 2 36

Sample output 1

255
11111111
5

Sample input 2

2
0 10 2
ZZ 36 10

Sample output 2

0
1295

Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).

Loading editor…

Log in to run and submit your code — it's free.