Under construction

Still being built. Things may change

Fibonacci Products

Time limit20sMemory limit1024 MB

Summary
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 F1=1F_1 = 1, F2=2F_2 = 2, and Fi=Fi−1+Fi−2F_i = F_{i-1} + F_{i-2} for i≥3i \ge 3. Every positive integer xx is uniquely written as a bit string (b1,b2,…,bn)(b_1, b_2, \ldots, b_n) for some n≥1n \ge 1 that satisfies the following conditions.

  • x=b1F1+b2F2+…+bnFnx = b_1F_1 + b_2F_2 + \ldots + b_nF_n
  • For all 1≤i<n1 \le i < n, bi∈{0,1}b_i \in \{0, 1\}, and bn=1b_n = 1. There are no leading zeros.
  • For all 1≤i<n1 \le i < n, bibi+1=0b_ib_{i+1} = 0. No two adjacent bits are both 1.

For example, 2=(0,1)2 = (0, 1), 4=(1,0,1)4 = (1, 0, 1), 5=(0,0,0,1)5 = (0, 0, 0, 1), and 20=(0,1,0,1,0,1)20 = (0, 1, 0, 1, 0, 1), because 20=F2+F4+F6=2+5+1320 = F_2 + F_4 + F_6 = 2 + 5 + 13.

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 tt (1≤t≤10001 \le t \le 1000). The next 2t2t lines describe the test cases, two lines per case. The first line of a case gives the Fibonacci representation of a positive integer xx: first its length nn, then the nn bits b1,b2,…,bnb_1, b_2, \ldots, b_n. The second line gives the representation of yy in the same format. The total length of all representations in the input does not exceed 10610^6.

Output

For each test case, print the product x×yx \times y in the Fibonacci system, in the same format as the input: first its length n′n', then the n′n' 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 (1,0,1)(1, 0, 1) and (0,0,0,1)(0, 0, 0, 1). Expanding gives (1F1+0F2+1F3)×(0F1+0F2+0F3+1F4)=(1+3)×5=20=F2+F4+F6(1F_1 + 0F_2 + 1F_3) \times (0F_1 + 0F_2 + 0F_3 + 1F_4) = (1 + 3) \times 5 = 20 = F_2 + F_4 + F_6. The result is therefore (0,1,0,1,0,1)(0, 1, 0, 1, 0, 1), with length 6.

Examples1

  1. Example 1

    Input
    2
    3 1 0 1
    4 0 0 0 1
    2 0 1
    1 1
    
    Expected output
    6 0 1 0 1 0 1
    2 0 1