1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <vector>
#include <algorithm>

int fib(int x)
{
    if (x == 1)
    {
        return 1;
    }
    int prev = 1;
    int current = 2;
    for (int i = 3; i <= x; i++)
    {
        int temp = prev;
        prev = current;
        current = prev + temp;
    }
    return current;
}

int main()
{
    int t;
    std::cin >> t;

    int theLongest = 0;
    std::vector<std::string> all(static_cast<unsigned long>(t * 2));

    for (int i = 0; i < t; i++)
    {
        for (int y = 0; y < 2; y++)
        {
            int n;
            std::cin >> n;

            if (theLongest < n)
            {
                theLongest = n;
            }

            std::string bitmask(n, 0);
            for (int x = 0; x < n; x++)
            {
                int number;
                std::cin >> number;

                bitmask[x] = static_cast<char>(number);
            }
            all[i * 2 + y] = bitmask;
        }
    }
    std::vector<int> numbers(t * 2, 0);

    std::vector<int> allFibs;
    for (int x = 1; x <= theLongest; x++)
    {
        int valueOfFib = fib(x);
        allFibs.push_back(valueOfFib);
        for (int i = 0; i < all.size(); i++)
        {
            const auto &bitmask = all[i];
            if (bitmask.length() >= x && bitmask[x - 1])
            {
                numbers[i] += valueOfFib;
            }
        }
    }

    std::vector<int> output(t, 0);
    for (int i = 0; i < t; i++)
    {
        output[i] = numbers[i * 2] * numbers[i * 2 + 1];
    }

    int theHighestOutput = 0;
    for (auto x : output)
    {
        if (x > theHighestOutput)
        {
            theHighestOutput = x;
        }
    }
    while (allFibs.back() < theHighestOutput)
    {
        allFibs.push_back(fib(static_cast<int>(allFibs.size() + 1)));
    }
    std::reverse(allFibs.begin(), allFibs.end());

    for (auto x : output)
    {
        int currentValue = x;
        std::vector<bool> bitmask;
        for (auto fib : allFibs)
        {
            if (currentValue - fib >= 0)
            {
                bitmask.push_back(true);
                currentValue -= fib;
            }
            else if (!bitmask.empty())
            {
                bitmask.push_back(false);
            }
        }
        std::reverse(bitmask.begin(), bitmask.end());

        std::cout << bitmask.size() << " ";
        for (auto bit : bitmask)
        {
            std::cout << bit << " ";
        }
        std::cout << std::endl;
    }
}