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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.LinkedList;

/**
 * Created by michal on 22.11.16.
 */
public class jed {

    static class ResultPair {
        int sum1;
        LinkedList<String> expressions;
        ResultPair(int sum1, LinkedList<String> exp) {
            this.sum1 = sum1;
            this.expressions = exp;
        }
    }

    private static int fun(int x, int c, int n) {
        return (x * x + c) % n;
    }

    private static int gcd(int x, int y) {
        int r;
        while (y != 0) {
            r = x % y;
            x = y;
            y = r;
        }
        return x;
    }

    private static int factorize(int n, int c) {

        int d = 1;
        int x = 2;
        int y = 2;

        while (d == 1) {
            x = fun(x, c, n);
            y = fun(fun(y, c, n), c, n);
            d = gcd(Math.abs(x - y), n);

        }
        if (d == n) {
            return -1;
        } else {
            return d;
        }
    }

    private static boolean isProbablyPrime(int x, int k) {
        if (x == 1) {
            return false;
        }
        if (x == 2) {
            return true;
        }
        if (x % 2 == 0) {
            return false;
        }
        --x;
        int s = 1;
        int p2 = 2;
        while (x % p2 == 0) {
            p2 <<= 1;
            ++s;
        }
        --s;
        p2 >>= 1;
        int d = x / p2;
        int a;
        for (int i=0; i < k; ++i) {
            a = 1 + (int)(Math.random() * x);
            if((int)(Math.pow(a, d)) % x != 1 ) {
                for (int j = 0; j < s; ++j) {
                    if((int)(d*Math.pow(2, j)) % x == -1 ) {
                        return false;
                    }
                }
            } else {
                return true;
            }
        }
        return false;
    }

    public static void main(String args[]) throws IOException {
        BufferedReader bi = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(bi.readLine());
        for(int i=0; i < t; ++i) {
            ResultPair result = doit(Integer.parseInt(bi.readLine()));
            StringBuilder stringBuilder = new StringBuilder();
            if(result.sum1 > 100) {
                System.out.println("NIE");
                continue;
            }
            Iterator<String> it = result.expressions.iterator();
            String s;
            while (it.hasNext()) {
                s = it.next();
                if (s.isEmpty() && it.hasNext()) {
                    s = "*";
                }
                stringBuilder.append(s);
            }
            System.out.println(stringBuilder.toString().replace(")(",")*("));
        }
    }

    private static ResultPair doit(int n) {
        int orig_n = n;
        LinkedList<String> factorsStr = new LinkedList<>();
        if (n == 1) {
            factorsStr.add("1");
            return new ResultPair(1, factorsStr);
        }
        if (n == 2) {
            factorsStr.add("(1+1)");
            return new ResultPair(2, factorsStr);
        }
        if (n == 3) {
            factorsStr.add("(1+1+1)");
            return new ResultPair(3, factorsStr);
        }
        if (isProbablyPrime(n, 1000)) {
            ResultPair resultPair = doit(n - 1);
            ++resultPair.sum1;
            resultPair.expressions.addFirst("(1+");
            String tmp = resultPair.expressions.getLast();
            if ("".equals(tmp)) {
                resultPair.expressions.set(resultPair.expressions.size() - 1, ")");
            } else {
                resultPair.expressions.addLast(")");
            }
            return resultPair;
        }
        LinkedList<Integer> factors = new LinkedList<>();
        int factors_sum = 0;
        int factor;
        int max_factor = 0;
        int max_factor_i = 0;
        int fi = 0;
        int c = 31607;
        while (n != 0) {
            //c = 2 + (int)(Math.random() * 19);
            factor = factorize(n, c);
            if(factor > 0) {
                factors.add(factor);
                factors_sum += factor;
                if (factor > max_factor) {
                    max_factor = factor;
                    max_factor_i = fi;
                }
                n = n / factor;
            } else {
                factors.add(n);
                factors_sum += n;
                if (n > max_factor) {
                    max_factor = n;
                    max_factor_i = fi;
                }
                n = 0;
            }
            ++fi;
        }

        Iterator<Integer> it = factors.iterator();
        int f;
        int i = 0;
        int sum1 = 0;
        while (it.hasNext()) {
            f = it.next();
            if (factors_sum <= 100 && i != max_factor_i) {
                for (int j = 1; j <= f; ++j) {
                    factorsStr.add((j == 1 ? "(" : "") + "1" + (j == f ? ")" : "+"));
                }
                factorsStr.add(it.hasNext() ? "*" : "");
                sum1 += f;
            } else if (f != orig_n){
                ResultPair tmp = doit(f);
                factorsStr.addAll(tmp.expressions);
                sum1 += tmp.sum1;
            } else {
                //
                ResultPair resultPair = doit(f - 1);
                ++resultPair.sum1;
                resultPair.expressions.addFirst("(1+");
                String tmp = resultPair.expressions.getLast();
                if ("".equals(tmp)) {
                    resultPair.expressions.set(resultPair.expressions.size() - 1, ")");
                } else {
                    resultPair.expressions.addLast(")");
                }
                return resultPair;
                //
            }
            ++i;
        }
        return new ResultPair(sum1, factorsStr);
    }
}