#include <bits/stdc++.h> // Tomasz Nowak using namespace std; // XIII LO Szczecin using LL = long long; // Poland #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) #define FORD(i, l, r) for(int i = (l); i >= (r); --i) template<class T> int size(T &&x) { return int(x.size()); } vector<int> add(vector<int> &a, vector<int> &b) { int n = size(a), m = size(b); vector<int> answer(max(n, m) + 10); REP(i, n) answer[i + 1] += a[i]; REP(i, m) answer[i + 1] += b[i]; auto rm_11 = [&] { FOR(i, 1, size(answer) - 4) { if(answer[i] == 1 && answer[i + 1] == 1 && answer[i + 2] == 2) { answer[i + 3] = 1; answer[i + 2] = 1; answer[i + 1] = 0; answer[i] = 1; } else { int d = min(answer[i], answer[i + 1]); answer[i] -= d; answer[i + 1] -= d; answer[i + 2] += d; } } }; auto rm_2 = [&] { FORD(i, size(answer) - 2, 2) if(answer[i] == 2 && answer[i - 1] == 0 && answer[i - 2] != 2) { bool last_2 = answer[i - 2] == 1; answer[i] = 1; answer[i - 1] = 1; answer[i - 2] = last_2 ? 2 : 1; } if(answer[1] == 0 && answer[2] == 2) { answer[1] = 2; answer[2] = 1; } else if(answer[1] == 2 && answer[2] == 0) { answer[1] = 0; answer[2] = 1; } if(answer[1] == 2 && answer[2] == 1) { int i = 2; int j = i; while(answer[j] == 1) ++j; int l = j - i; int start; if(l & 1) { start = i; answer[i - 1] = 1; } else start = i - 1; FOR(k, start, j) answer[k] = (k & 1) == (j & 1); } REP(i, size(answer) - 4) if(answer[i] == 1) { if(answer[i + 1] == 1 && answer[i + 2] == 0 && answer[i + 3] == 2) { answer[i + 2] = 1; answer[i] = answer[i + 1] = 0; } else if (answer[i + 1] == 2) { answer[i + 2] += 1; answer[i + 1] = 1; answer[i] = 0; } } }; auto rm_11_2 = [&] { FOR(i, 0, size(answer) - 2) if(answer[i] == 1 && answer[i + 1] == 1) { int j = i; while(answer[j] == 1) ++j; int l = j - i; int start; if(l & 1) { start = i + 2; answer[i + 1] = 0; answer[i] = 1; } else { start = i + 1; answer[i] = 0; } FOR(k, start, j) answer[k] = (k & 1) == (j & 1); } }; rm_11(); rm_2(); rm_11_2(); while(size(answer) && answer.back() == 0) answer.pop_back(); answer[1] += answer[0]; answer.erase(answer.begin()); return answer; } vector<int> mnoz(vector<int> a, vector<int> b) { vector<int> answer = {0}, prev1 = a, prev2 = a; for(int i = 0; i < size(b); ++i) { if(b[i]) answer = add(answer, prev1); if(i != size(b)) { prev2 = add(prev2, prev1); swap(prev2, prev1); } } return answer; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while(t --> 0) { int n; cin >> n; vector<int> a(n); for(int &i : a) cin >> i; int m; cin >> m; vector<int> b(m); for(int &i : b) cin >> i; vector<int> answer = mnoz(a, b); cout << size(answer) << ' '; for(int i : answer) cout << i << ' '; cout << '\n'; } }
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 | #include <bits/stdc++.h> // Tomasz Nowak using namespace std; // XIII LO Szczecin using LL = long long; // Poland #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) #define FORD(i, l, r) for(int i = (l); i >= (r); --i) template<class T> int size(T &&x) { return int(x.size()); } vector<int> add(vector<int> &a, vector<int> &b) { int n = size(a), m = size(b); vector<int> answer(max(n, m) + 10); REP(i, n) answer[i + 1] += a[i]; REP(i, m) answer[i + 1] += b[i]; auto rm_11 = [&] { FOR(i, 1, size(answer) - 4) { if(answer[i] == 1 && answer[i + 1] == 1 && answer[i + 2] == 2) { answer[i + 3] = 1; answer[i + 2] = 1; answer[i + 1] = 0; answer[i] = 1; } else { int d = min(answer[i], answer[i + 1]); answer[i] -= d; answer[i + 1] -= d; answer[i + 2] += d; } } }; auto rm_2 = [&] { FORD(i, size(answer) - 2, 2) if(answer[i] == 2 && answer[i - 1] == 0 && answer[i - 2] != 2) { bool last_2 = answer[i - 2] == 1; answer[i] = 1; answer[i - 1] = 1; answer[i - 2] = last_2 ? 2 : 1; } if(answer[1] == 0 && answer[2] == 2) { answer[1] = 2; answer[2] = 1; } else if(answer[1] == 2 && answer[2] == 0) { answer[1] = 0; answer[2] = 1; } if(answer[1] == 2 && answer[2] == 1) { int i = 2; int j = i; while(answer[j] == 1) ++j; int l = j - i; int start; if(l & 1) { start = i; answer[i - 1] = 1; } else start = i - 1; FOR(k, start, j) answer[k] = (k & 1) == (j & 1); } REP(i, size(answer) - 4) if(answer[i] == 1) { if(answer[i + 1] == 1 && answer[i + 2] == 0 && answer[i + 3] == 2) { answer[i + 2] = 1; answer[i] = answer[i + 1] = 0; } else if (answer[i + 1] == 2) { answer[i + 2] += 1; answer[i + 1] = 1; answer[i] = 0; } } }; auto rm_11_2 = [&] { FOR(i, 0, size(answer) - 2) if(answer[i] == 1 && answer[i + 1] == 1) { int j = i; while(answer[j] == 1) ++j; int l = j - i; int start; if(l & 1) { start = i + 2; answer[i + 1] = 0; answer[i] = 1; } else { start = i + 1; answer[i] = 0; } FOR(k, start, j) answer[k] = (k & 1) == (j & 1); } }; rm_11(); rm_2(); rm_11_2(); while(size(answer) && answer.back() == 0) answer.pop_back(); answer[1] += answer[0]; answer.erase(answer.begin()); return answer; } vector<int> mnoz(vector<int> a, vector<int> b) { vector<int> answer = {0}, prev1 = a, prev2 = a; for(int i = 0; i < size(b); ++i) { if(b[i]) answer = add(answer, prev1); if(i != size(b)) { prev2 = add(prev2, prev1); swap(prev2, prev1); } } return answer; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while(t --> 0) { int n; cin >> n; vector<int> a(n); for(int &i : a) cin >> i; int m; cin >> m; vector<int> b(m); for(int &i : b) cin >> i; vector<int> answer = mnoz(a, b); cout << size(answer) << ' '; for(int i : answer) cout << i << ' '; cout << '\n'; } } |