#include <bits/stdc++.h> #define rep(a,b,c) for(auto a = (b); a < (c); a++) #define repD(a,b,c) for(auto a = (b); a > (c); a--) #define repIn(a, b) for(auto& a : (b)) #define repIn2(a, b, c) for(auto& [a, b] : (c)) constexpr bool dbg = 0; #define DEBUG if constexpr(dbg) #define DC DEBUG std::cerr #define eol std::endl #define ll long long #define pb push_back using namespace std; #define SUM 1 #define ISC 2 #define NEG 3 constexpr int maxn = 5e4, maxm = 4e5; bitset<maxn> full, queried; vector<bitset<maxn>> a; int singleBit[maxn]; vector<pair<int, pair<int, int>>> ans; int currentlyIn; int n, s; int rightHalfInd; void makeSingles(int r, int notCutInd) { if(r == 0) return; DC << "Making singles before " << r << eol; // DC << " Number " << notCutInd << " is " << a[notCutInd] << " (should be all but (0,r)) " << eol; DEBUG { rep(i, 0, r) assert(!a[notCutInd][i]); rep(i, r, n) assert(a[notCutInd][i]); } ans.pb({NEG, {notCutInd, -1}}); DEBUG a.pb(a[notCutInd] ^ full); int cutInd = currentlyIn++; // DC << " Number " << cutInd << " is " << a[cutInd] << " (should be all in (0,r)) " << eol; DEBUG { rep(i, 0, r) assert(a[cutInd][i]); rep(i, r, n) assert(!a[cutInd][i]); } if(r == 1) { singleBit[0] = cutInd; return; } rep(i, r / 2, r) { if(queried[i]) { DEBUG a.pb(a[cutInd] & a[i]); ans.pb({ISC, {cutInd, i}}); singleBit[i] = currentlyIn++; } else singleBit[i] = i; } DEBUG a.pb(a[notCutInd] | a[singleBit[r / 2]]); ans.pb({SUM, {notCutInd, singleBit[r / 2]}}); currentlyIn++; rep(i, r / 2 + 1, r) { DEBUG a.pb(a.back() | a[singleBit[i]]); ans.pb({SUM, {currentlyIn++ - 1, singleBit[i]}}); } makeSingles(r / 2, currentlyIn - 1); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n >> s; rep(i, 0, s) { int x; cin >> x; queried[x - 1] = true; } rep(i, 0, n) full[i] = true; bool negL = 0, negR = 0; DEBUG { assert((ll)(full & (full << ((n + 1) / 2))).count() == (int)floor(n / 2.0)); assert((ll)(full & (full >> (n / 2))).count() == (int)ceil(n / 2.0)); } if((ll)(queried & (full & (full >> ((n + 1) / 2)))).count() > n / 4) { negL = true; queried ^= full & (full >> ((n + 1) / 2)); } if((ll)(queried & (full & (full << (n / 2)))).count() > n / 4) { negR = true; queried ^= full & (full << (n / 2)); } DEBUG { a.resize(n); rep(i, 0, n) for(ll j = i + 1; j < n + 1; j += i + 1) a[i][j - 1] = true; } currentlyIn = n; rep(i, n / 2, n - 1) { singleBit[i] = i; DEBUG a.pb(a.back() | a[i]); ans.pb({SUM, {currentlyIn++ - 1, i}}); } singleBit[n - 1] = n - 1; rightHalfInd = currentlyIn - 1; makeSingles(n / 2, currentlyIn - 1); DEBUG { DC << eol << "Singles: " << eol; rep(i, 0, n) { // DC << ' ' << a[singleBit[i]] << eol; assert(!queried[i] || (a[singleBit[i]].count() == 1 && a[singleBit[i]][i])); } DC << "Making them used " << currentlyIn - n << " operations " << eol << eol; } int ansInd = -1; if(queried.count() == 0) { ansInd = currentlyIn++; DEBUG a.pb(a[0] ^ full); ans.pb({NEG, {0, -1}}); } else { int theOne = 0; while(!queried[theOne]) theOne++; ansInd = singleBit[theOne]; theOne++; while(theOne < n) { if(queried[theOne]) { DEBUG a.pb(a[ansInd] | a[singleBit[theOne]]); ans.pb({SUM, {ansInd, singleBit[theOne]}}); ansInd = currentlyIn++; } theOne++; } } if(negL || negR) { int indN = -1, indL = -1, indR = -1; DEBUG a.pb(a[ansInd] ^ full); ans.pb({NEG, {ansInd, -1}}); indN = currentlyIn++; DEBUG a.pb(a[rightHalfInd] ^ full); ans.pb({NEG, {rightHalfInd, -1}}); int leftHalfInd = currentlyIn++; DC << leftHalfInd << eol; DEBUG { assert((full & (full >> ((n + 1) / 2))) == a[leftHalfInd]); assert((full & (full << (n / 2))) == a[rightHalfInd]); } if(negL) { DEBUG a.pb(a[indN] & a[leftHalfInd]); ans.pb({ISC, {indN, leftHalfInd}}); indL = currentlyIn++; queried ^= full & (full >> ((n + 1) / 2)); } else { DEBUG a.pb(a[ansInd] & a[leftHalfInd]); ans.pb({ISC, {ansInd, leftHalfInd}}); indL = currentlyIn++; } if(negR) { DEBUG a.pb(a[indN] & a[rightHalfInd]); ans.pb({ISC, {indN, rightHalfInd}}); indR = currentlyIn++; queried ^= full & (full << (n / 2)); } else { DEBUG a.pb(a[ansInd] & a[rightHalfInd]); ans.pb({ISC, {ansInd, rightHalfInd}}); indR = currentlyIn++; } DEBUG a.pb(a[indL] | a[indR]); ans.pb({SUM, {indL, indR}}); ansInd = currentlyIn++; } if(ansInd != currentlyIn - 1) { DEBUG a.pb(a[ansInd]); ans.pb({ISC, {ansInd, ansInd}}); ansInd = currentlyIn++; } // DC << "After all last one is" << eol << ' ' << a.back() << eol << "While it should be" << eol << ' ' << queried << eol; DC << "And we used " << a.size() - n << " operations" << eol << eol << eol; DEBUG assert(a.size() - n == ans.size()); DEBUG assert(a.back() == queried); DEBUG assert(ansInd == currentlyIn - 1); cout << ans.size() << '\n'; repIn2(tp, xy, ans) { auto [x, y] = xy; x++, y++; cout << tp << ' '; if(tp == NEG) cout << x << '\n'; else cout << x << ' ' << y << '\n'; } DC << "A according to me: " << eol; // repIn(i, a) DC << ' ' << i << eol; }
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 | #include <bits/stdc++.h> #define rep(a,b,c) for(auto a = (b); a < (c); a++) #define repD(a,b,c) for(auto a = (b); a > (c); a--) #define repIn(a, b) for(auto& a : (b)) #define repIn2(a, b, c) for(auto& [a, b] : (c)) constexpr bool dbg = 0; #define DEBUG if constexpr(dbg) #define DC DEBUG std::cerr #define eol std::endl #define ll long long #define pb push_back using namespace std; #define SUM 1 #define ISC 2 #define NEG 3 constexpr int maxn = 5e4, maxm = 4e5; bitset<maxn> full, queried; vector<bitset<maxn>> a; int singleBit[maxn]; vector<pair<int, pair<int, int>>> ans; int currentlyIn; int n, s; int rightHalfInd; void makeSingles(int r, int notCutInd) { if(r == 0) return; DC << "Making singles before " << r << eol; // DC << " Number " << notCutInd << " is " << a[notCutInd] << " (should be all but (0,r)) " << eol; DEBUG { rep(i, 0, r) assert(!a[notCutInd][i]); rep(i, r, n) assert(a[notCutInd][i]); } ans.pb({NEG, {notCutInd, -1}}); DEBUG a.pb(a[notCutInd] ^ full); int cutInd = currentlyIn++; // DC << " Number " << cutInd << " is " << a[cutInd] << " (should be all in (0,r)) " << eol; DEBUG { rep(i, 0, r) assert(a[cutInd][i]); rep(i, r, n) assert(!a[cutInd][i]); } if(r == 1) { singleBit[0] = cutInd; return; } rep(i, r / 2, r) { if(queried[i]) { DEBUG a.pb(a[cutInd] & a[i]); ans.pb({ISC, {cutInd, i}}); singleBit[i] = currentlyIn++; } else singleBit[i] = i; } DEBUG a.pb(a[notCutInd] | a[singleBit[r / 2]]); ans.pb({SUM, {notCutInd, singleBit[r / 2]}}); currentlyIn++; rep(i, r / 2 + 1, r) { DEBUG a.pb(a.back() | a[singleBit[i]]); ans.pb({SUM, {currentlyIn++ - 1, singleBit[i]}}); } makeSingles(r / 2, currentlyIn - 1); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n >> s; rep(i, 0, s) { int x; cin >> x; queried[x - 1] = true; } rep(i, 0, n) full[i] = true; bool negL = 0, negR = 0; DEBUG { assert((ll)(full & (full << ((n + 1) / 2))).count() == (int)floor(n / 2.0)); assert((ll)(full & (full >> (n / 2))).count() == (int)ceil(n / 2.0)); } if((ll)(queried & (full & (full >> ((n + 1) / 2)))).count() > n / 4) { negL = true; queried ^= full & (full >> ((n + 1) / 2)); } if((ll)(queried & (full & (full << (n / 2)))).count() > n / 4) { negR = true; queried ^= full & (full << (n / 2)); } DEBUG { a.resize(n); rep(i, 0, n) for(ll j = i + 1; j < n + 1; j += i + 1) a[i][j - 1] = true; } currentlyIn = n; rep(i, n / 2, n - 1) { singleBit[i] = i; DEBUG a.pb(a.back() | a[i]); ans.pb({SUM, {currentlyIn++ - 1, i}}); } singleBit[n - 1] = n - 1; rightHalfInd = currentlyIn - 1; makeSingles(n / 2, currentlyIn - 1); DEBUG { DC << eol << "Singles: " << eol; rep(i, 0, n) { // DC << ' ' << a[singleBit[i]] << eol; assert(!queried[i] || (a[singleBit[i]].count() == 1 && a[singleBit[i]][i])); } DC << "Making them used " << currentlyIn - n << " operations " << eol << eol; } int ansInd = -1; if(queried.count() == 0) { ansInd = currentlyIn++; DEBUG a.pb(a[0] ^ full); ans.pb({NEG, {0, -1}}); } else { int theOne = 0; while(!queried[theOne]) theOne++; ansInd = singleBit[theOne]; theOne++; while(theOne < n) { if(queried[theOne]) { DEBUG a.pb(a[ansInd] | a[singleBit[theOne]]); ans.pb({SUM, {ansInd, singleBit[theOne]}}); ansInd = currentlyIn++; } theOne++; } } if(negL || negR) { int indN = -1, indL = -1, indR = -1; DEBUG a.pb(a[ansInd] ^ full); ans.pb({NEG, {ansInd, -1}}); indN = currentlyIn++; DEBUG a.pb(a[rightHalfInd] ^ full); ans.pb({NEG, {rightHalfInd, -1}}); int leftHalfInd = currentlyIn++; DC << leftHalfInd << eol; DEBUG { assert((full & (full >> ((n + 1) / 2))) == a[leftHalfInd]); assert((full & (full << (n / 2))) == a[rightHalfInd]); } if(negL) { DEBUG a.pb(a[indN] & a[leftHalfInd]); ans.pb({ISC, {indN, leftHalfInd}}); indL = currentlyIn++; queried ^= full & (full >> ((n + 1) / 2)); } else { DEBUG a.pb(a[ansInd] & a[leftHalfInd]); ans.pb({ISC, {ansInd, leftHalfInd}}); indL = currentlyIn++; } if(negR) { DEBUG a.pb(a[indN] & a[rightHalfInd]); ans.pb({ISC, {indN, rightHalfInd}}); indR = currentlyIn++; queried ^= full & (full << (n / 2)); } else { DEBUG a.pb(a[ansInd] & a[rightHalfInd]); ans.pb({ISC, {ansInd, rightHalfInd}}); indR = currentlyIn++; } DEBUG a.pb(a[indL] | a[indR]); ans.pb({SUM, {indL, indR}}); ansInd = currentlyIn++; } if(ansInd != currentlyIn - 1) { DEBUG a.pb(a[ansInd]); ans.pb({ISC, {ansInd, ansInd}}); ansInd = currentlyIn++; } // DC << "After all last one is" << eol << ' ' << a.back() << eol << "While it should be" << eol << ' ' << queried << eol; DC << "And we used " << a.size() - n << " operations" << eol << eol << eol; DEBUG assert(a.size() - n == ans.size()); DEBUG assert(a.back() == queried); DEBUG assert(ansInd == currentlyIn - 1); cout << ans.size() << '\n'; repIn2(tp, xy, ans) { auto [x, y] = xy; x++, y++; cout << tp << ' '; if(tp == NEG) cout << x << '\n'; else cout << x << ' ' << y << '\n'; } DC << "A according to me: " << eol; // repIn(i, a) DC << ' ' << i << eol; } |