#include <unordered_map> #include <string> #include <iostream> bool ok(std::string S) { int bilans = 0; for (auto it : S) { if (it == '(') bilans++; if (it == ')') bilans--; if (bilans < 0) return false; } if (bilans != 0) return false; return true; } std::unordered_map<int, std::string> H; std::unordered_map<int, int> getPrimeFactors(int k) { std::unordered_map<int, int> res; for (int i = 2; i * i <= k; ++i) { while (k % i == 0) { res[i] ++; k /= i; } } if (k != 1) res[k] = 1; return res; } std::string get(int k) { auto ptr = H.find(k); if (ptr != H.end()) return ptr->second; auto r = getPrimeFactors(k); std::string result; if (r.size() == 1 && r.find(k) != r.end()) { result += "(" + get(k - 1) + "+1)"; } else { result = "("; for (auto it = r.begin(); it != r.end(); ++it) { if (it != r.begin()) result += "*"; for (int i = 0; i < it->second; ++i) { result += get(it->first); if (i < it->second - 1) result += "*"; } } result += ")"; } H[k] = result; return result; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL); H[1] = "1"; H[2] = "(1+1)"; int t; std::cin >> t; while (t--) { //std::cerr << t << "\n"; int k; std::cin >> k; auto r = get(k);; //if (ok(r) == false) { // std::cerr << "KUPKA\n"; // return 1; //} int cnt = 0; for (auto it : r) if (it == '1') ++cnt; if (cnt > 100) std::cout << "NIE\n"; else std::cout << r << "\n"; } 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 | #include <unordered_map> #include <string> #include <iostream> bool ok(std::string S) { int bilans = 0; for (auto it : S) { if (it == '(') bilans++; if (it == ')') bilans--; if (bilans < 0) return false; } if (bilans != 0) return false; return true; } std::unordered_map<int, std::string> H; std::unordered_map<int, int> getPrimeFactors(int k) { std::unordered_map<int, int> res; for (int i = 2; i * i <= k; ++i) { while (k % i == 0) { res[i] ++; k /= i; } } if (k != 1) res[k] = 1; return res; } std::string get(int k) { auto ptr = H.find(k); if (ptr != H.end()) return ptr->second; auto r = getPrimeFactors(k); std::string result; if (r.size() == 1 && r.find(k) != r.end()) { result += "(" + get(k - 1) + "+1)"; } else { result = "("; for (auto it = r.begin(); it != r.end(); ++it) { if (it != r.begin()) result += "*"; for (int i = 0; i < it->second; ++i) { result += get(it->first); if (i < it->second - 1) result += "*"; } } result += ")"; } H[k] = result; return result; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL); H[1] = "1"; H[2] = "(1+1)"; int t; std::cin >> t; while (t--) { //std::cerr << t << "\n"; int k; std::cin >> k; auto r = get(k);; //if (ok(r) == false) { // std::cerr << "KUPKA\n"; // return 1; //} int cnt = 0; for (auto it : r) if (it == '1') ++cnt; if (cnt > 100) std::cout << "NIE\n"; else std::cout << r << "\n"; } return 0; } |