#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> // #ifdef LOCAL // #include "../debug/debug.h" // #else // #define debug(...) // #define debugArr(...) // #endif using namespace std; using namespace __gnu_pbds; using ll = long long; using db = long double; using pi = pair<int,int>; using pl = pair<ll,ll>; using vi = vector<int>; using vl = vector<ll>; using vb = vector<bool>; using vpi = vector<pi>; using vpl = vector<pl>; using vvi = vector<vi>; #define mp make_pair #define eb emplace_back #define pb push_back #define x first #define y second #define sz(x) int((x).size()) #define bg(x) begin(x) #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(), (x).rend() #define rep(i,a,b) for(int i=(a);i<(b);i++) #define per(i,a,b) for(int i=(b)-1;i>=(a);i--) #define ft front() #define bk back() #define rsz resize #define ins insert #define each(a,x) for(auto&a:x) bool ckmin(auto&a,auto b){return b<a?a=b,1:0;} bool ckmax(auto&a,auto b){return b>a?a=b,1:0;} #ifdef LOCAL auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.x<<", "<<p.y<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto&e:x)o<<","+!i++<<e;return o<<"}";} #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X); #else #define debug(...){} #endif template<int MOD, int RT> struct mint { static const int mod = MOD; static constexpr mint rt() { return RT; } int v; explicit operator int() const { return v; } mint() : v(0) {} mint(ll _v) { v = int((-MOD < _v && _v < MOD) ? _v : _v % MOD); if (v < 0) v += MOD; } bool operator==(const mint& o) const { return v == o.v; } friend bool operator!=(const mint& a, const mint& b) { return !(a == b); } friend bool operator<(const mint& a, const mint& b) { return a.v < b.v; } mint& operator+=(const mint& o) { if ((v += o.v) >= MOD) v -= MOD; return *this; } mint& operator-=(const mint& o) { if ((v -= o.v) < 0) v += MOD; return *this; } mint& operator*=(const mint& o) { v = int((ll)v*o.v%MOD); return *this; } mint& operator/=(const mint& o) { return (*this) *= inv(o); } friend mint pow(mint a, ll p) { mint ans = 1; assert(p >= 0); for (; p; p /= 2, a *= a) if (p&1) ans *= a; return ans; } friend mint inv(const mint& a) { assert(a.v != 0); return pow(a,MOD-2); } mint operator-() const { return mint(-v); } mint& operator++() { return *this += 1; } mint& operator--() { return *this -= 1; } friend mint operator+(mint a, const mint& b) { return a += b; } friend mint operator-(mint a, const mint& b) { return a -= b; } friend mint operator*(mint a, const mint& b) { return a *= b; } friend mint operator/(mint a, const mint& b) { return a /= b; } friend ostream& operator<<(ostream& o, const mint& a) { return o << a.v; } friend istream& operator>>(istream& i, mint& a) { ll x; i >> x; a = x; return i; } }; const int MOD = 998244353; using mi = mint<MOD,5>; using vmi = vector<mi>; using pmi = pair<mi,mi>; using vpmi = vector<pmi>; const pi MOVES[] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}}; #ifdef LOCAL mt19937_64 rng(0xabadbeef); #else mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); #endif ll randr(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rng); } const int N = 5e4+64; using B = bitset<N>; vector<B> b; vector<tuple<int,int,int>> ops; void do_or(int x, int y) { ops.eb(1, x, y); b.eb(b[x] | b[y]); } void do_and(int x, int y) { ops.eb(2, x, y); b.eb(b[x] & b[y]); } void do_neg(int x) { ops.eb(3, x, -1); b.eb(~b[x]); } int n; int process(const B& target) { int x = target._Find_first(); // assert (x <= sz(target)); B to_delete = 0; B next_target = target; for (int y = x; y <= n; y += x) { if (!target[y]) { to_delete[y] = 1; } else { next_target[y] = 0; } } int idx = x; if (!to_delete.none()) { int to_delete_idx = process(to_delete); // assert(b[to_delete_idx] == to_delete); do_neg(to_delete_idx); do_and(x, sz(b) - 1); idx = sz(b) - 1; } // assert((b[idx] | next_target) == target); if (next_target.none()) { // assert(b[idx] == target); return idx; } int next_target_idx = process(next_target); // assert(b[next_target_idx] == next_target); do_or(idx, next_target_idx); return sz(b) - 1; } signed main() { cin.tie(0)->sync_with_stdio(0); #ifdef LOCAL int t = 1e9; #else int t = 1; #endif rep(tc,0,t) { #ifdef LOCAL n = 50'000; int s = randr(1, n); #else int s; cin >> n >> s; #endif b.clear(); ops.clear(); b.resize(n + 1); rep(i,1,n+1) { for (int j = i; j <= n; j += i) { b[i][j] = 1; } } B goal = 0; #ifdef LOCAL int cnt = 0; while (cnt < s) { int x = randr(1, n); cnt += (goal[x] == 0); goal[x] = 1; } #else rep(i,0,s) { int x; cin >> x; goal[x] = 1; } #endif int m = process(goal); if (m <= n) { do_or(m, m); m = sz(b) - 1; } // assert(b.back() == goal); #ifdef LOCAL assert(sz(ops) <= 100'000); // for (int i = n + 1; i <= m; i++) { // auto [type, x, y] = ops[i - n - 1]; // if (type == 3) { // b[i] = ~b[x]; // continue; // } // if (type == 1) { // b[i] = b[x] | b[y]; // } // else { // b[i] = b[x] & b[y]; // } // } // assert(b.back() == goal); cout << "TEST " + to_string(tc) + ": OK, ops: " + to_string(sz(ops)) << endl; #else cout << sz(ops) << '\n'; for (auto [type, x, y] : ops) { cout << type << ' ' << x; if (type != 3) cout << ' ' << y; cout << '\n'; } #endif } 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 | #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> // #ifdef LOCAL // #include "../debug/debug.h" // #else // #define debug(...) // #define debugArr(...) // #endif using namespace std; using namespace __gnu_pbds; using ll = long long; using db = long double; using pi = pair<int,int>; using pl = pair<ll,ll>; using vi = vector<int>; using vl = vector<ll>; using vb = vector<bool>; using vpi = vector<pi>; using vpl = vector<pl>; using vvi = vector<vi>; #define mp make_pair #define eb emplace_back #define pb push_back #define x first #define y second #define sz(x) int((x).size()) #define bg(x) begin(x) #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(), (x).rend() #define rep(i,a,b) for(int i=(a);i<(b);i++) #define per(i,a,b) for(int i=(b)-1;i>=(a);i--) #define ft front() #define bk back() #define rsz resize #define ins insert #define each(a,x) for(auto&a:x) bool ckmin(auto&a,auto b){return b<a?a=b,1:0;} bool ckmax(auto&a,auto b){return b>a?a=b,1:0;} #ifdef LOCAL auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.x<<", "<<p.y<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto&e:x)o<<","+!i++<<e;return o<<"}";} #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X); #else #define debug(...){} #endif template<int MOD, int RT> struct mint { static const int mod = MOD; static constexpr mint rt() { return RT; } int v; explicit operator int() const { return v; } mint() : v(0) {} mint(ll _v) { v = int((-MOD < _v && _v < MOD) ? _v : _v % MOD); if (v < 0) v += MOD; } bool operator==(const mint& o) const { return v == o.v; } friend bool operator!=(const mint& a, const mint& b) { return !(a == b); } friend bool operator<(const mint& a, const mint& b) { return a.v < b.v; } mint& operator+=(const mint& o) { if ((v += o.v) >= MOD) v -= MOD; return *this; } mint& operator-=(const mint& o) { if ((v -= o.v) < 0) v += MOD; return *this; } mint& operator*=(const mint& o) { v = int((ll)v*o.v%MOD); return *this; } mint& operator/=(const mint& o) { return (*this) *= inv(o); } friend mint pow(mint a, ll p) { mint ans = 1; assert(p >= 0); for (; p; p /= 2, a *= a) if (p&1) ans *= a; return ans; } friend mint inv(const mint& a) { assert(a.v != 0); return pow(a,MOD-2); } mint operator-() const { return mint(-v); } mint& operator++() { return *this += 1; } mint& operator--() { return *this -= 1; } friend mint operator+(mint a, const mint& b) { return a += b; } friend mint operator-(mint a, const mint& b) { return a -= b; } friend mint operator*(mint a, const mint& b) { return a *= b; } friend mint operator/(mint a, const mint& b) { return a /= b; } friend ostream& operator<<(ostream& o, const mint& a) { return o << a.v; } friend istream& operator>>(istream& i, mint& a) { ll x; i >> x; a = x; return i; } }; const int MOD = 998244353; using mi = mint<MOD,5>; using vmi = vector<mi>; using pmi = pair<mi,mi>; using vpmi = vector<pmi>; const pi MOVES[] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}}; #ifdef LOCAL mt19937_64 rng(0xabadbeef); #else mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); #endif ll randr(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rng); } const int N = 5e4+64; using B = bitset<N>; vector<B> b; vector<tuple<int,int,int>> ops; void do_or(int x, int y) { ops.eb(1, x, y); b.eb(b[x] | b[y]); } void do_and(int x, int y) { ops.eb(2, x, y); b.eb(b[x] & b[y]); } void do_neg(int x) { ops.eb(3, x, -1); b.eb(~b[x]); } int n; int process(const B& target) { int x = target._Find_first(); // assert (x <= sz(target)); B to_delete = 0; B next_target = target; for (int y = x; y <= n; y += x) { if (!target[y]) { to_delete[y] = 1; } else { next_target[y] = 0; } } int idx = x; if (!to_delete.none()) { int to_delete_idx = process(to_delete); // assert(b[to_delete_idx] == to_delete); do_neg(to_delete_idx); do_and(x, sz(b) - 1); idx = sz(b) - 1; } // assert((b[idx] | next_target) == target); if (next_target.none()) { // assert(b[idx] == target); return idx; } int next_target_idx = process(next_target); // assert(b[next_target_idx] == next_target); do_or(idx, next_target_idx); return sz(b) - 1; } signed main() { cin.tie(0)->sync_with_stdio(0); #ifdef LOCAL int t = 1e9; #else int t = 1; #endif rep(tc,0,t) { #ifdef LOCAL n = 50'000; int s = randr(1, n); #else int s; cin >> n >> s; #endif b.clear(); ops.clear(); b.resize(n + 1); rep(i,1,n+1) { for (int j = i; j <= n; j += i) { b[i][j] = 1; } } B goal = 0; #ifdef LOCAL int cnt = 0; while (cnt < s) { int x = randr(1, n); cnt += (goal[x] == 0); goal[x] = 1; } #else rep(i,0,s) { int x; cin >> x; goal[x] = 1; } #endif int m = process(goal); if (m <= n) { do_or(m, m); m = sz(b) - 1; } // assert(b.back() == goal); #ifdef LOCAL assert(sz(ops) <= 100'000); // for (int i = n + 1; i <= m; i++) { // auto [type, x, y] = ops[i - n - 1]; // if (type == 3) { // b[i] = ~b[x]; // continue; // } // if (type == 1) { // b[i] = b[x] | b[y]; // } // else { // b[i] = b[x] & b[y]; // } // } // assert(b.back() == goal); cout << "TEST " + to_string(tc) + ": OK, ops: " + to_string(sz(ops)) << endl; #else cout << sz(ops) << '\n'; for (auto [type, x, y] : ops) { cout << type << ' ' << x; if (type != 3) cout << ' ' << y; cout << '\n'; } #endif } return 0; } |