#include <cstdint> #include <cstdio> #include <cstdlib> #include <algorithm> #include <numeric> #include <vector> using fenc_t = std::vector<int64_t>; // #define SHOWDEBUG static fenc_t read_fibonacci() { int n; (void)scanf("%d", &n); fenc_t ret; ret.reserve(n); for (int i = 0; i < n; i++) { int x; (void)scanf("%d", &x); ret.push_back(x); } return ret; } static void write_fibonacci(const fenc_t& f) { printf("%d", (int)f.size()); for (auto x : f) { printf(" %d", (int)x); } putchar('\n'); } static void cut_zeros(fenc_t& f) { while (f.size() > 0 && f.back() == 0) { f.pop_back(); } } static void ensure_minimal(fenc_t& f) { while (f.size() < 4 || f.back() != 0) { f.push_back(0); } } static void sum_normalization_remove_twos(fenc_t& f) { ensure_minimal(f); #ifdef SHOWDEBUG // write_fibonacci(f); #endif int64_t combined = 0; for (int pos = (int)f.size() - 1; pos >= 0; pos--) { auto fix = [&](int64_t a, int64_t b, int64_t c, int64_t d) { if (d != 0) { f[pos - 1] += d; } f[pos + 0] = c; f[pos + 1] = b; f[pos + 2] = a; }; combined = ((combined << 4) & 0xFFF) | f[pos]; switch (combined) { case 0x020: if (pos > 0) { fix(1, 0, 0, 1); } break; case 0x030: if (pos > 0) { fix(1, 1, 0, 1); } break; case 0x021: fix(1, 1, 0, 0); break; case 0x012: fix(1, 0, 1, 0); break; } // write_fibonacci(f); } // Final cleanup if (f[1] == 3) { f[0] = 1; f[1] = 1; f[2] = 1; } else if (f[1] == 2) { if (f[2] == 0) { f[0] += 1; f[1] = 0; f[2] = 1; } else { f[0] = 0; f[1] = 1; f[2] = 0; f[3] = 1; } } if (f[0] == 3) { f[0] = 1; f[1] = 1; } else if (f[0] == 2) { if (f[1] == 0) { f[0] = 0; f[1] = 1; } else { f[0] = 1; f[1] = 0; f[2] = 1; } } // No 2's or 3's should be left } static void sum_normalization_remove_ones(fenc_t& f) { ensure_minimal(f); int i = 0; auto op = [&] { if (f[i] == 1 && f[i + 1] == 1 && f[i + 2] == 0) { f[i] = 0; f[i + 1] = 0; f[i + 2] = 1; return 2; } else { return 1; } }; while (i + 3 <= (int)f.size()) { i += op(); } i = f.size() - 3; while (i >= 0) { i -= op(); } } // Makes a sequence resulting from piecewise addition of two fibonacci-encoded // numbers into a real fibonacci-encoded number static void normalize_after_addition(fenc_t& f) { // write_fibonacci(f); sum_normalization_remove_twos(f); // write_fibonacci(f); // write_fibonacci(f); sum_normalization_remove_ones(f); } static fenc_t add_two_fibs(fenc_t a, const fenc_t& b) { if (a.size() < b.size()) { a.resize(b.size(), 0); } for (size_t i = 0; i < b.size(); i++) { a[i] += b[i]; } // The important, non-trivial part of addition normalize_after_addition(a); return a; } // Normalizes a totally-denormalized vector of ints // O(n * log (max value in vector)) static fenc_t turbo_normalize(const fenc_t& f) { const uint64_t max_num = (uint64_t)*std::max_element(f.begin(), f.end()); uint64_t layer_bit = 0; while ((max_num & layer_bit) != max_num) { layer_bit = (layer_bit << 1) | 1; } layer_bit = (layer_bit + 1) >> 1; fenc_t ret; fenc_t slice; while (layer_bit > 0) { slice.resize(f.size(), 0); for (size_t i = 0; i < f.size(); i++) { slice[i] = (f[i] & layer_bit) ? 1 : 0; } // There may be consecutive bits in this slice, so perform second // normalization step sum_normalization_remove_ones(slice); #ifdef SHOWDEBUG printf("Adding "); write_fibonacci(slice); #endif // Add ret to itself for (auto& x : ret) { x *= 2; } normalize_after_addition(ret); #ifdef SHOWDEBUG printf("Normalized "); write_fibonacci(ret); #endif // Now add slice to ret ret = add_two_fibs(std::move(ret), slice); #ifdef SHOWDEBUG printf("Result "); write_fibonacci(ret); #endif layer_bit >>= 1; } return ret; } static fenc_t multiply_meh(const fenc_t& a, const fenc_t& b) { std::vector<int64_t> events; fenc_t ret; events.resize(a.size() + b.size() + 1 + 4, 0); ret.resize(a.size() + b.size() + 1 + 4, 0); std::vector<size_t> ones_a, ones_b; for (size_t i = 0; i < a.size(); i++) { if (a[i] == 1) { ones_a.push_back(i); } } for (size_t i = 0; i < b.size(); i++) { if (b[i] == 1) { ones_b.push_back(i); } } // Prepare events for (const auto a_it : ones_a) { for (const auto b_it : ones_b) { auto a_pos = a_it; auto b_pos = b_it; if (a_pos > b_pos) { std::swap(a_pos, b_pos); } #ifdef SHOWDEBUG printf(" (pair %d %d)\n", (int)a_pos, (int)b_pos); #endif const size_t last = a_pos + b_pos; const size_t length = 4 * (a_pos / 2); if (a_pos % 2 == 1) { if (a_pos == b_pos) { if (a_pos > 0) { ret[0] += 1; } } else { ret[last - length - 3] += 1; } } events[last - length] += 1; events[last + 4] -= 1; #ifdef SHOWDEBUG write_fibonacci(ret); write_fibonacci(events); #endif } } #ifdef SHOWDEBUG write_fibonacci(a); write_fibonacci(b); #endif // Walk through events and complete `ret` int64_t counts[4] = {0, 0, 0, 0}; for (size_t i = 0; i < events.size(); i++) { counts[i % 4] += events[i]; ret[i] += counts[i % 4]; } #ifdef SHOWDEBUG write_fibonacci(ret); #endif return turbo_normalize(std::move(ret)); } void solve() { fenc_t a = read_fibonacci(); fenc_t b = read_fibonacci(); fenc_t ret = multiply_meh(a, b); cut_zeros(ret); write_fibonacci(ret); } int main() { int t; (void)scanf("%d", &t); while (t-- > 0) { #ifdef SHOWDEBUG printf("--- --- ---\n"); #endif solve(); } return 0; }
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 | #include <cstdint> #include <cstdio> #include <cstdlib> #include <algorithm> #include <numeric> #include <vector> using fenc_t = std::vector<int64_t>; // #define SHOWDEBUG static fenc_t read_fibonacci() { int n; (void)scanf("%d", &n); fenc_t ret; ret.reserve(n); for (int i = 0; i < n; i++) { int x; (void)scanf("%d", &x); ret.push_back(x); } return ret; } static void write_fibonacci(const fenc_t& f) { printf("%d", (int)f.size()); for (auto x : f) { printf(" %d", (int)x); } putchar('\n'); } static void cut_zeros(fenc_t& f) { while (f.size() > 0 && f.back() == 0) { f.pop_back(); } } static void ensure_minimal(fenc_t& f) { while (f.size() < 4 || f.back() != 0) { f.push_back(0); } } static void sum_normalization_remove_twos(fenc_t& f) { ensure_minimal(f); #ifdef SHOWDEBUG // write_fibonacci(f); #endif int64_t combined = 0; for (int pos = (int)f.size() - 1; pos >= 0; pos--) { auto fix = [&](int64_t a, int64_t b, int64_t c, int64_t d) { if (d != 0) { f[pos - 1] += d; } f[pos + 0] = c; f[pos + 1] = b; f[pos + 2] = a; }; combined = ((combined << 4) & 0xFFF) | f[pos]; switch (combined) { case 0x020: if (pos > 0) { fix(1, 0, 0, 1); } break; case 0x030: if (pos > 0) { fix(1, 1, 0, 1); } break; case 0x021: fix(1, 1, 0, 0); break; case 0x012: fix(1, 0, 1, 0); break; } // write_fibonacci(f); } // Final cleanup if (f[1] == 3) { f[0] = 1; f[1] = 1; f[2] = 1; } else if (f[1] == 2) { if (f[2] == 0) { f[0] += 1; f[1] = 0; f[2] = 1; } else { f[0] = 0; f[1] = 1; f[2] = 0; f[3] = 1; } } if (f[0] == 3) { f[0] = 1; f[1] = 1; } else if (f[0] == 2) { if (f[1] == 0) { f[0] = 0; f[1] = 1; } else { f[0] = 1; f[1] = 0; f[2] = 1; } } // No 2's or 3's should be left } static void sum_normalization_remove_ones(fenc_t& f) { ensure_minimal(f); int i = 0; auto op = [&] { if (f[i] == 1 && f[i + 1] == 1 && f[i + 2] == 0) { f[i] = 0; f[i + 1] = 0; f[i + 2] = 1; return 2; } else { return 1; } }; while (i + 3 <= (int)f.size()) { i += op(); } i = f.size() - 3; while (i >= 0) { i -= op(); } } // Makes a sequence resulting from piecewise addition of two fibonacci-encoded // numbers into a real fibonacci-encoded number static void normalize_after_addition(fenc_t& f) { // write_fibonacci(f); sum_normalization_remove_twos(f); // write_fibonacci(f); // write_fibonacci(f); sum_normalization_remove_ones(f); } static fenc_t add_two_fibs(fenc_t a, const fenc_t& b) { if (a.size() < b.size()) { a.resize(b.size(), 0); } for (size_t i = 0; i < b.size(); i++) { a[i] += b[i]; } // The important, non-trivial part of addition normalize_after_addition(a); return a; } // Normalizes a totally-denormalized vector of ints // O(n * log (max value in vector)) static fenc_t turbo_normalize(const fenc_t& f) { const uint64_t max_num = (uint64_t)*std::max_element(f.begin(), f.end()); uint64_t layer_bit = 0; while ((max_num & layer_bit) != max_num) { layer_bit = (layer_bit << 1) | 1; } layer_bit = (layer_bit + 1) >> 1; fenc_t ret; fenc_t slice; while (layer_bit > 0) { slice.resize(f.size(), 0); for (size_t i = 0; i < f.size(); i++) { slice[i] = (f[i] & layer_bit) ? 1 : 0; } // There may be consecutive bits in this slice, so perform second // normalization step sum_normalization_remove_ones(slice); #ifdef SHOWDEBUG printf("Adding "); write_fibonacci(slice); #endif // Add ret to itself for (auto& x : ret) { x *= 2; } normalize_after_addition(ret); #ifdef SHOWDEBUG printf("Normalized "); write_fibonacci(ret); #endif // Now add slice to ret ret = add_two_fibs(std::move(ret), slice); #ifdef SHOWDEBUG printf("Result "); write_fibonacci(ret); #endif layer_bit >>= 1; } return ret; } static fenc_t multiply_meh(const fenc_t& a, const fenc_t& b) { std::vector<int64_t> events; fenc_t ret; events.resize(a.size() + b.size() + 1 + 4, 0); ret.resize(a.size() + b.size() + 1 + 4, 0); std::vector<size_t> ones_a, ones_b; for (size_t i = 0; i < a.size(); i++) { if (a[i] == 1) { ones_a.push_back(i); } } for (size_t i = 0; i < b.size(); i++) { if (b[i] == 1) { ones_b.push_back(i); } } // Prepare events for (const auto a_it : ones_a) { for (const auto b_it : ones_b) { auto a_pos = a_it; auto b_pos = b_it; if (a_pos > b_pos) { std::swap(a_pos, b_pos); } #ifdef SHOWDEBUG printf(" (pair %d %d)\n", (int)a_pos, (int)b_pos); #endif const size_t last = a_pos + b_pos; const size_t length = 4 * (a_pos / 2); if (a_pos % 2 == 1) { if (a_pos == b_pos) { if (a_pos > 0) { ret[0] += 1; } } else { ret[last - length - 3] += 1; } } events[last - length] += 1; events[last + 4] -= 1; #ifdef SHOWDEBUG write_fibonacci(ret); write_fibonacci(events); #endif } } #ifdef SHOWDEBUG write_fibonacci(a); write_fibonacci(b); #endif // Walk through events and complete `ret` int64_t counts[4] = {0, 0, 0, 0}; for (size_t i = 0; i < events.size(); i++) { counts[i % 4] += events[i]; ret[i] += counts[i % 4]; } #ifdef SHOWDEBUG write_fibonacci(ret); #endif return turbo_normalize(std::move(ret)); } void solve() { fenc_t a = read_fibonacci(); fenc_t b = read_fibonacci(); fenc_t ret = multiply_meh(a, b); cut_zeros(ret); write_fibonacci(ret); } int main() { int t; (void)scanf("%d", &t); while (t-- > 0) { #ifdef SHOWDEBUG printf("--- --- ---\n"); #endif solve(); } return 0; } |