Weird Numbers

Interview

Time limit1sMemory limit128 MB

Summary
Convert numbers between decimal and a given negative base representation, in both directions, for multiple queries until an end marker.
Level

Medium4 of 10

Topics
Math, Implementation, Simulation
Solved
No attempts yet

Problem

Binary numbers form the principal basis of computer science. Most people have heard of other systems, such as ternary, octal, or hexadecimal, and know how to use them and convert numbers between them. But did you know that the base (radix) of a number system can also be negative? A university lecturer recently encountered negabinary numbers and other systems with a negative base. Will you help convert numbers to and from these systems?

A number written in a system with a positive base RR is a string of digits between 00 and R−1R-1 inclusive. A digit at position PP (positions are counted from right to left, starting at zero) represents a value of RPR^{P}: the digit is multiplied by RPR^{P}, and the values of all positions are summed. For example, in the octal system (R=8R = 8) the number 1702417024 has the value

1×84+7×83+0×82+2×81+4×80=4096+3584+16+4=7700.1\times 8^{4} + 7\times 8^{3} + 0\times 8^{2} + 2\times 8^{1} + 4\times 8^{0} = 4096 + 3584 + 16 + 4 = 7700.

With a negative radix −R-R the principle is the same: a digit at position PP has a value of (−R)P(-R)^{P}. For example, the number 1702417024 in base −8-8 equals

1×(−8)4+7×(−8)3+0×(−8)2+2×(−8)1+4×(−8)0=4096−3584−16+4=500.1\times(-8)^{4} + 7\times(-8)^{3} + 0\times(-8)^{2} + 2\times(-8)^{1} + 4\times(-8)^{0} = 4096 - 3584 - 16 + 4 = 500.

One big advantage of a negative base is that no minus sign is needed to write negative numbers. Here are some examples for the negabinary system (R=−2R = -2):

decimalnegabinarydecimalnegabinarydecimalnegabinary
-101010-311014100
-91011-2105101
-81000-111611010
-7100100711011
-6111011811000
-511112110911001
-4110031111011110

When no leading zeros are allowed, the representation of any integer in a negative base is unique; the only number that may start with the digit 0 is zero itself.

Input

The input contains several conversions, one per line.

A conversion from decimal to a negative-base system begins with the lowercase word to, immediately followed (no space) by a minus sign and the base RR, then one space and a decimal number NN. For example, to-2 10 asks for 1010 written in base −2-2.

A conversion to decimal begins with the lowercase word from, immediately followed by a minus sign and the base RR, then one space and a number written in base −R-R. For example, from-2 1010 asks for the decimal value of 10101010 read in base −2-2.

The input ends with a line containing the lowercase word end. All numbers satisfy 2≤R≤102 \le R \le 10 and −1,000,000≤N≤1,000,000-1{,}000{,}000 \le N \le 1{,}000{,}000 (in decimal).

Output

For each conversion, print one number on its own line. If the conversion started from decimal, print the same value written in base −R-R. If the conversion was to decimal, print the decimal value.

Neither the input nor the output numbers contain leading zeros. A minus sign - may appear only with a negative number written in decimal; any non-negative number, or any number written in a negative base, must not begin with one.

Examples6

  1. Example 1

    Input
    to-2 10
    from-2 1010
    to-10 10
    to-10 -10
    from-10 10
    end
    
    Expected output
    11110
    -10
    190
    10
    -10
    
  2. Example 2

    Input
    to-2 0
    from-2 0
    to-10 0
    from-10 0
    end
    
    Expected output
    0
    0
    0
    0
    
  3. Example 3

    Input
    from-8 17024
    to-8 500
    end
    
    Expected output
    500
    17024
    
  4. Example 4

    Input
    to-2 -10
    to-2 -5
    to-2 6
    to-2 -1
    to-2 1
    end
    
    Expected output
    1010
    1111
    11010
    11
    1
    
  5. Example 5

    Input
    from-2 11011
    from-2 11000
    from-2 11110
    from-2 11
    end
    
    Expected output
    7
    8
    10
    -1
    
  6. Example 6

    Input
    to-5 -100
    end
    
    Expected output
    1100