#include<cstdio> #include<vector> #include<cmath> using namespace std; int count_ones(const vector<char>& v) { int acc = 0; const int size = v.size(); for(int i = 0; i < size; i++) { if (v[i] == '1') acc++; } return acc; } void concat(vector<char>& out, const vector<char>& left, char c, const vector<char>& right) { out.push_back('('); for(char c : left) { out.push_back(c); } out.push_back(c); for(char c : right) { out.push_back(c); } out.push_back(')'); } bool solve(int n, int available_ones, vector<char>& out) { if (available_ones <= 0) return false; if (n == 0) { return true; } if (n == 1) { out.push_back('1'); return true; } const int sq = ceil(sqrt(n)); for(int i = 2; i < sq; i++) { if (n % i == 0) { vector<char> right; bool ok_right = solve(n/i, available_ones - 1, right); if (!ok_right) continue; int used_ones = count_ones(right); vector<char> left; bool ok_left = solve(i, available_ones - used_ones, left); if (!ok_left) continue; int total_ones = used_ones + count_ones(left); if (total_ones <= available_ones) { concat(out, left, '*', right); return true; } } } vector<char> last; bool last_ok = solve(n-1, available_ones-1, last); if (!last_ok) { return false; } else { out.push_back('('); out.push_back('1'); out.push_back('+'); for(char c : last) out.push_back(c); out.push_back(')'); return true; } } int main() { int t; scanf("%d", &t); while(t--) { int k; scanf("%d", &k); vector<char> out; if (solve(k, 101, out)) { for(char c : out) { printf("%c", c); } printf("\n"); } else { printf("NIE\n"); } } 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 | #include<cstdio> #include<vector> #include<cmath> using namespace std; int count_ones(const vector<char>& v) { int acc = 0; const int size = v.size(); for(int i = 0; i < size; i++) { if (v[i] == '1') acc++; } return acc; } void concat(vector<char>& out, const vector<char>& left, char c, const vector<char>& right) { out.push_back('('); for(char c : left) { out.push_back(c); } out.push_back(c); for(char c : right) { out.push_back(c); } out.push_back(')'); } bool solve(int n, int available_ones, vector<char>& out) { if (available_ones <= 0) return false; if (n == 0) { return true; } if (n == 1) { out.push_back('1'); return true; } const int sq = ceil(sqrt(n)); for(int i = 2; i < sq; i++) { if (n % i == 0) { vector<char> right; bool ok_right = solve(n/i, available_ones - 1, right); if (!ok_right) continue; int used_ones = count_ones(right); vector<char> left; bool ok_left = solve(i, available_ones - used_ones, left); if (!ok_left) continue; int total_ones = used_ones + count_ones(left); if (total_ones <= available_ones) { concat(out, left, '*', right); return true; } } } vector<char> last; bool last_ok = solve(n-1, available_ones-1, last); if (!last_ok) { return false; } else { out.push_back('('); out.push_back('1'); out.push_back('+'); for(char c : last) out.push_back(c); out.push_back(')'); return true; } } int main() { int t; scanf("%d", &t); while(t--) { int k; scanf("%d", &k); vector<char> out; if (solve(k, 101, out)) { for(char c : out) { printf("%c", c); } printf("\n"); } else { printf("NIE\n"); } } return 0; } |