#include <iostream> #include <vector> #include <set> #include <unordered_map> #include <algorithm> #include <iterator> enum OperationType { SUM = 1, INTERSECTION = 2, NEGATION = 3 }; struct Data { unsigned n; unsigned s; std::set<unsigned> B; std::unordered_map<unsigned, std::set<unsigned>> sets; } data; void ReadData() { std::cin >> data.n >> data.s; for (unsigned i = 0; i <= data.s; ++i) { unsigned val; std::cin >> val; data.B.insert(val); } for (unsigned i = 1; i <= data.n; ++i) { std::set<unsigned> s; for (unsigned j = data.n; j >= i; --j) { if (j % i == 0) s.insert(j); } data.sets[i] = s; } } struct operation { OperationType op; unsigned x; unsigned y; }; void intersect_neg(std::set<unsigned> &curr, const std::set<unsigned> &neg) { for (unsigned i = 1; i <= data.n; ++i) { if (neg.contains(i)) curr.erase(i); } } void sum(std::set<unsigned> &curr, const std::set<unsigned> &sum) { for (unsigned i = 1; i <= data.n; ++i) { if (sum.contains(i)) curr.insert(i); } } std::vector<operation> BuildSet() { unsigned current_set_id = 1; unsigned m = data.n; std::set<unsigned> current_set = data.sets[current_set_id]; std::vector<operation> ops; for (unsigned i = 1; i <= data.n; ++i) { if (!data.B.contains(i) && current_set.contains(i)) { ops.emplace_back(NEGATION, i); m++; ops.emplace_back(INTERSECTION, current_set_id, m); intersect_neg(current_set, data.sets[i]); current_set_id = ++m; } else if (data.B.contains(i) && !current_set.contains(i)) { ops.emplace_back(SUM, current_set_id, i); sum(current_set, data.sets[i]); current_set_id = ++m; } } return ops; } void print_ops(const std::vector<operation> ops) { std::cout << ops.size() << "\n"; for (const auto op : ops) { switch (op.op) { case NEGATION: std::cout << op.op << " " << op.x << "\n"; break; case INTERSECTION: case SUM: std::cout << op.op << " " << op.x << " " << op.y << "\n"; break; default: break; } } } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); ReadData(); const auto ops = BuildSet(); print_ops(std::move(ops)); 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 | #include <iostream> #include <vector> #include <set> #include <unordered_map> #include <algorithm> #include <iterator> enum OperationType { SUM = 1, INTERSECTION = 2, NEGATION = 3 }; struct Data { unsigned n; unsigned s; std::set<unsigned> B; std::unordered_map<unsigned, std::set<unsigned>> sets; } data; void ReadData() { std::cin >> data.n >> data.s; for (unsigned i = 0; i <= data.s; ++i) { unsigned val; std::cin >> val; data.B.insert(val); } for (unsigned i = 1; i <= data.n; ++i) { std::set<unsigned> s; for (unsigned j = data.n; j >= i; --j) { if (j % i == 0) s.insert(j); } data.sets[i] = s; } } struct operation { OperationType op; unsigned x; unsigned y; }; void intersect_neg(std::set<unsigned> &curr, const std::set<unsigned> &neg) { for (unsigned i = 1; i <= data.n; ++i) { if (neg.contains(i)) curr.erase(i); } } void sum(std::set<unsigned> &curr, const std::set<unsigned> &sum) { for (unsigned i = 1; i <= data.n; ++i) { if (sum.contains(i)) curr.insert(i); } } std::vector<operation> BuildSet() { unsigned current_set_id = 1; unsigned m = data.n; std::set<unsigned> current_set = data.sets[current_set_id]; std::vector<operation> ops; for (unsigned i = 1; i <= data.n; ++i) { if (!data.B.contains(i) && current_set.contains(i)) { ops.emplace_back(NEGATION, i); m++; ops.emplace_back(INTERSECTION, current_set_id, m); intersect_neg(current_set, data.sets[i]); current_set_id = ++m; } else if (data.B.contains(i) && !current_set.contains(i)) { ops.emplace_back(SUM, current_set_id, i); sum(current_set, data.sets[i]); current_set_id = ++m; } } return ops; } void print_ops(const std::vector<operation> ops) { std::cout << ops.size() << "\n"; for (const auto op : ops) { switch (op.op) { case NEGATION: std::cout << op.op << " " << op.x << "\n"; break; case INTERSECTION: case SUM: std::cout << op.op << " " << op.x << " " << op.y << "\n"; break; default: break; } } } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); ReadData(); const auto ops = BuildSet(); print_ops(std::move(ops)); return 0; } |