#include <iostream> #include <list> #include <map> struct Expr { std::string render = ""; std::uint_fast32_t ones = 0; }; class Calculator { public: Calculator() { Expr one; one.render = "1"; one.ones = 1; // Naprawdę nie powinienem musieć tego robić. exprs[1] = one; } Expr getExpr(std::uint_fast32_t in) { auto find = exprs.find(in); if (find != exprs.end()) { return find->second; } else { Expr ret; while (in % 2 == 0) { in /= 2; modPrimeExpr(2, ret); } for (std::uint_fast32_t d = 3; in != 1; d += 2) { while (in % d == 0) { in /= d; modPrimeExpr(d, ret); } } exprs.insert({in, ret}); return ret; } } private: void modPrimeExpr(std::uint_fast32_t in, Expr ¤t) { bool first = current.ones == 0; if (!first) { current.render.insert(0, 1, '('); current.render += '*'; } auto find = exprs.find(in); if (find != exprs.end()) { current.render += find->second.render; current.ones += find->second.ones; } else { Expr prime = getExpr(in - 1); prime.render.insert(0, 1, '('); prime.render += "+1)"; prime.ones++; exprs.insert({in, prime}); current.render += prime.render; current.ones += prime.ones; } if (!first) { current.render += ')'; } } private: std::map<std::uint_fast32_t, Expr> exprs; }; int main() { std::ios_base::sync_with_stdio(false); Calculator calc; std::uint_fast16_t t; std::cin >> t; for (std::uint_fast16_t i = 0; i < t; i++) { std::uint_fast32_t k_i; std::cin >> k_i; Expr out = calc.getExpr(k_i); if (out.ones > 100) { std::cout << "NIE\n"; } else { std::cout << out.render << "\n"; } } }
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 | #include <iostream> #include <list> #include <map> struct Expr { std::string render = ""; std::uint_fast32_t ones = 0; }; class Calculator { public: Calculator() { Expr one; one.render = "1"; one.ones = 1; // Naprawdę nie powinienem musieć tego robić. exprs[1] = one; } Expr getExpr(std::uint_fast32_t in) { auto find = exprs.find(in); if (find != exprs.end()) { return find->second; } else { Expr ret; while (in % 2 == 0) { in /= 2; modPrimeExpr(2, ret); } for (std::uint_fast32_t d = 3; in != 1; d += 2) { while (in % d == 0) { in /= d; modPrimeExpr(d, ret); } } exprs.insert({in, ret}); return ret; } } private: void modPrimeExpr(std::uint_fast32_t in, Expr ¤t) { bool first = current.ones == 0; if (!first) { current.render.insert(0, 1, '('); current.render += '*'; } auto find = exprs.find(in); if (find != exprs.end()) { current.render += find->second.render; current.ones += find->second.ones; } else { Expr prime = getExpr(in - 1); prime.render.insert(0, 1, '('); prime.render += "+1)"; prime.ones++; exprs.insert({in, prime}); current.render += prime.render; current.ones += prime.ones; } if (!first) { current.render += ')'; } } private: std::map<std::uint_fast32_t, Expr> exprs; }; int main() { std::ios_base::sync_with_stdio(false); Calculator calc; std::uint_fast16_t t; std::cin >> t; for (std::uint_fast16_t i = 0; i < t; i++) { std::uint_fast32_t k_i; std::cin >> k_i; Expr out = calc.getExpr(k_i); if (out.ones > 100) { std::cout << "NIE\n"; } else { std::cout << out.render << "\n"; } } } |