#include<cstdio> #include<cmath> #include<iostream> #include<algorithm> #include <sstream> #include<vector> using namespace std; string binary(int number) { string result = ""; do { if ((number & 1) == 0) result += "0"; else result += "1"; number >>= 1; } while (number); reverse(result.begin(), result.end()); return result; } vector<int> designateExp(string bin) { vector<int> e; long long x = bin.size()-1; for(int i = 0; i < bin.size(); i++) { char c = bin[i]; if (c == '1'); e.push_back(x); x--; } sort(e.begin(), e.end()); return e; } int main() { ios::sync_with_stdio(0); int t; scanf("%d", &t); while (t--) { vector<long long> ex; long long number; scanf("%lld", &number); string bin = binary(number); int o = bin.size() - 1; int u = 0; while(u < bin.size()){ char r = bin[u]; if (r == '1') ex.push_back(o); o--; u++; } sort(ex.begin(), ex.end()); stringstream e; int b = 0; int g = 0; while(g < ex.size()) { int c = ex[g]; if (c == 0) { e << "0"; if (g != ex.size() - 1) e << "+"; } else { for (int j = g; j < ex.size(); j++) ex[j] -= c; e << c; if (g != ex.size() - 1) { e << "(0+"; b++; }} g++;} for(int z = 0; z < b; z++) e << ')'; string eq = e.str(); e.str(string()); int p = 0; while(p < eq.size()) { char X = eq[p]; if (isdigit(X)) { int wyk = X - '0'; if (isdigit(eq[p + 1])) { wyk *= 10; wyk += eq[p + 1] - '0'; p++; } if (wyk == 0) e << "1"; else { e << "(1+1)"; for (int j = 1; j < wyk; j++) e<< "*(1+1)"; } if (eq[p + 1] == '(') { e << "*";}} else e << X; p++; } cout << e.str() << endl; } }
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 | #include<cstdio> #include<cmath> #include<iostream> #include<algorithm> #include <sstream> #include<vector> using namespace std; string binary(int number) { string result = ""; do { if ((number & 1) == 0) result += "0"; else result += "1"; number >>= 1; } while (number); reverse(result.begin(), result.end()); return result; } vector<int> designateExp(string bin) { vector<int> e; long long x = bin.size()-1; for(int i = 0; i < bin.size(); i++) { char c = bin[i]; if (c == '1'); e.push_back(x); x--; } sort(e.begin(), e.end()); return e; } int main() { ios::sync_with_stdio(0); int t; scanf("%d", &t); while (t--) { vector<long long> ex; long long number; scanf("%lld", &number); string bin = binary(number); int o = bin.size() - 1; int u = 0; while(u < bin.size()){ char r = bin[u]; if (r == '1') ex.push_back(o); o--; u++; } sort(ex.begin(), ex.end()); stringstream e; int b = 0; int g = 0; while(g < ex.size()) { int c = ex[g]; if (c == 0) { e << "0"; if (g != ex.size() - 1) e << "+"; } else { for (int j = g; j < ex.size(); j++) ex[j] -= c; e << c; if (g != ex.size() - 1) { e << "(0+"; b++; }} g++;} for(int z = 0; z < b; z++) e << ')'; string eq = e.str(); e.str(string()); int p = 0; while(p < eq.size()) { char X = eq[p]; if (isdigit(X)) { int wyk = X - '0'; if (isdigit(eq[p + 1])) { wyk *= 10; wyk += eq[p + 1] - '0'; p++; } if (wyk == 0) e << "1"; else { e << "(1+1)"; for (int j = 1; j < wyk; j++) e<< "*(1+1)"; } if (eq[p + 1] == '(') { e << "*";}} else e << X; p++; } cout << e.str() << endl; } } |