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
#include <iostream>
#include <cmath>
#include <climits>
#include <stdlib.h>
#include <sstream>
#include <strings.h>
#include <algorithm>

using namespace std;

int potegi[51][30]={0};

string kody [36] = {
"0",
"1",
"(1+1)",
"(1+1+1)",
"(1+1+1+1)"
};

void set_codes()
{
    kody[5] = "(" + kody[2] + "*" + kody[2] + "+1)";
    kody[6] = "(" + kody[2] + "*" + kody[3] + ")";
    kody[7] = "(" + kody[2] + "*" + kody[3] + "+1)";
    kody[8] = "(" + kody[4] + "*" + kody[2] + ")";
    kody[9] = "(" + kody[4] + "*" + kody[2] + "+1)";
    kody[10] = "(" + kody[4] + "*" + kody[2] + "+1+1)";
    kody[11] = "(" + kody[4] + "*" + kody[2] + "+1+1+1)";
    kody[12] = "(" + kody[3] + "*" + kody[4] + ")";
    kody[13] = "(" + kody[3] + "*" + kody[4] + "+1)";
    kody[14] = "(" + kody[3] + "*" + kody[4] + "+1+1)";
    kody[15] = "(" + kody[3] + "*" + kody[5] + ")";
    kody[16] = "(" + kody[4] + "*" + kody[4] + ")";
    kody[17] = "(" + kody[4] + "*" + kody[4] + "+1)";
    kody[18] = "(" + kody[4] + "*" + kody[4] + "+1+1)";
    kody[19] = "(" + kody[4] + "*" + kody[4] + "+1+1+1)";
    kody[20] = "(" + kody[4] + "*" + kody[5] + ")";
    kody[21] = "(" + kody[7] + "*" + kody[3] + ")";
    kody[22] = "(" + kody[7] + "*" + kody[3] + "+1)";
    kody[23] = "(" + kody[7] + "*" + kody[3] + "+1+1)";
    kody[24] = "(" + kody[6] + "*" + kody[4] + ")";
    kody[25] = "(" + kody[5] + "*" + kody[5] + ")";
    kody[26] = "(" + kody[5] + "*" + kody[5] + "+1)";
    kody[27] = "(" + kody[3] + "*" + kody[9] + ")";
    kody[28] = "(" + kody[3] + "*" + kody[9] + "+1)";
    kody[29] = "(" + kody[3] + "*" + kody[9] + "+1+1)";
    kody[30] = "(" + kody[3] + "*" + kody[10] + ")";
    kody[31] = "(" + kody[3] + "*" + kody[10] + "+1)";
    kody[32] = "(" + kody[3] + "*" + kody[10] + "+1+1)";
    kody[33] = "(" + kody[3] + "*" + kody[11] + ")";
    kody[34] = "(" + kody[3] + "*" + kody[11] + "+1)";
    kody[35] = "(" + kody[3] + "*" + kody[11] + "+1+1)";

    return;
}

std::pair<int, int> find_nearest(int k)
{
    int diff = INT_MAX;
    //int nearest = INT_MAX;
    int nearest_base, nearest_pow = 0;
    for (int i=0;i<35;i++)
    {
        for (int j=0;j<30;j++)
        {
            if ((k-potegi[i][j])<diff && (k-potegi[i][j]) >0)
            {
                diff = k-potegi[i][j];
                //nearest = potegi[i][j];
                nearest_base = i;
                nearest_pow = j;
            }
        }
    }
    return std::make_pair(nearest_base, nearest_pow);
}


void print_ones_expression(int wejscie)
{
    int rest=wejscie;
    int curr_val=0;
    string wynik_num = "";
    string wynik = "";
    int loop = 0;

    while (rest > 5)
    {
        std::pair<int, int> nearest_info = find_nearest(rest);
        curr_val = curr_val+pow(nearest_info.first,nearest_info.second);
        //cout << nearest_info.first << "^" << nearest_info.second << "+";
        rest = wejscie - curr_val;
        //cout << " rest:" << rest << " curr_val:" << curr_val << endl;
        for (int pot=0;pot<nearest_info.second;pot++)
        {
            if (pot!=0) wynik = wynik + "*";
            if (pot!=0) wynik_num = wynik_num + "*";
            stringstream ss;
            ss << nearest_info.first;
            wynik_num = wynik_num + ss.str();
            wynik = wynik + kody[nearest_info.first];
        }
        wynik = wynik + "+";
        wynik_num = wynik_num + "+";
        loop++;
        if (loop>10)
        {
            cout <<"NIE"<<endl;
            return;
        }
    }

    //dopisanie reszty , czyli "+1"
    if (rest != 0)
    {
        for (int r=0;r<rest;r++)
        {
            wynik = wynik + "1+";
            wynik_num = wynik_num + "1+";
        }
    }
    //obcinamy nadmiarowy "+" z konca
    wynik = wynik.substr(0,wynik.length()-1);
    wynik_num = wynik_num.substr(0,wynik_num.length()-1);

    /*for (int k=0;k<26;k++)
    /{
        cout << k <<" = "<<kody[k] << endl;
    }*/

    //zliczenie jedynek
    int ones_count = std::count(wynik.begin(), wynik.end(), '1');

    if (ones_count<101)
    {
        //cout << "wynik_num:" << wynik_num << endl;
        cout << wynik << endl;
        //cout << "jedynek:" << ones_count << endl;
    }else
    {
        cout <<"NIE"<<endl;
    }
    return;
}

int main()
{
    ios_base::sync_with_stdio(0);
    long potega=0;
    set_codes();

    //stworzenie tablicy poteg kolejnych liczb
    for (int i=2;i<51;i++)
    {
        potega=0;
        for (int j=1;potega<pow(10,9) && potega >= 0;j++)
        {
            potega=pow(i,j);
            potegi[i][j]=potega;
        }
    }

    //przeczyszczenie tablicy ze zbednych elem
    for (int i=0;i<51;i++)
    {
        //cout << i << ":";
        for (int j=0;j<30;j++)
        {
            if (potegi[i][j] < 0 || potegi[i][j] > pow(10,9)) potegi[i][j] = 0;
            //cout << potegi[i][j] << ",";
        }
        //cout <<endl;
    }

    //int wejscie=99;
    int n;
    cin >> n;
    while (cin >> n)
    {
        print_ones_expression(n);
    }
    return 0;
}