#include <iostream> #include <memory> #include <vector> #include <string> #include <unordered_map> using ull = unsigned long long; struct part { virtual ~part() { } virtual ull ones() = 0; virtual std::string to_string() = 0; }; struct group : part { group(ull total_ = 0) : total(total_) { } ull ones() override { return total; } std::string to_string() override { std::string result; result.reserve(total * 2 + 1); if (total > 1) { result.append("("); } for (int i = 1; i < total; ++i) { result.append("1+"); } result.append("1"); if (total > 1) { result.append(")"); } return result; } ull total; }; std::unordered_map<int, part*> cache; struct ops : part { ops(char op_) : op(op_) { } ull ones() override { ull result = 0; for (auto &&p : parts) { result += cache[p]->ones(); } return result; } std::string to_string() override { std::string result = '(' + cache[parts[0]]->to_string(); for (int i = 1; i < parts.size(); ++i) { result += op + cache[parts[i]]->to_string(); } return result + ')'; } std::vector<ull> parts; char op; }; void decompose_prime(ull); void decompose(ull x) { if (cache.find(x) != cache.end()) { return; } std::vector<ull> decomp; ull val = x; for (ull i = 2; i * i <= x; ++i) { while (x % i == 0) { decomp.push_back(i); x /= i; } } if (x == val) { decompose_prime(x); } else { if (x != 1) { decomp.push_back(x); } for (auto &&d : decomp) { if (d > 5) { decompose_prime(d); } } // ----- ops *o = new ops('*'); o->parts = std::move(decomp); cache[val] = o; } } void decompose_prime(ull x) { if (cache.find(x) != cache.end()) { return; } decompose(x - 1); ops *o = new ops('+'); o->parts.push_back(x - 1); o->parts.push_back(1); cache[x] = o; } int main() { std::ios::sync_with_stdio(false); // ----- for (ull i = 1; i <= 5; ++i) { cache[i] = new group(i); } // ----- int t; std::cin >> t; while (t--) { ull k; std::cin >> k; decompose(k); if (cache[k]->ones() <= 100) { std::cout << cache[k]->to_string() << std::endl; } else { std::cout << "NIE" << std::endl; } } // don't clean /*for (auto &&i : cache) { delete i.second; }*/ 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 | #include <iostream> #include <memory> #include <vector> #include <string> #include <unordered_map> using ull = unsigned long long; struct part { virtual ~part() { } virtual ull ones() = 0; virtual std::string to_string() = 0; }; struct group : part { group(ull total_ = 0) : total(total_) { } ull ones() override { return total; } std::string to_string() override { std::string result; result.reserve(total * 2 + 1); if (total > 1) { result.append("("); } for (int i = 1; i < total; ++i) { result.append("1+"); } result.append("1"); if (total > 1) { result.append(")"); } return result; } ull total; }; std::unordered_map<int, part*> cache; struct ops : part { ops(char op_) : op(op_) { } ull ones() override { ull result = 0; for (auto &&p : parts) { result += cache[p]->ones(); } return result; } std::string to_string() override { std::string result = '(' + cache[parts[0]]->to_string(); for (int i = 1; i < parts.size(); ++i) { result += op + cache[parts[i]]->to_string(); } return result + ')'; } std::vector<ull> parts; char op; }; void decompose_prime(ull); void decompose(ull x) { if (cache.find(x) != cache.end()) { return; } std::vector<ull> decomp; ull val = x; for (ull i = 2; i * i <= x; ++i) { while (x % i == 0) { decomp.push_back(i); x /= i; } } if (x == val) { decompose_prime(x); } else { if (x != 1) { decomp.push_back(x); } for (auto &&d : decomp) { if (d > 5) { decompose_prime(d); } } // ----- ops *o = new ops('*'); o->parts = std::move(decomp); cache[val] = o; } } void decompose_prime(ull x) { if (cache.find(x) != cache.end()) { return; } decompose(x - 1); ops *o = new ops('+'); o->parts.push_back(x - 1); o->parts.push_back(1); cache[x] = o; } int main() { std::ios::sync_with_stdio(false); // ----- for (ull i = 1; i <= 5; ++i) { cache[i] = new group(i); } // ----- int t; std::cin >> t; while (t--) { ull k; std::cin >> k; decompose(k); if (cache[k]->ones() <= 100) { std::cout << cache[k]->to_string() << std::endl; } else { std::cout << "NIE" << std::endl; } } // don't clean /*for (auto &&i : cache) { delete i.second; }*/ return 0; } |