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
//
//  main.cpp
//  Potyczki 2016 jedynki
//
//  Created by Sebastian Skalacki on 23.11.2016.
//  Copyright © 2016 Sebastian Skalacki. All rights reserved.
//

#include <iostream>

using namespace std;

struct Step {
	static const char* COMPONENT[];
	char oper;
	const char* component;
};

class Runner {
	const int MAX_LENGTH = 100;
	int k, length;
	string result;
	string prefix;

	inline bool empty() { return result.empty(); }
	inline void check_too_long();
	inline bool try_times(int, Step& step);
	inline bool try_add_1(Step& step);
	void recur();

public:
	void go();
};

void Runner::go() {
	cin >> k;
	length = 0;
	result.clear();
	result.reserve(2000);
	prefix.clear();
	prefix.reserve(200);

	try {
		recur();
		cout << prefix << result;
	} catch(...) {
		cout << "NIE";
	}
	cout << endl;
}

void Runner::recur() {
	Step step;

	try_times(2, step) || try_times(3, step) ||
			try_times(5, step) || try_add_1(step);

	if (result.empty()) {
		result += step.component;
	} else if (step.oper == '*') {
		prefix += "(";
		result += ")";
		result += step.oper;
		result += "(";
		result += step.component;
		result += ")";
	} else {
		result += step.oper;
		result += step.component;
	}
}

bool Runner::try_times(int n, Step &step) {
	if (k % n){ return false; }

	const char* component = Step::COMPONENT[n];

	k /= n;
	length += n;

	check_too_long();

	if (k > 1) {
		recur();
	}

	step.oper = '*';
	step.component = component;
	return true;
}

bool Runner::try_add_1(Step &step) {
	k -= 1;
	length += 1;

	check_too_long();

	if (k > 1) {
		recur();
	}

	step.oper = '+';
	step.component = "1";
	return true;
}

void Runner::check_too_long() {
	if (length > MAX_LENGTH) { throw "too long"; }
}

const char* Step::COMPONENT[] = {
	"", "1", "1+1", "1+1+1", "1+1+1+1", "1+1+1+1+1"
};

int main(int argc, const char **argv) {

	int t;
	Runner runner;

	for (cin >> t ; t > 0 ; t--) {
		runner.go();
	}

	return 0;
}