#include <bits/stdc++.h> #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define VAR(v, i) __typeof(i) v=(i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define all(v) (v).begin(),(v).end() #define PII pair<int,int> #define st first #define nd second #define pb push_back #define lint long long int #define VI vector<int> #define debug(x) {cout <<#x <<" = " <<x <<endl; } #define debug2(x,y) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y <<endl; } #define debug3(x,y,z) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y << ", " << #z << " = " << z <<endl; } #define debugv(x) {{cout <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }} #define debugt(t,n) {{cerr <<#t <<" = "; FOR(it,0,(n)) cerr <<t[it] <<", "; cerr <<endl; }} #define make( x) int (x); scanf("%d",&(x)); #define make2( x, y) int (x), (y); scanf("%d%d",&(x),&(y)); #define make3(x, y, z) int (x), (y), (z); scanf("%d%d%d",&(x),&(y),&(z)); #define make4(x, y, z, t) int (x), (y), (z), (t); scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define makev(v,n) VI (v); FOR(i,0,(n)) { make(a); (v).pb(a);} #define IOS ios_base::sync_with_stdio(0) #define HEAP priority_queue #define read( x) scanf("%d",&(x)); #define read2( x, y) scanf("%d%d",&(x),&(y)); #define read3(x, y, z) scanf("%d%d%d",&(x),&(y),&(z)); #define read4(x, y, z, t) scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define readv(v,n) FOR(i,0,(n)) { make(a); (v).pb(a);} #define jeb() fflush(stdout); using namespace std; void normalizuj(vector < int >&tab, int n) { int ile_jedynek = 0; FOR(i, 0, n + 5) { if (tab[i] == 0) { if (ile_jedynek == 0) { ile_jedynek = 0; continue; } // 0 -> 0 if (ile_jedynek == 1) { ile_jedynek = 0; continue; } // 010 -> 010 if (ile_jedynek % 2 == 0) { // 01^(2k) -> 00 (01)^k FOR(j, 1, ile_jedynek + 1) { tab[i - j] = 0; if (j % 2 == 0) tab[i - j] = 1; } tab[i - ile_jedynek] = 0; tab[i] = 1; ile_jedynek = 1; continue; } // 01^(2k+1) -> 010(01)^k FOR(j, 1, ile_jedynek+1) { tab[i - j] = 0; if (j % 2 == 0) tab[i - j] = 1; } tab[i] = 1; tab[i - ile_jedynek] = 1; tab[i - ile_jedynek + 1] = 0; ile_jedynek = 1; continue; } if (tab[i] == 1) { ile_jedynek++; } if (tab[i] == 2) { // nastepna zawsze jest == 0 if (ile_jedynek == 0) { continue; } if (ile_jedynek == 1) { tab[i - 1] = 0; tab[i] = 1; tab[i + 1] = 1; ile_jedynek = 1; // 0120 -> 0011 } if (ile_jedynek == 2) { tab[i - 1] = 0; tab[i] = 1; tab[i + 1] = 1; ile_jedynek = 1; // 01120 -> 01011 } // innych mozliwosci nie MA !!! } } return; } void eliminate_102_002(vector < int >&tab, int n) { // chcemy wyeliminowac sekwencje 102 i 002 // ciag na wejsciu jest znormalizowany. // i kazda 2 jest "otoczona" zerami FORD(i, n + 5, 2) { if (tab[i] == 2) { if ((tab[i - 1] == 0) && (tab[i - 2] == 1)) { // 102 -> 211 tab[i - 2] = 2; tab[i - 1] = 1; tab[i] = 1; continue; } if ((tab[i - 1] == 0) && (tab[i - 2] == 0)) { // 002 -> 111 tab[i - 2] = 1; tab[i - 1] = 1; tab[i] = 1; } } } if ((tab[1] == 2) && (tab[0] == 0)) { tab[1] = 1; tab[0] = 2; } // teraz po kazdej dwojce jest pewna (niezerowa) ilosc jedynek return; } // usuwa 2 z poczatku void eliminate_front(vector < int >&tab, int n){ if ((tab[0] == 2) && (tab[1] == 0)) { tab[0] = 0; tab[1] = 1; } int i = 0; if (tab[i] == 2) { int ile_jedynek = 0; while (tab[i + ile_jedynek + 1] == 1) ile_jedynek++; if ((ile_jedynek != 0) && (ile_jedynek % 2 == 0)) { // 21^(2k)0 -> (01)^(k+1) FOR(j,0, ile_jedynek + 2) { if (j % 2 == 0) tab[i + j] = 0; if (j % 2 == 1) tab[i + j] = 1; } i += ile_jedynek + 1; } if (ile_jedynek % 2 == 1) { // 21^(2k+1)0 -> 1(01)^(k+1) FOR(j,0, ile_jedynek + 2) { if (j % 2 == 0) tab[i + j] = 1; if (j % 2 == 1) tab[i + j] = 0; } i += ile_jedynek + 1; } } // poniewaz w danych nie bylo 002 to w wyniku nie bedzie 102 } void eliminate_last(vector < int >&tab, int n) { // ostatnia eliminacja FOR(i, 2, n + 6) { if (tab[i] == 2) { // 1102 -> 0012 if ((tab[i - 1] == 0) && (tab[i - 2] == 1) && (tab[i - 3] == 1)) { tab[i - 3] = 0; tab[i - 2] = 0; tab[i - 1] = 1; tab[i] = 2; continue; } } if (tab[i - 1] == 2) { if (tab[i] == 0) { // 120 -> 011 tab[i - 2] = 0; tab[i - 1] = 1; tab[i] = 1; continue; } if (tab[i] == 1) { // 121 --> 012 tab[i - 2] = 0; tab[i - 1] = 1; tab[i] = 2; continue; } } } } void dodaj(VI& v, VI& w) { int n = max(v.size(), w.size()); while (v.size() < n + 10) v.push_back(0); FOR(i,0,w.size()) v[i] += w[i]; normalizuj(v, n); eliminate_102_002(v, n); eliminate_front(v, n); eliminate_last(v, n); normalizuj(v, n); } void rob_wektor(int a, int b, VI& res) { if (a == 1) { res[b-1] = 1; return; } if (a == 2) { if (b == 2) { res[0] = 1; res[2] = 1; return; } res[b] = 1; res[b-3] = 1; return; } rob_wektor(a-2, b-2, res); res[a+b-2] = 1; } void solve() { make(k); makev(v, k); make(l); makev(w, l); vector<VI> skladniki; FOR(i,0,k) FOR(j,0,l) { if (v[i] == 1 && w[j] == 1) { int a = min(i, j) + 1; int b = max(i, j) + 1; VI kij(a+b+5); rob_wektor(a, b, kij); while (kij.back() == 0) kij.pop_back(); skladniki.pb(kij); } } VI ans = skladniki[0]; FOR(i,1,skladniki.size()) { dodaj(ans, skladniki[i]); } while (ans.back() == 0) ans.pop_back(); printf("%d ", (int)ans.size()); FOR(i,0,ans.size()) { printf("%d ", ans[i]); } printf("\n"); } int main() { make(z); while (z--) { solve(); } }
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 | #include <bits/stdc++.h> #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define VAR(v, i) __typeof(i) v=(i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define all(v) (v).begin(),(v).end() #define PII pair<int,int> #define st first #define nd second #define pb push_back #define lint long long int #define VI vector<int> #define debug(x) {cout <<#x <<" = " <<x <<endl; } #define debug2(x,y) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y <<endl; } #define debug3(x,y,z) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y << ", " << #z << " = " << z <<endl; } #define debugv(x) {{cout <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }} #define debugt(t,n) {{cerr <<#t <<" = "; FOR(it,0,(n)) cerr <<t[it] <<", "; cerr <<endl; }} #define make( x) int (x); scanf("%d",&(x)); #define make2( x, y) int (x), (y); scanf("%d%d",&(x),&(y)); #define make3(x, y, z) int (x), (y), (z); scanf("%d%d%d",&(x),&(y),&(z)); #define make4(x, y, z, t) int (x), (y), (z), (t); scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define makev(v,n) VI (v); FOR(i,0,(n)) { make(a); (v).pb(a);} #define IOS ios_base::sync_with_stdio(0) #define HEAP priority_queue #define read( x) scanf("%d",&(x)); #define read2( x, y) scanf("%d%d",&(x),&(y)); #define read3(x, y, z) scanf("%d%d%d",&(x),&(y),&(z)); #define read4(x, y, z, t) scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define readv(v,n) FOR(i,0,(n)) { make(a); (v).pb(a);} #define jeb() fflush(stdout); using namespace std; void normalizuj(vector < int >&tab, int n) { int ile_jedynek = 0; FOR(i, 0, n + 5) { if (tab[i] == 0) { if (ile_jedynek == 0) { ile_jedynek = 0; continue; } // 0 -> 0 if (ile_jedynek == 1) { ile_jedynek = 0; continue; } // 010 -> 010 if (ile_jedynek % 2 == 0) { // 01^(2k) -> 00 (01)^k FOR(j, 1, ile_jedynek + 1) { tab[i - j] = 0; if (j % 2 == 0) tab[i - j] = 1; } tab[i - ile_jedynek] = 0; tab[i] = 1; ile_jedynek = 1; continue; } // 01^(2k+1) -> 010(01)^k FOR(j, 1, ile_jedynek+1) { tab[i - j] = 0; if (j % 2 == 0) tab[i - j] = 1; } tab[i] = 1; tab[i - ile_jedynek] = 1; tab[i - ile_jedynek + 1] = 0; ile_jedynek = 1; continue; } if (tab[i] == 1) { ile_jedynek++; } if (tab[i] == 2) { // nastepna zawsze jest == 0 if (ile_jedynek == 0) { continue; } if (ile_jedynek == 1) { tab[i - 1] = 0; tab[i] = 1; tab[i + 1] = 1; ile_jedynek = 1; // 0120 -> 0011 } if (ile_jedynek == 2) { tab[i - 1] = 0; tab[i] = 1; tab[i + 1] = 1; ile_jedynek = 1; // 01120 -> 01011 } // innych mozliwosci nie MA !!! } } return; } void eliminate_102_002(vector < int >&tab, int n) { // chcemy wyeliminowac sekwencje 102 i 002 // ciag na wejsciu jest znormalizowany. // i kazda 2 jest "otoczona" zerami FORD(i, n + 5, 2) { if (tab[i] == 2) { if ((tab[i - 1] == 0) && (tab[i - 2] == 1)) { // 102 -> 211 tab[i - 2] = 2; tab[i - 1] = 1; tab[i] = 1; continue; } if ((tab[i - 1] == 0) && (tab[i - 2] == 0)) { // 002 -> 111 tab[i - 2] = 1; tab[i - 1] = 1; tab[i] = 1; } } } if ((tab[1] == 2) && (tab[0] == 0)) { tab[1] = 1; tab[0] = 2; } // teraz po kazdej dwojce jest pewna (niezerowa) ilosc jedynek return; } // usuwa 2 z poczatku void eliminate_front(vector < int >&tab, int n){ if ((tab[0] == 2) && (tab[1] == 0)) { tab[0] = 0; tab[1] = 1; } int i = 0; if (tab[i] == 2) { int ile_jedynek = 0; while (tab[i + ile_jedynek + 1] == 1) ile_jedynek++; if ((ile_jedynek != 0) && (ile_jedynek % 2 == 0)) { // 21^(2k)0 -> (01)^(k+1) FOR(j,0, ile_jedynek + 2) { if (j % 2 == 0) tab[i + j] = 0; if (j % 2 == 1) tab[i + j] = 1; } i += ile_jedynek + 1; } if (ile_jedynek % 2 == 1) { // 21^(2k+1)0 -> 1(01)^(k+1) FOR(j,0, ile_jedynek + 2) { if (j % 2 == 0) tab[i + j] = 1; if (j % 2 == 1) tab[i + j] = 0; } i += ile_jedynek + 1; } } // poniewaz w danych nie bylo 002 to w wyniku nie bedzie 102 } void eliminate_last(vector < int >&tab, int n) { // ostatnia eliminacja FOR(i, 2, n + 6) { if (tab[i] == 2) { // 1102 -> 0012 if ((tab[i - 1] == 0) && (tab[i - 2] == 1) && (tab[i - 3] == 1)) { tab[i - 3] = 0; tab[i - 2] = 0; tab[i - 1] = 1; tab[i] = 2; continue; } } if (tab[i - 1] == 2) { if (tab[i] == 0) { // 120 -> 011 tab[i - 2] = 0; tab[i - 1] = 1; tab[i] = 1; continue; } if (tab[i] == 1) { // 121 --> 012 tab[i - 2] = 0; tab[i - 1] = 1; tab[i] = 2; continue; } } } } void dodaj(VI& v, VI& w) { int n = max(v.size(), w.size()); while (v.size() < n + 10) v.push_back(0); FOR(i,0,w.size()) v[i] += w[i]; normalizuj(v, n); eliminate_102_002(v, n); eliminate_front(v, n); eliminate_last(v, n); normalizuj(v, n); } void rob_wektor(int a, int b, VI& res) { if (a == 1) { res[b-1] = 1; return; } if (a == 2) { if (b == 2) { res[0] = 1; res[2] = 1; return; } res[b] = 1; res[b-3] = 1; return; } rob_wektor(a-2, b-2, res); res[a+b-2] = 1; } void solve() { make(k); makev(v, k); make(l); makev(w, l); vector<VI> skladniki; FOR(i,0,k) FOR(j,0,l) { if (v[i] == 1 && w[j] == 1) { int a = min(i, j) + 1; int b = max(i, j) + 1; VI kij(a+b+5); rob_wektor(a, b, kij); while (kij.back() == 0) kij.pop_back(); skladniki.pb(kij); } } VI ans = skladniki[0]; FOR(i,1,skladniki.size()) { dodaj(ans, skladniki[i]); } while (ans.back() == 0) ans.pop_back(); printf("%d ", (int)ans.size()); FOR(i,0,ans.size()) { printf("%d ", ans[i]); } printf("\n"); } int main() { make(z); while (z--) { solve(); } } |