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
#include <iostream>
#include <cstring>
#include <stdexcept>
#include <cassert>

class BinExpr
{
    const unsigned int _bufferSize;
    char *_buffer;

    char *_dataBegin;
    char *_dataEnd;
    unsigned int _value;
    unsigned int _units;
    bool _hasPlus;

    void _prepend(char c)
    {
        if (_buffer == _dataBegin)
            throw std::overflow_error("No space at beginning of buffer");
        *_dataBegin = c;
        --_dataBegin;
    }

    void _append(char c)
    {
        if (_buffer + _bufferSize - 1 == _dataEnd)
            throw std::overflow_error("No space at end of buffer");
        *_dataEnd = c;
        ++_dataEnd;
    }

public:
    BinExpr(unsigned int buffer_size):
        _bufferSize(buffer_size)
    {
        _buffer = new char [buffer_size];
        clear();
    }

    ~BinExpr()
    {
        delete [] _buffer;
    }

    BinExpr(const BinExpr &other) = delete;

    void clear()
    {
        std::memset(_buffer, 0, _bufferSize);
        _dataBegin = _buffer + _bufferSize / 2;
        _dataEnd = _dataBegin + 1;
        _hasPlus = false;
        _units = 0;
        _value = 0;
    }

    const char *data() const
    {
        return _dataBegin + 1;
    }

    void inc()
    {
        if (_dataBegin + 1 == _dataEnd) {
            _append('1');
        } else {
            _append('+');
            _append('1');
            _hasPlus = true;
        }
        _value += 1;
        _units += 1;
    }

    void dbl()
    {
        if (_value == 0)
            return;
        if (_hasPlus) {
            _prepend('(');
            _append(')');
            _hasPlus = false;
        }
        _prepend('*');
        _prepend(')');
        _prepend('1');
        _prepend('+');
        _prepend('1');
        _prepend('(');
        _units += 2;
        _value *= 2;
    }

    unsigned int countUnits() const
    {
        return _units;
    }

    unsigned int value() const
    {
        return _value;
    }
};


void BuildValueExpression(const unsigned int value, BinExpr &buffer)
{
    buffer.clear();
    for (unsigned int mask = 1 << (sizeof(value) * 8 - 1); mask != 0; mask >>= 1) {
        buffer.dbl();
        if ((mask & value) != 0)
            buffer.inc();
    }

}


int main()
{
    BinExpr buffer(1024);

    unsigned int t;
    std::cin >> t;
    for (unsigned int i = 0; i < t; ++i) {
        unsigned int ki;
        std::cin >> ki;
        BuildValueExpression(ki, buffer);
        if (buffer.value() == ki && buffer.countUnits() <= 100)
            std::cout << buffer.data() << "\n";
        else
            std::cout << "NIE\n";
    }

    return 0;
}