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 #include #define FOR(i, n) for(int i = 0; i < (n); ++i) #define REP(i, a, b) for(int i = (a); i < (b); ++i) #define TRAV(i, n) for(auto &i : n) #define SZ(x) (int)(x).size() #define PR std::pair #define MP std::make_pair #define X first #define Y second typedef long long ll; typedef std::pair PII; typedef std::vector VI; void add(VI &a, VI &b){ int carry = 0; if(SZ(a) < SZ(b)) a.resize(SZ(b)); a.push_back(0); FOR(i, SZ(a)){ int now = carry+a[i]+(i < SZ(b) ? b[i] : 0); if(now&1) a[i] = 1; else a[i] = 0; if(now&2) carry = 1; else carry = 0; } while(SZ(a) && a.back() == 0) a.pop_back(); } std::vector > Q; void solve_jedynki(){ FOR(q, SZ(Q)){ int n = SZ(Q[q].X); int m = SZ(Q[q].Y); VI ret(n+m+1); int pos = n+m; int xd = 0; int ile = std::min(n, m)/2+1; int last = -1; while(pos >= 0){ if(xd == 0 && ile) ret[pos] = 1, ile--, last = pos; xd = (xd+1)%4; pos--; } if(std::min(n, m)%2 == 0 && last != n+m) std::swap(ret[last], ret[last+1]); if(ret[1] == 1) ret[2] = 1; std::printf("%d ", SZ(ret)-2); REP(i, 2, SZ(ret)) std::printf("%d ", ret[i]); std::printf("\n"); } } void sub(VI &a, VI &b){ int need = 0; FOR(i, SZ(a)){ a[i] -= (i < SZ(b) ? b[i] : 0) + need; if(a[i] < 0) a[i] += 2, need = 1; else need = 0; } while(SZ(a) && a.back() == 0) a.pop_back(); } bool wiekszy(VI &a, VI &b){ if(SZ(a) > SZ(b)) return true; if(SZ(a) < SZ(b)) return false; for(int i = SZ(a)-1; i >= 0; --i){ if(a[i] > b[i]) return true; else if(a[i] < b[i]) return false; } return true; } void solve_kwadrat(int MX){ MX = 3*MX+12; FOR(q, SZ(Q)){ int n = SZ(Q[q].X); int m = SZ(Q[q].Y); VI b1, b2; VI fa = VI{1}; VI fb = VI{1}; FOR(i, n){ if(Q[q].X[i]) add(b1, fb); add(fa, fb); std::swap(fb, fa); } fa = VI{1}; fb = VI{1}; FOR(i, m){ if(Q[q].Y[i]) add(b2, fb); add(fa, fb); std::swap(fb, fa); } VI ret; FOR(i, SZ(b1)){ if(b1[i]) add(ret, b2); add(b2, b2); } VI ans; fa = VI{1}; fb = VI{1}; int ind = 1; while(wiekszy(ret, fb)){ add(fa, fb); std::swap(fb, fa); ind++; } while(SZ(ret)){ if(wiekszy(ret, fb)){ sub(ret, fb); if(SZ(ans) < ind) ans.resize(ind); ans[ind-1] = 1; } sub(fb, fa); std::swap(fa, fb); ind--; } std::printf("%d ", SZ(ans)); TRAV(i, ans) std::printf("%d ", i); std::printf("\n"); } } int main(){ int t; std::scanf("%d", &t); bool cs = true; int MAX = 0; while(t--){ Q.emplace_back(); int n, m; std::scanf("%d", &n); Q.back().X.resize(n); int cnt = 0; FOR(i, n) { std::scanf("%d", &Q.back().X[i]); cnt += Q.back().X[i]; } std::scanf("%d", &m); Q.back().Y.resize(m); FOR(i, m){ std::scanf("%d", &Q.back().Y[i]); cnt += Q.back().Y[i]; } if(cnt > 2) cs = false; MAX = std::max(MAX, n+m+3); } if(cs){ solve_jedynki(); }else{ solve_kwadrat(MAX); } return 0; }