Fibonacci Products
Time limit20sMemory limit1024 MB
Given two positive integers in Zeckendorf form, print their product in the same Fibonacci representation. Total input length is up to 10^6 bits.
- Level
Hard9 of 10
- Topics
- Dynamic programming, String, Math
- Solved
- No attempts yet
Problem
The brilliant computer scientist Bajtazar has spent more than 15 years building an unusual computer. It stores numbers in the Fibonacci system, where each number is written as a sum of distinct, non-consecutive Fibonacci numbers.
Formally, define , , and for . Every positive integer is uniquely written as a bit string for some that satisfies the following conditions.
- For all , , and . There are no leading zeros.
- For all , . No two adjacent bits are both 1.
For example, , , , and , because .
Long ago, Bajtazar asked Olympiad participants to help implement addition in the Fibonacci system. That story took place during the Fibonacci Sums problem from the second stage of the XII OI and is described in the Olympiad's 'blue booklet'. This time the task is harder, and Bajtazar has been stuck on it for several years. He asks Potyczki Algorytmiczne participants for advice. Help him implement multiplication!
Input
The first line contains the number of test cases (). The next lines describe the test cases, two lines per case. The first line of a case gives the Fibonacci representation of a positive integer : first its length , then the bits . The second line gives the representation of in the same format. The total length of all representations in the input does not exceed .
Output
For each test case, print the product in the Fibonacci system, in the same format as the input: first its length , then the bits. Separate the numbers on each line with single spaces. Representations in the input and output must satisfy the conditions of the problem, so each bit string must end with a 1 and must not contain two adjacent 1s.
Hint
In the first test case, we multiply the numbers represented by and . Expanding gives . The result is therefore , with length 6.