#include <iostream> #include <string> #include <vector> #include <algorithm> #include <unordered_map> using namespace std; vector<int> primeFactors(int n) { vector<int> result; while (n % 2 == 0) { result.push_back(2); n = n / 2; } for (int i = 3; i <= sqrt(n); i = i + 2) { while (n%i == 0) { result.push_back(i); n = n / i; } } if (n > 2) result.push_back(n); return result; } bool isOne(char s) { return s == '1'; } unordered_map<int, string> map; string solve(int n) { if (n == 1) return "1"; else if (n == 2) return "1+1"; else if (n == 3) return "1+1+1"; auto found = map.find(n); if (found != map.end()) return found->second; auto result = primeFactors(n); if (result.size() == 1) { const string output = "(" + solve(n - 1) + ")+1"; map.insert(make_pair(n, output)); return output; } else { string output = ""; for (auto&& x : result) { string tmp; found = map.find(x); if (found != map.end()) tmp = found->second; else { tmp = solve(x); map.insert(make_pair(n, tmp)); } if (tmp != "1") { if (output != "") output += "*"; output += "(" + tmp + ")"; } } map.insert(make_pair(n, output)); return output; } } int main() { ios::sync_with_stdio(false); int t; cin >> t; while (t--) { int x; cin >> x; const string res = solve(x); int ones = count_if(res.begin(), res.end(), isOne); if (ones > 100) cout << "NIE"; else cout << res; cout << 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 | #include <iostream> #include <string> #include <vector> #include <algorithm> #include <unordered_map> using namespace std; vector<int> primeFactors(int n) { vector<int> result; while (n % 2 == 0) { result.push_back(2); n = n / 2; } for (int i = 3; i <= sqrt(n); i = i + 2) { while (n%i == 0) { result.push_back(i); n = n / i; } } if (n > 2) result.push_back(n); return result; } bool isOne(char s) { return s == '1'; } unordered_map<int, string> map; string solve(int n) { if (n == 1) return "1"; else if (n == 2) return "1+1"; else if (n == 3) return "1+1+1"; auto found = map.find(n); if (found != map.end()) return found->second; auto result = primeFactors(n); if (result.size() == 1) { const string output = "(" + solve(n - 1) + ")+1"; map.insert(make_pair(n, output)); return output; } else { string output = ""; for (auto&& x : result) { string tmp; found = map.find(x); if (found != map.end()) tmp = found->second; else { tmp = solve(x); map.insert(make_pair(n, tmp)); } if (tmp != "1") { if (output != "") output += "*"; output += "(" + tmp + ")"; } } map.insert(make_pair(n, output)); return output; } } int main() { ios::sync_with_stdio(false); int t; cin >> t; while (t--) { int x; cin >> x; const string res = solve(x); int ones = count_if(res.begin(), res.end(), isOne); if (ones > 100) cout << "NIE"; else cout << res; cout << endl; } return 0; } |