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
#include <cinttypes>
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

#define TEST 0

#if TEST
bool calcExpr(const char* expr, uint32_t& res, uint32_t& cnt)
{
	struct State
	{
		vector<uint32_t> acc;
		char op;
	};
	State curr;
	curr.op = '+';
	vector<State> stack;

	res = 0;
	cnt = 0;

	for (const char* p = expr; *p; ++p) {
		switch (*p) {
		case '(':
			stack.push_back(curr);
			curr.acc.clear();
			curr.op = '+';
			break;
		case '1':
			cnt++;
			if (curr.op == '+') {
				curr.acc.push_back(1);
			}
			break;
		case '+':
		case '*':
			curr.op = *p;
			break;
		case ')': {
			if (stack.empty()) {
				fprintf(stderr, "ERROR (%u): Unmatched ')'\n", static_cast<uint32_t>(p - expr));
				return false;
			}
			uint32_t sum = 0;
			for (uint32_t i = 0; i < curr.acc.size(); ++i) {
				sum += curr.acc[i];
			}

			curr = stack.back();
			stack.pop_back();

			if (curr.op == '+' || curr.acc.empty()) {
				curr.acc.push_back(sum);
			} else {
				curr.acc.back() *= sum;
			}
			break;
		}
		default:
			fprintf(stderr, "ERROR (%u): Invalid char: %c\n", static_cast<uint32_t>(p - expr), *p);
			return false;
		}
	}
	if (!stack.empty()) {
		fprintf(stderr, "ERROR: Unmatched '('\n");
		return false;
	}

	for (uint32_t i = 0; i < curr.acc.size(); ++i) {
		res += curr.acc[i];
	}
	return true;
}
#endif

int main(int argc, const char* argv[])
{
	uint32_t t;
	scanf("%u", &t);

	for (uint32_t i = 0; i < t; ++i) {
		uint32_t n;
		scanf("%u", &n);

		char expr[1024];
		char* p = expr;
		uint32_t parens = 0;
		uint32_t acc = n;
		if (acc == 1) {
			strcpy(p++, "1");
		} else while (acc > 1) {
			const char* part;
			if (acc % 3 == 0) {
				acc /= 3;
				if (acc == 1) {
					part = "(1+1+1)";
				} else {
					part = "(1+1+1)*";
				}
			} else if (acc % 2 == 0) {
				acc /= 2;
				if (acc == 1) {
					part = "(1+1)";
				} else {
					part = "(1+1)*";
				}
			} else {
				acc -= 1;
				if (p == expr) {
					part = "1+";
				} else {
					part = "(1+";
					parens++;
				}
			}
			strcpy(p, part);
			p += strlen(part);
		}
		while (parens--) {
			*p++ = ')';
		}
		*p = 0;
		printf("%s\n", expr);
#if TEST
		uint32_t res, cnt;
		if (calcExpr(expr, res, cnt)) {
			if (res != n) {
				fprintf(stderr, "ERROR: Expression evaluates to %u instead of %u!!!\n", res, n);
			} else if (cnt > 100) {
				fprintf(stderr, "ERROR: Expression has %u 1's!!!\n");
			} else {
				fprintf(stderr, "OK: Expression is fine\n");
			}
		}
#endif
	}
	return 0;
}