#include <cstdio> #include <algorithm> #include <string> #include <vector> using namespace std; string result; int ile = 0; unsigned long long twoResults[32]; unsigned long long threeResults[32]; std::vector<unsigned long long> primes; void print_ones(int ile, bool omit_par) { if (!ile) return; if (ile == 1) { result.append("1"); return; } if(!omit_par) result.append("("); result.append("1"); for (int i = 1; i < ile; ++i) result.append("+1"); if (!omit_par) result.append(")"); } unsigned long long solve3(unsigned long long v) { if (!v) return 0; if (v % 2 == 0) { ile += 2; v /= 2; if (!result.empty() && *result.rbegin() != '(') result.append("+"); if (v == 1) v = 0; print_ones(2, v == 0); if (v > 0) { result.append("*("); v = solve3(v); result.append(")"); } } else if (v % 3 == 0) { ile += 3; v /= 3; if (!result.empty() && *result.rbegin() != '(') result.append("+"); if (v == 1) v = 0; print_ones(3, v == 0); if (v) { result.append("*("); v = solve3(v); result.append(")"); } } else { bool found = false; for (int i = 0; v && i < primes.size() && primes[i] < v; ++i) { if (v % primes[i] == 0) { result.append("("); solve3(primes[i]); result.append(")"); v /= primes[i]; result.append("*("); v = solve3(v); result.append(")"); found = true; } } //prime number if (!found) { if (!result.empty() && *result.rbegin() != '(') result.append("+"); print_ones(1, true); v -= 1; ++ile; } } if (v) v = solve3(v); return v; } int main() { primes.push_back(2); for (int i = 3; i < 100000; i++) { bool prime = true; for (int j = 0;j<primes.size() && primes[j] * primes[j] <= i;j++) { if (i % primes[j] == 0) { prime = false; break; } } if (prime) { primes.push_back(i); } } twoResults[0] = 1; threeResults[0] = 1; for (int i = 1; i < 32; ++i) { twoResults[i] = twoResults[i - 1] * 2; threeResults[i] = threeResults[i - 1] * 3; } int n; scanf("%d", &n); while (n--) { long long v; scanf("%lld", &v); solve3(v); if (ile <= 100) printf("%s\n", result.c_str()); else puts("NIE"); ile = 0; result.clear(); } 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 | #include <cstdio> #include <algorithm> #include <string> #include <vector> using namespace std; string result; int ile = 0; unsigned long long twoResults[32]; unsigned long long threeResults[32]; std::vector<unsigned long long> primes; void print_ones(int ile, bool omit_par) { if (!ile) return; if (ile == 1) { result.append("1"); return; } if(!omit_par) result.append("("); result.append("1"); for (int i = 1; i < ile; ++i) result.append("+1"); if (!omit_par) result.append(")"); } unsigned long long solve3(unsigned long long v) { if (!v) return 0; if (v % 2 == 0) { ile += 2; v /= 2; if (!result.empty() && *result.rbegin() != '(') result.append("+"); if (v == 1) v = 0; print_ones(2, v == 0); if (v > 0) { result.append("*("); v = solve3(v); result.append(")"); } } else if (v % 3 == 0) { ile += 3; v /= 3; if (!result.empty() && *result.rbegin() != '(') result.append("+"); if (v == 1) v = 0; print_ones(3, v == 0); if (v) { result.append("*("); v = solve3(v); result.append(")"); } } else { bool found = false; for (int i = 0; v && i < primes.size() && primes[i] < v; ++i) { if (v % primes[i] == 0) { result.append("("); solve3(primes[i]); result.append(")"); v /= primes[i]; result.append("*("); v = solve3(v); result.append(")"); found = true; } } //prime number if (!found) { if (!result.empty() && *result.rbegin() != '(') result.append("+"); print_ones(1, true); v -= 1; ++ile; } } if (v) v = solve3(v); return v; } int main() { primes.push_back(2); for (int i = 3; i < 100000; i++) { bool prime = true; for (int j = 0;j<primes.size() && primes[j] * primes[j] <= i;j++) { if (i % primes[j] == 0) { prime = false; break; } } if (prime) { primes.push_back(i); } } twoResults[0] = 1; threeResults[0] = 1; for (int i = 1; i < 32; ++i) { twoResults[i] = twoResults[i - 1] * 2; threeResults[i] = threeResults[i - 1] * 3; } int n; scanf("%d", &n); while (n--) { long long v; scanf("%lld", &v); solve3(v); if (ile <= 100) printf("%s\n", result.c_str()); else puts("NIE"); ile = 0; result.clear(); } return 0; } |