Negative Bases

Problem #95

Tags: computing

Who solved this?

Previous:Arbitrary Base Fractions Next:Arbitrary Base88


By now we should feel comfortable representing any number in any arbitrary base B. In fact if we are given enough symbols to work with then B could be any positive integer between 2 and infinity! But, are there any other bases we may be overlooking?

We can take a moment to observe that base B = 1 doesn't quite work out. We only have a single digit 0 to work with, so while at first we might imagine being a bit creative and using a system similar to tally marks, in practice we find this doesn't at all work like a "typical" base representation.

Let's remind ourselves how "typical" base representations work. Given some number in base B, then for each digit D in position i the decimal representation can be calculated by taking the total sum of D * B^i for each digit. Now it should be clear that while 12 is the base B = 3 representation of the decimal number 5 because (1 * 3^1) + (2 * 3^0) = 5, something like 00000 is not the base B = 1 representation of 5.

We should also be able to convince ourselves that there is no base B = 0.

However what if we keep going in the same direction and cross into the realm of B < 0, what will happen then? We find that our rules for "typical" representations suddenly begin to work again for B <= -2! For example, the representation of the decimal value 1234 in base B = -10 would be 19374, because (1 * (-10)^4) + (9 * (-10)^3) + (3 * (-10)^2) + (7 * (-10)^1) + (4 * (-10)^0)= 1234.

Problem Statement

Input Data
First line will be Q, the quantity of testcases.
Q lines will follow, each with two space-separated integers in the format X B.

Answer
Should consist of Q space-separated values corresponding to the representation of X in base B for each testcase.
All answers should be positive.

Example

input data:
2
1234 -10
-97531 -36

answer:
19374 3XAT
You need to login to get test data and submit solution.