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
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * Created by stawicad on 2016-11-22.
 */
public class jed {

    public static void main(String[] args) {
        Map<Integer, String> cache = new HashMap<>();
        String[] samples = {null, "1", "1+1", "1+1+1", "1+1+1+1", "1+1+1+1+1", "1+1+1+1+1+1", "1+1+1+1+1+1+1"};
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        for (int i = 0; i < t; i++) {
            String no = toString(sc.nextInt(), cache, samples);
            System.out.println(count(no) > 100 ? "NIE" : no);
        }
    }

    private static int count(final String no) {
        int answer = 0;
        for (char c : no.toCharArray()) {
            if(c == '1') answer++;
        }
        return answer;
    }

    private static String toString(int k, final Map<Integer, String> cache, final String[] samples) {
        if(k < samples.length) return samples[k];
        if(cache.containsKey(k)) return cache.get(k);
        String answer = null;
        for (int i = 2; i * i <= k; i++) {
            if(k % i == 0) {
                String p = "(" 
                         + toString(i, cache, samples)
                         + ")*("
                         + toString(k / i, cache, samples)
                         + ")";
                if(answer == null || answer.length() > p.length()) answer = p;
            }
        }
        if(answer == null) answer = toString(k - 1, cache, samples) +"+1";
        cache.put(k, answer);
        return answer;
    }
}