Fibonacci Sums
Time limit1sMemory limit128 MB
Given two Zeckendorf representations of positive integers, compute the Zeckendorf representation of their sum.
- Level
Hard8 of 10
- Topics
- Greedy, Math, Number theory, Implementation
- Solved
- No attempts yet
Problem
The Fibonacci numbers are an integer sequence defined as follows: , , and for . The first few terms of the sequence are
The computer scientist Byteazar is building an unusual computer in which numbers are represented in the Fibonacci system: a bit string denotes the number . (Note that is not used.) Unfortunately, this representation is not unique: the same number can have several representations. For example, the number can be written as , , or . For this reason, Byteazar restricts himself to representations satisfying the following two conditions:
- if , then ; that is, the representation has no leading zeros.
- if , then (for ); that is, the representation contains no two (or more) consecutive ones.
Byteazar is having trouble implementing addition. Help him!
Write a program that:
- reads from standard input the representations of two positive integers, and
- computes and writes to standard output the representation of their sum.
Input
The input contains the Fibonacci representations (satisfying the conditions above) of two positive integers and — one on the first line, the other on the second line. Each representation is a sequence of non-negative integers separated by single spaces. The first number on the line is the length of the representation, where . It is followed by zeros and/or ones.
Output
On the only line of output, write the Fibonacci representation (satisfying the conditions above) of the sum . The representation must be a sequence of non-negative integers separated by single spaces, as described in the Input section. The first number on the line is the length of the representation, where , followed by zeros and/or ones.