//Piotr Golda #include <iostream> #include <cstdio> #include <string> #include <vector> #include <sstream> #include <map> using namespace std; #define uint unsigned int uint T; std::map < uint, std::pair<std::string, uint > > best; std::pair<std::string, uint> compute(uint N); std::pair<std::string, uint> computePrime(uint P); std::string ones(uint n) { std::stringstream ss{ "" }; ss << "1"; for (uint i = 1; i < n; ++i) { ss << "+1"; } return ss.str(); } std::vector<uint > factorize(uint n) { std::vector< uint > factors; while (n % 2 == 0) { factors.push_back(2); n /= 2; } uint i = 3; while (i*i <= n) { while (n%i == 0) { factors.push_back(i); n /= i; } i += 2; } if (n > 1) { factors.push_back(n); } return std::move(factors); } std::pair<std::string, uint> computePrime(uint P) { auto pos = best.find(P); if (pos != best.end()) { return pos->second; } uint onesNum{ 1 }; std::stringstream ss{ "" }; std::string out; uint onesN; std::tie(out,onesN) = compute(P - 1); ss << "1+(" << out << ")"; onesNum += onesN; best[P] = { ss.str(), onesNum }; return { ss.str(), onesNum }; } std::pair<std::string, uint> compute(uint N) { auto pos = best.find(N); if ( pos != best.end()) { return pos->second; } uint onesNum{ 0 }; std::stringstream ss{ "" }; std::vector<uint > factors = factorize(N); std::string out; uint onesN; std::tie(out, onesN) = computePrime(factors[0]); ss << "(" << out << ")"; onesNum += onesN; for (uint i = 1; i < factors.size(); ++i) { std::tie(out, onesN) = computePrime(factors[i]); ss << "*(" << out << ")"; onesNum += onesN; } best[N] = { ss.str(), onesNum }; return{ ss.str(), onesNum }; } void init() { best[1] = { "1", 1 }; best[2] = { "1+1", 2 }; best[3] = { "1+1+1", 3 }; } int main() { int N; std::string out; uint onesN; scanf("%u", &T); init(); for (uint t = 0; t < T; ++t) { scanf("%u", &N); std::tie(out, onesN) = compute(N); if (onesN <= 100) { std::cout << out << std::endl; } else { std::cout << "NIE" << std::endl; } } 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 | //Piotr Golda #include <iostream> #include <cstdio> #include <string> #include <vector> #include <sstream> #include <map> using namespace std; #define uint unsigned int uint T; std::map < uint, std::pair<std::string, uint > > best; std::pair<std::string, uint> compute(uint N); std::pair<std::string, uint> computePrime(uint P); std::string ones(uint n) { std::stringstream ss{ "" }; ss << "1"; for (uint i = 1; i < n; ++i) { ss << "+1"; } return ss.str(); } std::vector<uint > factorize(uint n) { std::vector< uint > factors; while (n % 2 == 0) { factors.push_back(2); n /= 2; } uint i = 3; while (i*i <= n) { while (n%i == 0) { factors.push_back(i); n /= i; } i += 2; } if (n > 1) { factors.push_back(n); } return std::move(factors); } std::pair<std::string, uint> computePrime(uint P) { auto pos = best.find(P); if (pos != best.end()) { return pos->second; } uint onesNum{ 1 }; std::stringstream ss{ "" }; std::string out; uint onesN; std::tie(out,onesN) = compute(P - 1); ss << "1+(" << out << ")"; onesNum += onesN; best[P] = { ss.str(), onesNum }; return { ss.str(), onesNum }; } std::pair<std::string, uint> compute(uint N) { auto pos = best.find(N); if ( pos != best.end()) { return pos->second; } uint onesNum{ 0 }; std::stringstream ss{ "" }; std::vector<uint > factors = factorize(N); std::string out; uint onesN; std::tie(out, onesN) = computePrime(factors[0]); ss << "(" << out << ")"; onesNum += onesN; for (uint i = 1; i < factors.size(); ++i) { std::tie(out, onesN) = computePrime(factors[i]); ss << "*(" << out << ")"; onesNum += onesN; } best[N] = { ss.str(), onesNum }; return{ ss.str(), onesNum }; } void init() { best[1] = { "1", 1 }; best[2] = { "1+1", 2 }; best[3] = { "1+1+1", 3 }; } int main() { int N; std::string out; uint onesN; scanf("%u", &T); init(); for (uint t = 0; t < T; ++t) { scanf("%u", &N); std::tie(out, onesN) = compute(N); if (onesN <= 100) { std::cout << out << std::endl; } else { std::cout << "NIE" << std::endl; } } return 0; } |