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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <numeric>
#include <string>
#include <utility>
#include <tuple>
#include <vector>

constexpr int big_int_base = 1 << 30;
constexpr std::size_t naive_multiplication_limit = 20;

class big_unsigned {
public:
	big_unsigned() = default;
	big_unsigned(int value) {
		*this = value;
	}
	bool operator!() const {
		return digits.empty();
	}
	friend bool operator==(const big_unsigned& lhs, const big_unsigned& rhs) {
		return lhs.digits == rhs.digits;
	}
	friend bool operator!=(const big_unsigned& lhs, const big_unsigned& rhs) {
		return !(lhs == rhs);
	}
	friend bool operator<(const big_unsigned& lhs, const big_unsigned& rhs) {
		if (lhs.digits.size() < rhs.digits.size())
			return true;
		if (lhs.digits.size() > rhs.digits.size())
			return false;
		return std::lexicographical_compare(lhs.digits.rbegin(), lhs.digits.rend(), rhs.digits.rbegin(), rhs.digits.rend());
	}
	friend bool operator>(const big_unsigned& lhs, const big_unsigned& rhs) {
		return rhs < lhs;
	}
	friend bool operator<=(const big_unsigned& lhs, const big_unsigned& rhs) {
		return !(rhs < lhs);
	}
	friend bool operator>=(const big_unsigned& lhs, const big_unsigned& rhs) {
		return !(lhs < rhs);
	}
	big_unsigned& operator=(int other) {
		if (other != 0)
			digits = {other};
		else
			digits.clear();
		return *this;
	}
	big_unsigned& operator*=(int other) {
		long long n = other;
		long long carry = 0;
		for (auto&& digit : digits) {
			long long new_digit = digit * n + carry;
			digit = new_digit % big_int_base;
			carry = new_digit / big_int_base;
		}
		while (carry > 0) {
			digits.push_back(carry % big_int_base);
			carry /= big_int_base;
		}
		return *this;
	}
	big_unsigned& operator+=(const big_unsigned& other) {
		if (&other == this)
			return *this *= 2;
		if (other.digits.size() > digits.size()) {
			digits.reserve(other.digits.size() + 1);
			digits.resize(other.digits.size());
		}
		add(other.digits.begin(), other.digits.end());
		return *this;
	}
	big_unsigned& operator-=(const big_unsigned& other) {
		if (&other == this)
			return *this = 0;
		bool carry = false;
		auto this_it = digits.begin();
		for (auto it = other.digits.begin(); it != other.digits.end(); ++it, ++this_it) {
			auto& digit = *this_it;
			digit -= *it + carry;
			carry = digit < 0;
			if (carry)
				digit += big_int_base;
		}
		for (; carry && this_it != digits.end(); ++this_it) {
			auto& digit = *this_it;
			digit--;
			carry = digit < 0;
			if (carry)
				digit += big_int_base;
		}
		while (!digits.empty() && digits.back() == 0) {
			digits.pop_back();
		}
		return *this;
	}
	big_unsigned& operator*=(const big_unsigned& other) {
		return *this = multiply(digits.begin(), digits.end(), other.digits.begin(), other.digits.end());
	}
	friend big_unsigned operator*(big_unsigned&& lhs, int rhs) {
		big_unsigned result = std::move(lhs);
		return result *= rhs;
	}
	friend big_unsigned operator*(int lhs, big_unsigned&& rhs) {
		big_unsigned result = std::move(rhs);
		return result *= lhs;
	}
	friend big_unsigned operator+(const big_unsigned& lhs, const big_unsigned& rhs) {
		if (lhs.digits.size() < rhs.digits.size()) {
			big_unsigned result = rhs;
			return result += lhs;
		}
		big_unsigned result = lhs;
		return result += rhs;
	}
	friend big_unsigned operator+(const big_unsigned& lhs, big_unsigned&& rhs) {
		big_unsigned result = std::move(rhs);
		return result += lhs;
	}
	friend big_unsigned operator+(big_unsigned&& lhs, const big_unsigned& rhs) {
		big_unsigned result = std::move(lhs);
		return result += rhs;
	}
	friend big_unsigned operator+(big_unsigned&& lhs, big_unsigned&& rhs) {
		if (lhs.digits.capacity() < rhs.digits.capacity()) {
			big_unsigned result = std::move(rhs);
			return result += lhs;
		}
		big_unsigned result = std::move(lhs);
		return result += rhs;
	}
	friend big_unsigned operator-(const big_unsigned& lhs, const big_unsigned& rhs) {
		big_unsigned result = lhs;
		return result -= rhs;
	}
	friend big_unsigned operator-(big_unsigned&& lhs, const big_unsigned& rhs) {
		big_unsigned result = std::move(lhs);
		return result -= rhs;
	}
	friend big_unsigned operator*(const big_unsigned& lhs, const big_unsigned& rhs) {
		return multiply(lhs.digits.begin(), lhs.digits.end(), rhs.digits.begin(), rhs.digits.end());
	}
private:
	template<class ForwardIt>
	void add(ForwardIt first, ForwardIt last) {
		bool carry = false;
		auto this_it = digits.begin();
		for (auto it = first; it != last; ++it, ++this_it) {
			auto& digit = *this_it;
			digit += *it + carry;
			carry = digit >= big_int_base;
			if (carry)
				digit -= big_int_base;
		}
		for (; carry && this_it != digits.end(); ++this_it) {
			auto& digit = *this_it;
			digit++;
			carry = digit >= big_int_base;
			if (carry)
				digit -= big_int_base;
		}
		if (carry) {
			digits.push_back(1);
		} else {
			while (!digits.empty() && digits.back() == 0) {
				digits.pop_back();
			}
		}
	}
	template<class ForwardIt1, class ForwardIt2>
	static big_unsigned add(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, ForwardIt2 last2) {
		big_unsigned result;
		auto size1 = std::distance(first1, last1);
		auto size2 = std::distance(first2, last2);
		if (size1 < size2) {
			result.digits.reserve(size2 + 1);
			result.digits.assign(first2, last2);
			result.add(first1, last1);
		} else {
			result.digits.reserve(size1 + 1);
			result.digits.assign(first1, last1);
			result.add(first2, last2);
		}
		return result;
	}
	template<class ForwardIt1, class ForwardIt2>
	static big_unsigned multiply(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, ForwardIt2 last2) {
		auto size1 = std::distance(first1, last1);
		auto size2 = std::distance(first2, last2);
		if (size1 == 0 || size2 == 0)
			return {};
		if (size1 < naive_multiplication_limit || size2 < naive_multiplication_limit)
			return naive_multiply(first1, last1, first2, last2);
		return karatsuba(first1, last1, first2, last2);
	}
	template<class ForwardIt1, class ForwardIt2>
	static big_unsigned naive_multiply(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, ForwardIt2 last2) {
		big_unsigned result;
		result.digits.resize(std::distance(first1, last1) + std::distance(first2, last2));
		auto out = result.digits.begin();
		for (auto it = first1; it != last1; ++it, ++out) {
			long long digit = *it;
			long long carry = 0;
			auto out2 = out;
			for (auto it2 = first2; it2 != last2; ++it2, ++out2) {
				long long new_digit = *out2;
				new_digit += digit * *it2 + carry;
				*out2 = new_digit % big_int_base;
				carry = new_digit / big_int_base;
			}
			*out2 = (int)carry;
		}
		while (!result.digits.empty() && result.digits.back() == 0) {
			result.digits.pop_back();
		}
		return result;
	}
	template<class ForwardIt1, class ForwardIt2>
	static big_unsigned karatsuba(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, ForwardIt2 last2) {
		auto size1 = std::distance(first1, last1);
		auto size2 = std::distance(first2, last2);
		auto half = std::max(size1, size2) / 2;
		auto middle1 = first1;
		auto middle2 = first2;
		std::advance(middle1, std::min(size1, half));
		std::advance(middle2, std::min(size2, half));
		auto z0 = multiply(first1, middle1, first2, middle2);
		auto z2 = multiply(middle1, last1, middle2, last2);
		auto z1 = add(first1, middle1, middle1, last1) * add(first2, middle2, middle2, last2) - z0 - z2;
		z1.digits.insert(z1.digits.begin(), half, 0);
		z2.digits.insert(z2.digits.begin(), half * 2, 0);
		return std::move(z0) + std::move(z1) + std::move(z2);
	}
	std::vector<int> digits;
};

