#include <cstdio> #include <cmath> #include <vector> #include <string> using namespace std; const int MAX_K = 1e9; const int SQRT_MAX_K = 31623; vector<int> primes; class NumberDescription { public: int onesCount = 0; bool isPrime = true; }; vector<int> getFactors(int x) { vector<int> v; int limit = sqrt(x); for(const auto element : primes) { if (element > limit) break; do { auto x1 = x / element; if (x1 * element == x) { x = x1; v.push_back(element); x1 = x / element; } else { break; } } while (x > 1); } if (x > 1) v.push_back(x); return v; } NumberDescription numbers[SQRT_MAX_K + 1]; void CreatePrimesVector() { numbers[2].onesCount; numbers[2].isPrime = true; int i; for(i = 2; i <= SQRT_MAX_K/2+1; ++i) { if (!numbers[i].isPrime) continue; for(auto j = i*2; j <= SQRT_MAX_K; j+=i) { numbers[j].isPrime = false; } primes.push_back(i); } for (; i <= SQRT_MAX_K; ++i) { if (!numbers[i].isPrime) continue; primes.push_back(i); } } string createStringFromFactors(const vector<int>& factors, int& onesSoFar); string factorToString(int currentFactor, int& onesSoFar) { switch (currentFactor) { case 2: onesSoFar += 2; if (onesSoFar > 100) return ""; return "(1+1)"; case 3: onesSoFar += 3; if (onesSoFar > 100) return ""; return "(1+1+1)"; case 5: onesSoFar += 5; if (onesSoFar > 100) return ""; return "(1+1+1+1+1)"; default: auto factors = getFactors(currentFactor-1); auto x = createStringFromFactors(factors, onesSoFar); onesSoFar += 1; if (onesSoFar > 100) return ""; return "(1+" + x +")"; } } string createStringFromFactors(const vector<int>& factors, int& onesSoFar) { string result = ""; for(int i = 0; i < factors.size(); ++i) { auto currentFactor = factors[i]; const auto& factored = factorToString(currentFactor, onesSoFar); if (i == 0) result += factored; else result += "*" + factored; }; return result; } int main() { primes.reserve(3401); CreatePrimesVector(); int t, k; scanf("%d", &t); while(t-->0) { scanf("%d", &k); if(k==1) { printf("1\n"); } else { auto onesSoFar = 0; const auto factors = getFactors(k); auto x = createStringFromFactors(factors, onesSoFar); if (onesSoFar > 100) printf("NIE\n"); else printf("%s\n", x.c_str()); } } }
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 | #include <cstdio> #include <cmath> #include <vector> #include <string> using namespace std; const int MAX_K = 1e9; const int SQRT_MAX_K = 31623; vector<int> primes; class NumberDescription { public: int onesCount = 0; bool isPrime = true; }; vector<int> getFactors(int x) { vector<int> v; int limit = sqrt(x); for(const auto element : primes) { if (element > limit) break; do { auto x1 = x / element; if (x1 * element == x) { x = x1; v.push_back(element); x1 = x / element; } else { break; } } while (x > 1); } if (x > 1) v.push_back(x); return v; } NumberDescription numbers[SQRT_MAX_K + 1]; void CreatePrimesVector() { numbers[2].onesCount; numbers[2].isPrime = true; int i; for(i = 2; i <= SQRT_MAX_K/2+1; ++i) { if (!numbers[i].isPrime) continue; for(auto j = i*2; j <= SQRT_MAX_K; j+=i) { numbers[j].isPrime = false; } primes.push_back(i); } for (; i <= SQRT_MAX_K; ++i) { if (!numbers[i].isPrime) continue; primes.push_back(i); } } string createStringFromFactors(const vector<int>& factors, int& onesSoFar); string factorToString(int currentFactor, int& onesSoFar) { switch (currentFactor) { case 2: onesSoFar += 2; if (onesSoFar > 100) return ""; return "(1+1)"; case 3: onesSoFar += 3; if (onesSoFar > 100) return ""; return "(1+1+1)"; case 5: onesSoFar += 5; if (onesSoFar > 100) return ""; return "(1+1+1+1+1)"; default: auto factors = getFactors(currentFactor-1); auto x = createStringFromFactors(factors, onesSoFar); onesSoFar += 1; if (onesSoFar > 100) return ""; return "(1+" + x +")"; } } string createStringFromFactors(const vector<int>& factors, int& onesSoFar) { string result = ""; for(int i = 0; i < factors.size(); ++i) { auto currentFactor = factors[i]; const auto& factored = factorToString(currentFactor, onesSoFar); if (i == 0) result += factored; else result += "*" + factored; }; return result; } int main() { primes.reserve(3401); CreatePrimesVector(); int t, k; scanf("%d", &t); while(t-->0) { scanf("%d", &k); if(k==1) { printf("1\n"); } else { auto onesSoFar = 0; const auto factors = getFactors(k); auto x = createStringFromFactors(factors, onesSoFar); if (onesSoFar > 100) printf("NIE\n"); else printf("%s\n", x.c_str()); } } } |