#include <bits/stdc++.h> #define F first #define S second #define pb push_back #define INF (1 << 30) #define SQR(a) ((a) * (a)) using namespace std; const int N = 1111; bool check(string& s, int l, int r) { if ((r - l) == 1) return true; int lvl = 0; for (int i = l; i < r; i++) { if (s[i] == '(') lvl++; else if (s[i] == ')') lvl--; else { if (lvl == 0 && s[i] == '+') return true; } } return false; } string debrace(string s) { vector<int> v; vector<int> f(s.size(), 0); f[0] = 1; f.back() = 1; string res; for (int i = 0; i < (int)s.size(); i++) { if (s[i] == '(') { v.push_back(i); } else if (s[i] == ')') { if (!check(s, v.back() + 1, i)) { f[v.back()] = 1; f[i] = 1; } v.pop_back(); } } for (int i = 0; i < (int)s.size(); i++) { if (f[i] == 0) res += s[i]; } return res; } string brace(string s) { return "(" + s + ")"; } string solve(int k) { string nxt = ""; if (k / 2 > 1) nxt = brace("1+1") + "*" + brace(solve(k / 2)); if (k / 2 == 1) nxt = brace("1+1"); string cur = ""; if (k % 2 != 0) cur = "1"; if (nxt == "") return brace(cur); if (cur == "") return brace(nxt); return brace(cur + "+" + nxt); } bool delim(char c) { return c == ' '; } bool is_op(char c) { return c == '+' || c == '-' || c == '*' || c == '/' || c == '%'; } int priority(char op) { return op == '+' || op == '-' ? 1 : op == '*' || op == '/' || op == '%' ? 2 : -1; } void process_op(vector<int>& st, char op) { int r = st.back(); st.pop_back(); int l = st.back(); st.pop_back(); switch (op) { case '+': st.push_back(l + r); break; case '-': st.push_back(l - r); break; case '*': st.push_back(l * r); break; case '/': st.push_back(l / r); break; case '%': st.push_back(l % r); break; } } int calc(const string& s) { vector<int> st; vector<char> op; for (size_t i = 0; i < s.length(); ++i) if (!delim(s[i])) { if (s[i] == '(') { op.push_back('('); } else if (s[i] == ')') { while (op.back() != '(') process_op(st, op.back()), op.pop_back(); op.pop_back(); } else if (is_op(s[i])) { char curop = s[i]; while (!op.empty() && priority(op.back()) >= priority(s[i])) process_op(st, op.back()), op.pop_back(); op.push_back(curop); } else { string operand; while (i < s.length() && isalnum(s[i])) operand += s[i++]; --i; if (isdigit(operand[0])) st.push_back(atoi(operand.c_str())); } } while (!op.empty()) process_op(st, op.back()), op.pop_back(); return st.back(); } int countOnes(string s) { int res = 0; for (auto e : s) if (e == '1') res++; return res; } int main() { ios::sync_with_stdio(false); //freopen("input.txt", "r", stdin); int t = 0; cin >> t; while (t--) { int k; cin >> k; string str = debrace(solve(k)); if (countOnes(str) > 100) str = "NIE"; cout << str << endl; //assert(calc(str) == k); } 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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | #include <bits/stdc++.h> #define F first #define S second #define pb push_back #define INF (1 << 30) #define SQR(a) ((a) * (a)) using namespace std; const int N = 1111; bool check(string& s, int l, int r) { if ((r - l) == 1) return true; int lvl = 0; for (int i = l; i < r; i++) { if (s[i] == '(') lvl++; else if (s[i] == ')') lvl--; else { if (lvl == 0 && s[i] == '+') return true; } } return false; } string debrace(string s) { vector<int> v; vector<int> f(s.size(), 0); f[0] = 1; f.back() = 1; string res; for (int i = 0; i < (int)s.size(); i++) { if (s[i] == '(') { v.push_back(i); } else if (s[i] == ')') { if (!check(s, v.back() + 1, i)) { f[v.back()] = 1; f[i] = 1; } v.pop_back(); } } for (int i = 0; i < (int)s.size(); i++) { if (f[i] == 0) res += s[i]; } return res; } string brace(string s) { return "(" + s + ")"; } string solve(int k) { string nxt = ""; if (k / 2 > 1) nxt = brace("1+1") + "*" + brace(solve(k / 2)); if (k / 2 == 1) nxt = brace("1+1"); string cur = ""; if (k % 2 != 0) cur = "1"; if (nxt == "") return brace(cur); if (cur == "") return brace(nxt); return brace(cur + "+" + nxt); } bool delim(char c) { return c == ' '; } bool is_op(char c) { return c == '+' || c == '-' || c == '*' || c == '/' || c == '%'; } int priority(char op) { return op == '+' || op == '-' ? 1 : op == '*' || op == '/' || op == '%' ? 2 : -1; } void process_op(vector<int>& st, char op) { int r = st.back(); st.pop_back(); int l = st.back(); st.pop_back(); switch (op) { case '+': st.push_back(l + r); break; case '-': st.push_back(l - r); break; case '*': st.push_back(l * r); break; case '/': st.push_back(l / r); break; case '%': st.push_back(l % r); break; } } int calc(const string& s) { vector<int> st; vector<char> op; for (size_t i = 0; i < s.length(); ++i) if (!delim(s[i])) { if (s[i] == '(') { op.push_back('('); } else if (s[i] == ')') { while (op.back() != '(') process_op(st, op.back()), op.pop_back(); op.pop_back(); } else if (is_op(s[i])) { char curop = s[i]; while (!op.empty() && priority(op.back()) >= priority(s[i])) process_op(st, op.back()), op.pop_back(); op.push_back(curop); } else { string operand; while (i < s.length() && isalnum(s[i])) operand += s[i++]; --i; if (isdigit(operand[0])) st.push_back(atoi(operand.c_str())); } } while (!op.empty()) process_op(st, op.back()), op.pop_back(); return st.back(); } int countOnes(string s) { int res = 0; for (auto e : s) if (e == '1') res++; return res; } int main() { ios::sync_with_stdio(false); //freopen("input.txt", "r", stdin); int t = 0; cin >> t; while (t--) { int k; cin >> k; string str = debrace(solve(k)); if (countOnes(str) > 100) str = "NIE"; cout << str << endl; //assert(calc(str) == k); } return 0; } |