constexpr std::size_t fib_count = 75'000;
big_unsigned fib[fib_count] {1, 1};

big_unsigned to_big_unsigned(const std::vector<int>& x) {
	int n = (int)x.size();
	int s = std::max(2, (int)std::sqrt(n));
	std::vector<big_unsigned> y((n - 1) / s + 1);
	std::vector<big_unsigned> z((n - 1) / s + 1);
	for (int i = 0; i < n; i++) {
		if (x[i]) {
			if (i % s == 0) {
				y[i / s] = 1;
			} else if (i % s == 1) {
				z[i / s] = 1;
			} else {
				y[i / s] += fib[i % s - 2];
				z[i / s] += fib[i % s - 1];
			}
		}
	}
	for (auto i = y.size(); --i;) {
		y[i - 1] += y[i] * fib[s - 2] + z[i] * fib[s - 1];
		z[i - 1] += y[i] * fib[s - 1] + z[i] * fib[s];
	}
	return y[0] + z[0] * 2;
}

big_unsigned load() {
	int n;
	std::cin >> n;
	std::vector<int> x(n);
	for (auto&& b : x) {
		std::cin >> b;
	}
	return to_big_unsigned(x);
}

void write(big_unsigned&& x) {
	std::vector<int> result;
	int bound = fib_count;
	while (!!x) {
		int a = 0;
		int b = bound;
		while (b - a > 1) {
			auto mid = a + (b - a) / 2;
			if (fib[mid] <= x)
				a = mid;
			else
				b = mid;
		}
		x -= fib[a];
		if ((int)result.size() < a)
			result.resize(a);
		result[a - 1] = 1;
		bound = a - 1;
	}
	std::cout << result.size();
	for (const auto& b : result) {
		std::cout << ' ' << b;
	}
	std::cout << '\n';
}

int main() {
	for (std::size_t i = 2; i < fib_count; i++) {
		fib[i] = fib[i - 1] + fib[i - 2];
	}
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(nullptr);
	int t;
	std::cin >> t;
	while (t--) {
		write(load() * load());
	}
	return 0;
}