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
#include <iostream>
#include <string>
#include <cmath>

bool is_syntactic_sum(const std::string& s) {
    int depth = 0;
    for (const auto& c: s) {
        switch (c) {
            case '(': {
                ++depth;
                break;
            }
            case ')': {
                --depth;
                break;
            }
            case '+': {
                if (depth == 0) {
                    return true;
                }
            }
        }
    }
    return false;
} 
 
std::string as_ones(int n) {
    // using namespace std::string_literals; // your compilator doesn't accept this
    if (n < 6) {
        switch (n) {
            case 1: {
                return "1";
            }
            case 2: {
                return "1+1";
            }
            case 3: {
                return "1+1+1";
            }
            case 4: {
                return "1+1+1+1";
            }
            case 5: {
                return "1+1+1+1+1";
            }
        }
    } else {
        switch (n) {    
            // square optimizations
            case 9: {
                return "(1+1+1)*(1+1+1)";
            }
            case 16: {
                return "(1+1+1+1)*(1+1+1+1)";
            }
            case 25: {
                return "(1+1+1+1+1)*(1+1+1+1+1)";
            }
            case 36: {
                return "(1+1+1)*(1+1)*(1+1+1)*(1+1)";
            }
            case 49: {
                return "((1+1+1)*(1+1)+1)*((1+1+1)*(1+1)+1)";
            }
            
            // cubic optimizations ... making that lookup extensive is boring, but it could be generated
            case 27: {
                return "(1+1+1)*(1+1+1)*(1+1+1)";
            }
            case 64: {
                return "(1+1+1+1)*(1+1+1+1)*(1+1+1+1)";
            }
            case 125: {
                return "(1+1+1+1+1)*(1+1+1+1+1)*(1+1+1+1+1)";
            }
            default: { break; }
        }
    }
    
    if (n > 63) { // cubic root optimization
        double root = std::cbrt(n);
        if (root == std::floor(root)) {
            auto repr = as_ones(root);
            if (is_syntactic_sum(repr)) { 
                return std::string("(") + repr + ")*(" + repr + ")*(" + repr + ")"; 
            } else { // we can omit extra brace
                return repr + "*" + repr + "*" + repr; 
            }
        }
    }    
 
    if (n > 8) { // square root optimization
        double root = std::sqrt(n);
        if (root == std::floor(root)) {
            auto repr = as_ones(root);
            if (is_syntactic_sum(repr)) {
                return std::string("(") + repr + ")*(" + repr + ")"; 
            } else { 
                return repr + "*" + repr; 
            }
        }
    }
    
    // regular *2[+1] notation
    int halved = n / 2;
    int rest = n % 2;
    
    auto half = as_ones(halved);
    if (rest == 0) {
        if (is_syntactic_sum(half)) {
            return std::string("(") + as_ones(halved) + ")*(1+1)";            
        } else {
            return as_ones(halved) + "*(1+1)";            
        }
    } else {
        if (is_syntactic_sum(half)) {
            return std::string("(") + half + ")*(1+1)+" + as_ones(rest);
        } else {
            return half + "*(1+1)+" + as_ones(rest);
        }        
    }
}

int count_ones(const std::string& s) {
    int sum = 0;
    for (const auto& c: s) {
        if (c == '1') {
            ++sum;
        }
        if (sum > 100) break; // stop wasting time!
    }
    return sum;
}

int main() {
    std::ios::sync_with_stdio(false);
    int tests = 0;
    std::cin >> tests;
    for (int i = 0; i < tests; ++i) {
        int k = 0;
        std::cin >> k;
        auto r = as_ones(k);
        std::cout << (count_ones(r) > 100 ? "NIE" : r ) << '\n';
    }
    
}