Skew Binary

Time limit1sMemory limit128 MB

Summary
Read each skew binary string and print its decimal value, stopping at the terminating 0 line.
Level

Easy3 of 10

Topics
Math, String, Implementation
Solved
No attempts yet

Problem

When a number is written in decimal, the kk-th digit (counting from the right, with the least-significant digit numbered 00) represents a multiple of 10k10^k. For example,

8130710=8×104+1×103+3×102+0×101+7×100=80000+1000+300+0+7=8130781307_{10} = 8\times10^4 + 1\times10^3 + 3\times10^2 + 0\times10^1 + 7\times10^0 = 80000 + 1000 + 300 + 0 + 7 = 81307

When a number is written in binary, the kk-th digit represents a multiple of 2k2^k. For example,

100112=1×24+0×23+0×22+1×21+1×20=16+0+0+2+1=1910011_2 = 1\times2^4 + 0\times2^3 + 0\times2^2 + 1\times2^1 + 1\times2^0 = 16 + 0 + 0 + 2 + 1 = 19

In skew binary, the kk-th digit represents a multiple of 2k+1−12^{k+1} - 1. The only allowed digits are 00 and 11, except that the least-significant nonzero digit may be a 22. For example,

10120skew=1×(25−1)+0×(24−1)+1×(23−1)+2×(22−1)+0×(21−1)=31+0+7+6+0=4410120_{\text{skew}} = 1\times(2^5-1) + 0\times(2^4-1) + 1\times(2^3-1) + 2\times(2^2-1) + 0\times(2^1-1) = 31 + 0 + 7 + 6 + 0 = 44

The first 1010 numbers in skew binary are 0,1,2,10,11,12,20,100,101,0, 1, 2, 10, 11, 12, 20, 100, 101, and 102102. (Skew binary is useful in some applications because you can add 11 with at most one carry. However, that has nothing to do with this problem.)

Input

The input consists of one or more lines, each containing a single integer nn. If n=0n = 0, it signals the end of the input; otherwise nn is a nonnegative integer written in skew binary. The decimal value of nn is at most 231−1=21474836472^{31} - 1 = 2147483647.

Output

For each number, output its decimal equivalent on its own line.

Examples1

  1. Example 1

    Input
    10120
    200000000000000000000000000000
    10
    1000000000000000000000000000000
    11
    100
    11111000001110000101101102000
    0
    
    Expected output
    44
    2147483646
    3
    2147483647
    4
    7
    1041110737