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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <bits/stdc++.h>
#define MULTIPLE_TESTS
// #define ENDLESS_TESTS
#define MEMORY_LIMIT 512

using namespace std;

auto read = [](auto &vec)
{
    int n;
    cin >> n;
    vec.reserve((n+1)/2);

    for (int i = 0; i < n; ++i)
    {
        int val;
        cin >> val;
        if (val == 1)
            vec.push_back(i);
    }
};

struct SingleResult
{
    int single_pos = -1;
    int first_pos = 0;
    int count = 0;
};

SingleResult mul(int a, int b)
{
    if (a > b) swap(a, b);

    SingleResult ret;
    ret.count = a / 2 + 1;

    const bool even = (a%2 == 0);
    ret.first_pos = b-a;

    if (!even)
    {
        const bool equal = (a==b);
        ret.first_pos += 2;
        ret.single_pos = ret.first_pos - (equal ?  2 : 3);
    }

    return ret;
}

void test()
{
    vector<int> x;
    vector<int> y;
    read(x);
    read(y);

    // if (1LL * x.size() * y.size() > 1000000000)
    // {
    //     cout << "Too much data\n";
    //     return;
    // }

    vector<int> result;

    auto at = [&](int idx) -> int&
    {
        if (idx >= static_cast<int>(result.size()))
            result.resize(idx+1, 0);
        return result[idx];
    };

    auto fix = [&result, &at]()
    {
        int i = result.size()-1;

        while (i >= 0)
        {
            // cout << "fix @ " << i << endl;

            // for (int j = i - 3; j < i + 3 && j < result.size(); ++j)
            //     cout << result[j] << ' ';
            // cout << endl;

            if (result[i] == 0)
            {
                i -= 1;
                continue;
            }

            if (i == 0)
            {
                if (result[0] == 1)
                    break;

                at(1) += result[0]/2;
                result[0] %= 2;
                i = 1;
                continue;
            }

            if (at(i+1) > 0)
            {
                i += 1;
                continue;
            }

            if (result[i] == 1 && result[i-1] == 0)
            {
                i -= 1;
                continue;
            }

            if (result[i] > 0 && result[i-1] > 0)
            {
                const int step = min(result[i], result[i-1]);
                result[i-1] -= step;
                result[i] -= step;
                at(i+1) += step;
                i += 1;
                continue;
            }

            assert(result[i] > 0);

            if (i == 1)
            {
                const int step = max(1, result[i]/3);
                result[1] -= step;
                result[0] += 2*step;
                continue;
            }

            const int step = max(1, result[i]/2);
            result[i] -= step;
            result[i-1] += step;
            result[i-2] += step;
            continue;
        }

        while(result.back() == 0)
            result.pop_back();
    };

    auto print = [&]()
    {
        cout << result.size();
        for (auto v : result)
            cout << ' ' << v;
        cout << '\n';
    };

    vector<int> changes;

    auto changes_at = [&](int idx) -> int&
    {
        if (idx >= static_cast<int>(changes.size()))
            changes.resize(idx+1, 0);
        return changes[idx];
    };

    for (auto a : x)
        for (auto b : y)
        {
            auto tmp = mul(a, b);

            if (tmp.single_pos != -1)
                at(tmp.single_pos) += 1;

            changes_at(tmp.first_pos) += 1;
            changes_at(tmp.first_pos + 4*tmp.count) -= 1;
        }

    {
        array<int, 4> state = {0, 0, 0, 0};
        result.resize(changes.size(), 0);

        for (auto i = 0u; i < changes.size(); ++i)
        {
            auto &s = state[i%4];
            s += changes[i];
            result[i] += s;
        }
    }

    fix();
    print();
}

/******************************************************************************/

int main()
{
#if defined(CONTEST_WORKSPACE) && defined(MEMORY_LIMIT)
    void limit_ram(float);
    limit_ram(MEMORY_LIMIT);
#endif

#if !defined(CONTEST_WORKSPACE)
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
#endif

#if defined(MULTIPLE_TESTS)
    int T = 0;
    cin >> T;

    while (T --> 0)
        test();

#elif defined(ENDLESS_TESTS)
    while(!(cin >> std::ws).eof())
        test();

#else
    test();
#endif

    return EXIT_SUCCESS;
}