Death to Binary?
Time limit1sMemory limit128 MB
Add two numbers written in the Fibonacci base and print the sum in canonical Zeckendorf form, aligned for display.
- Level
Medium6 of 10
- Topics
- Math, Greedy, Implementation, Simulation
- Solved
- No attempts yet
Problem
A group of eccentric calculation enthusiasts has found a wonderful new way to count. Instead of ordinary decimal numbers, they use the Fibonacci base. A number in this base is written as a string of s and s, just like a binary number, but the weight of each digit is not a power of two — it is an element of the Fibonacci progression. This progression starts with and , and for it satisfies (that is, ).
In a representation, the rightmost digit has weight , and each digit further to the left carries the next Fibonacci weight. For example,
Every integer can be expressed in this base, but not necessarily uniquely — for instance, can also be written as . However, for every integer there is a unique representation that contains no two adjacent s; we call it the canonical representation. For example, the canonical representation of is .
To build a computer that calculates in the Fibonacci base, write a program that adds two numbers given in the Fibonacci base (not necessarily in canonical form).
Input
The input consists of several test cases, each on a single line. Each line contains two numbers and in the Fibonacci base, separated by a single space. Each number has at most digits. The end of the input is marked only by the end of file; there is no special terminator.
Output
For each test case, print the following four lines:
- Line 1: the canonical representation of , left-padded with spaces.
- Line 2: a plus sign
+followed by the canonical representation of , left-padded with spaces. - Line 3: two spaces followed by a row of minus signs
-whose length equals the length of the canonical representation of the sum. - Line 4: two spaces followed by the canonical representation of .
Left-pad and with spaces so that the least significant (rightmost) digits of , , and line up in the same column. Put a single blank line between the outputs of consecutive test cases.