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
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
using namespace std;

//************************************************************
//Test Millera-Rabina

int dividers[40];
string result;
bool last = false;


int mulmod(int a, int b, int mod) {
    int x = 0;
    int y = a % mod;

    while(b>0) {
        if(b%2 == 1) {
            x = (x+y)%mod;
        }
        y=(y*2)%mod;
        b/=2;
    }
    return x % mod;
}

/*
 * modular exponentiation
 */

int modulo(int base, int exponent, int mod) {
    long long x = 1;
    long long y = base;
    while(exponent > 0) {
        if(exponent%2 == 1)
            x = (x * y)%mod;
        y = (y * y)%mod;
        exponent = exponent/2;
    }
    return x%mod;
}

/*
 * Miller-Rabin primality test, iteration signifies the accuracy
 */


bool Miller(int p,int iteration) {
    if(p<2) {
        return false;
    }
    if (p != 2 && p % 2==0) {
        return false;
    }
    int s = p-1;

    while(s % 2 == 0) {
        s /= 2;
    }

    for(int i = 0; i < iteration; ++i) {
        int a = rand() % (p - 1) + 1, temp = s;
        int mod = modulo(a, temp, p);
        while(temp != p - 1 && mod != 1 && mod != p - 1) {
            mod = mulmod(mod, mod, p);
            temp *= 2;
        }

        if(mod != p - 1 && temp % 2 == 0) {
            return false;
        }
    }
    return true;
}

//************************************************************

void output(int number) {
    bool isPrime;
    int countDividers = 0;
    isPrime = Miller(number,12);

    if(number == 1) {
        result += "1";
        return;
    }
    if(number == 2) {
        //cout << "Mamy dwojke !!!" << endl;
        result += "(1+1)";
        //cout << "(1+1)" << endl;
        return;
    }
    if(isPrime && number != 2) {
        //cout << "Mamy liczbe pierwsza = " << number << endl;
        result += "(1+";
        //cout << "(1+" << endl;
        output(number-1);
        if(!last) {
            result += ")";
            //cout << ")" << endl;
        }

    }
    if(!isPrime) {
        int i = 2;
        bool flaga = false;
        while(i*i <= number) {

            //cout << "Mamy liczbe = " << number << endl;
            if(number%i == 0) {
                flaga = true;
                number = number/i;
                //i = 1;
                //cout << "Liczba zmniejszona = " << number << endl;

                if(Miller(i, 10)) {
                    //cout << "Rekurencyjne wywolanie i = " << i << endl;
                    output(i);
                    //i = 1;
                    if(!last) {
                        result += "*";
                        //cout << "*" << endl;
                    }
                    //break;
                }
            }

            if(flaga) {
                flaga = false;
                i = 1;
            }

           i++;
        }

        if(Miller(number,10)) {
            //cout << "Ostatnie wywolanie rekurencyjne number = " << number << endl;
            output(number);
            //result +=")";
            //cout << "(" << endl;
        }
        /*result += "(1+1)";
        //cout << "(1+1)" << endl;
        result += "*";
        //cout << "*" << endl;*/
    }
}

int main(int argc, char *argv[]) {
    int t;
    int number;

    cin >> t;
    while(t--) {
        cin >> number;
        output(number);
        cout << result << endl;
        result = "";
    }
    return 0;
}