// ofhui, blef jednak xD #include<cstdio> #include<vector> #include<algorithm> #include<set> #include<iostream> #include<map> using namespace std; #define REP(i,n) for(int i=0; i<(int)n; i++) #define ST first #define ND second #define MP make_pair #define PB push_back typedef vector<int> VI; typedef pair<int,int> PI; typedef vector<PI> VPII; typedef set<int> SI; typedef long long LL; #ifdef DEBUG const bool debug = true; #else const bool debug = false; #endif LL n,m,k,l, test = 1; const int INF = 1000 * 1000 * 1000 ; const int MAKSN = 1000 * 1000 + 13; // UZUPElnic const int MAX_ONES = 100; const int POWS_CNT = 23; LL pows[30][2]; void readIn() { cin >> n; } string nr(int num) { if(num == 1) return "1"; string res = "("; for(int i=0; i < num; i++) { res += "1"; i < num - 1 ? res += "+" : res += ")"; } return res; } string pot(int a, int wykl) { string res = "";//= "("; string num = nr(a); while(wykl) { res += num; if(--wykl) res += "*"; } // res += ")"; return res; } void precalc() { pows[0][0] = 1; pows[0][1] = 2; for(int i=1; i < POWS_CNT; i++) { REP(j, 2) { pows[i][j] = pows[i-1][j]*3; } } } vector<pair<LL, PI> > v; void findBiggest() { LL biggest = 0; int pot = 0; int mn = 0; REP(j, 2) { REP(i, POWS_CNT){ if(pows[i][j] <= n){ if(pows[i][j] >= biggest){ biggest = pows[i][j]; mn = j+1; pot = i; } } } } v.PB(MP(biggest, MP(pot, mn))); n -= biggest; } void rozloz() { while(n) findBiggest(); reverse(v.begin(), v.end()); } string turnToString() { string res = ""; int last_pot = 0; int ilosc_naw = 0; REP(i, v.size()) { int cur_pot = v[i].ND.ST; int cur_mn = v[i].ND.ND; string potega = pot(3, cur_pot-last_pot); string numer = nr(cur_mn); if(potega != "") { if(i == (int)v.size() - 1) if(cur_mn == 1) res += potega; else { res += "(" + potega + "*" + numer; ilosc_naw += 1; } else { res += "(" + potega + "*(" + numer; ilosc_naw +=2; } } else res += numer; i < int(v.size()-1) ? res+= "+" : res; last_pot = cur_pot; } while(ilosc_naw) { res += ")"; ilosc_naw--; } return res; } int calcOnes(string s) { int res = 0; REP(i, s.size()) if (s[i] == '1') res++; return res; } void solve() { rozloz(); string s = turnToString(); int cnt = calcOnes(s); if(cnt > MAX_ONES) cout << "NIE\n"; else cout << s <<"\n"; } void zeruj() { v.clear(); } int main() { ios_base::sync_with_stdio(0); precalc(); cin >> test; REP(i, test) { zeruj(); readIn(); solve(); } 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 176 177 178 179 180 181 182 183 184 185 186 | // ofhui, blef jednak xD #include<cstdio> #include<vector> #include<algorithm> #include<set> #include<iostream> #include<map> using namespace std; #define REP(i,n) for(int i=0; i<(int)n; i++) #define ST first #define ND second #define MP make_pair #define PB push_back typedef vector<int> VI; typedef pair<int,int> PI; typedef vector<PI> VPII; typedef set<int> SI; typedef long long LL; #ifdef DEBUG const bool debug = true; #else const bool debug = false; #endif LL n,m,k,l, test = 1; const int INF = 1000 * 1000 * 1000 ; const int MAKSN = 1000 * 1000 + 13; // UZUPElnic const int MAX_ONES = 100; const int POWS_CNT = 23; LL pows[30][2]; void readIn() { cin >> n; } string nr(int num) { if(num == 1) return "1"; string res = "("; for(int i=0; i < num; i++) { res += "1"; i < num - 1 ? res += "+" : res += ")"; } return res; } string pot(int a, int wykl) { string res = "";//= "("; string num = nr(a); while(wykl) { res += num; if(--wykl) res += "*"; } // res += ")"; return res; } void precalc() { pows[0][0] = 1; pows[0][1] = 2; for(int i=1; i < POWS_CNT; i++) { REP(j, 2) { pows[i][j] = pows[i-1][j]*3; } } } vector<pair<LL, PI> > v; void findBiggest() { LL biggest = 0; int pot = 0; int mn = 0; REP(j, 2) { REP(i, POWS_CNT){ if(pows[i][j] <= n){ if(pows[i][j] >= biggest){ biggest = pows[i][j]; mn = j+1; pot = i; } } } } v.PB(MP(biggest, MP(pot, mn))); n -= biggest; } void rozloz() { while(n) findBiggest(); reverse(v.begin(), v.end()); } string turnToString() { string res = ""; int last_pot = 0; int ilosc_naw = 0; REP(i, v.size()) { int cur_pot = v[i].ND.ST; int cur_mn = v[i].ND.ND; string potega = pot(3, cur_pot-last_pot); string numer = nr(cur_mn); if(potega != "") { if(i == (int)v.size() - 1) if(cur_mn == 1) res += potega; else { res += "(" + potega + "*" + numer; ilosc_naw += 1; } else { res += "(" + potega + "*(" + numer; ilosc_naw +=2; } } else res += numer; i < int(v.size()-1) ? res+= "+" : res; last_pot = cur_pot; } while(ilosc_naw) { res += ")"; ilosc_naw--; } return res; } int calcOnes(string s) { int res = 0; REP(i, s.size()) if (s[i] == '1') res++; return res; } void solve() { rozloz(); string s = turnToString(); int cnt = calcOnes(s); if(cnt > MAX_ONES) cout << "NIE\n"; else cout << s <<"\n"; } void zeruj() { v.clear(); } int main() { ios_base::sync_with_stdio(0); precalc(); cin >> test; REP(i, test) { zeruj(); readIn(); solve(); } return 0; } |