#include <iostream> #include <sstream> #include <algorithm> #include <vector> #include <ctype.h> using namespace std; typedef unsigned long long long64; int main() { ios::sync_with_stdio(false); int tests; cin >> tests; while (tests--) { long64 number; cin >> number; stringstream stringBuffer; while (number != 0) { stringBuffer << (number % 2); number /= 2; } string binaryString = stringBuffer.str(); reverse(binaryString.begin(), binaryString.end()); vector<int> exponents; int currentExp = binaryString.size() - 1; for (int i = 0; i < binaryString.size(); i++) { char ch = binaryString[i]; if (ch == '1') exponents.push_back(currentExp); currentExp--; } sort(exponents.begin(), exponents.end()); stringBuffer.str(string()); int openBraces = 0; for (int i = 0; i < exponents.size(); i++) { int exponent = exponents[i]; if (exponent == 0) { stringBuffer << "0"; if (i != exponents.size() - 1) stringBuffer << "+"; } else { for (int j = i; j < exponents.size(); j++) exponents[j] -= exponent; stringBuffer << exponent; if (i != exponents.size() - 1) { stringBuffer << "(0+"; openBraces++; } } } stringBuffer << string(openBraces, ')'); string result = stringBuffer.str(); stringBuffer.str(string()); for (int i = 0; i < result.size(); i++) { char ch = result[i]; if (isdigit(ch)) { int exponent = ch - '0'; if (((i + 1) < result.size()) && (isdigit(result[i + 1]))) { exponent = (exponent * 10) + (result[i + 1] - '0'); i++; } if (exponent == 0) { stringBuffer << "1"; } else { stringBuffer << "(1+1)"; for (int j = 1; j < exponent; j++) stringBuffer << "*(1+1)"; } if ((i + 1 < result.size()) && (result[i + 1] == '(')) { stringBuffer << "*"; } } else { stringBuffer << ch; } } string finalResult = stringBuffer.str(); int oneCount = 0; for (int i = 0; i < finalResult.size(); i++) { char ch = finalResult[i]; if (ch == '1') oneCount++; } if (oneCount <= 100) { cout << finalResult << endl; } else { cout << "NIE" << 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 103 104 105 106 107 108 109 110 | #include <iostream> #include <sstream> #include <algorithm> #include <vector> #include <ctype.h> using namespace std; typedef unsigned long long long64; int main() { ios::sync_with_stdio(false); int tests; cin >> tests; while (tests--) { long64 number; cin >> number; stringstream stringBuffer; while (number != 0) { stringBuffer << (number % 2); number /= 2; } string binaryString = stringBuffer.str(); reverse(binaryString.begin(), binaryString.end()); vector<int> exponents; int currentExp = binaryString.size() - 1; for (int i = 0; i < binaryString.size(); i++) { char ch = binaryString[i]; if (ch == '1') exponents.push_back(currentExp); currentExp--; } sort(exponents.begin(), exponents.end()); stringBuffer.str(string()); int openBraces = 0; for (int i = 0; i < exponents.size(); i++) { int exponent = exponents[i]; if (exponent == 0) { stringBuffer << "0"; if (i != exponents.size() - 1) stringBuffer << "+"; } else { for (int j = i; j < exponents.size(); j++) exponents[j] -= exponent; stringBuffer << exponent; if (i != exponents.size() - 1) { stringBuffer << "(0+"; openBraces++; } } } stringBuffer << string(openBraces, ')'); string result = stringBuffer.str(); stringBuffer.str(string()); for (int i = 0; i < result.size(); i++) { char ch = result[i]; if (isdigit(ch)) { int exponent = ch - '0'; if (((i + 1) < result.size()) && (isdigit(result[i + 1]))) { exponent = (exponent * 10) + (result[i + 1] - '0'); i++; } if (exponent == 0) { stringBuffer << "1"; } else { stringBuffer << "(1+1)"; for (int j = 1; j < exponent; j++) stringBuffer << "*(1+1)"; } if ((i + 1 < result.size()) && (result[i + 1] == '(')) { stringBuffer << "*"; } } else { stringBuffer << ch; } } string finalResult = stringBuffer.str(); int oneCount = 0; for (int i = 0; i < finalResult.size(); i++) { char ch = finalResult[i]; if (ch == '1') oneCount++; } if (oneCount <= 100) { cout << finalResult << endl; } else { cout << "NIE" << endl; } } return 0; } |