Under construction

Still being built. Things may change

Geek Challenge [SKRZAT] (Base Minus Two)

Interview

Time limit1sMemory limit128 MB

Summary
Convert numbers between decimal and base -2 (Weird Binary), echoing each query with its formatted result.
Level

Easy3 of 10

Topics
Math, Implementation, Number theory
Solved
No attempts yet

Problem

Geek Challenge [SKRZAT] is a very old game from Poland, played on a console with two buttons and a joystick. True to its name, the game communicates in binary: one button means zero and the other means one. It goes one step further and uses a number system whose base is minus two instead of plus two — a representation we will call “Weird Binary”.

In Weird Binary the bit in position ii (counted from the right, starting at 00) has weight (−2)i(-2)^i. The value of a bit string is the sum of the weights of the positions that hold a one. The five-bit patterns and their values are:

Every five-bit pattern, ordered by bit pattern:

BitsValueBitsValueBitsValueBitsValue
00000001000-81000016110008
00001101001-71000117110019
00010-201010-101001014110106
00011-101011-91001115110117
00100401100-410100201110012
00101501101-310101211110113
00110201110-610110181111010
00111301111-510111191111111

The same values, ordered by magnitude:

BitsValueBitsValueBitsValueBitsValue
01010-1000010-21101061001014
01011-900011-11101171001115
01000-80000001100081000016
01001-70000111100191000117
01110-600110211110101011018
01111-500111311111111011119
01100-400100411100121010020
01101-300101511101131010121

Numbers appear on the screen in Weird Binary, and the player answers from the console with a stream of zeroes and ones. Write a program that helps a novice by translating numbers between decimal and Weird Binary.

Input

The first line contains a single integer nn, the number of queries. Each of the next nn lines is one query:

  • a line beginning with b contains a Weird Binary string that must be converted to decimal;
  • a line beginning with d contains a decimal integer that must be converted to Weird Binary.

Every value fits in a 15-bit Weird Binary number, so the decimal range is −10922-10922 to 2184521845 inclusive.

Output

Print one line per query, echoing the query type and its input string together with the converted result:

  • for a b query: From binary: <string> is <decimal>
  • for a d query: From decimal: <string> is <weird binary>

Reproduce the input string exactly (a Weird Binary input may contain leading zeroes). In a produced Weird Binary result leading zeroes are not allowed, and the value 00 is written as 0. Match the spacing shown above exactly.

Examples1

  1. Example 1

    Input
    10
    b 1001101
    b 0111111
    b 101001000100001
    b 010010001000010
    b 100110100110100
    d -137
    d 137
    d 8191
    d -10000
    d 21000
    
    Expected output
    From binary: 1001101 is 61
    From binary: 0111111 is -21
    From binary: 101001000100001 is 19937
    From binary: 010010001000010 is -7106
    From binary: 100110100110100 is 15604
    From decimal: -137 is 10001011
    From decimal: 137 is 110011001
    From decimal: 8191 is 110000000000011
    From decimal: -10000 is 10100100110000
    From decimal: 21000 is 101011000011000