#include <cstdio>
#include <iostream>
#include <algorithm>
#include <map>
#define DBG(X)
using namespace std;
map<int, pair<int,int>> M;
pair<int, int> ile(int n)
{
if (M.find(n) != M.end())
{
return M[n];
}
if (n <= 3)
{
return {n, n};
}
int ret1 = 2 + ile(n / 2).first + n % 2;
//int ret2 = 3 + ile(n / 3).first + n % 3;
//if (ret1 < ret2)
{
M.insert(pair<int, pair<int,int>>(n, {ret1, 2}));
return {ret1, 2};
}
//else
{
//M.insert(pair<int, pair<int,int>>(n, {ret2, 3}));
//return {ret2, 3};
}
}
void wypisz(int n, int gl)
{
if (n == 1)
{
if (gl == 0)
printf("1");
return;
}
if (n == 2)
{
if (gl == 0)
printf("1+1");
else
printf("(1+1)");
return;
}
if (n == 3)
{
if (gl == 0)
printf("1+1+1");
else
printf("(1+1+1)");
return;
}
//int ileJed = M[n].first;
//int divider = M[n].second;
//cout << "n " << n << "divider " << divider << " " << n % divider << endl;
//if (divider == 2)
{
if (gl > 0 && n % 2)
printf("(");
printf("(1+1)*");
wypisz(n / 2, gl + 1);
if (n % 2 == 1)
printf("+1");
if (gl > 0 && n % 2)
printf(")");
}
/*else
{
if (gl > 0 && n % 3)
printf("(");
printf("(1+1+1)*");
wypisz(n / 3, gl + 1);
if (n % 3 == 1)
printf("+1");
if (n % 3 == 2)
printf("+1+1");
if (gl > 0 && n % 3)
printf(")");
}*/
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int n;
scanf("%d", &n);
DBG(cout << " t " << t << " n " << n << " ile " << ile(n).first << " " << ile(n).second << endl;)
/* if (ile(n).first > 100)
printf("NIE\n");
else*/
{
wypisz(n, 0);
printf("\n");
}
}
return 0;
}
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 | #include <cstdio> #include <iostream> #include <algorithm> #include <map> #define DBG(X) using namespace std; map<int, pair<int,int>> M; pair<int, int> ile(int n) { if (M.find(n) != M.end()) { return M[n]; } if (n <= 3) { return {n, n}; } int ret1 = 2 + ile(n / 2).first + n % 2; //int ret2 = 3 + ile(n / 3).first + n % 3; //if (ret1 < ret2) { M.insert(pair<int, pair<int,int>>(n, {ret1, 2})); return {ret1, 2}; } //else { //M.insert(pair<int, pair<int,int>>(n, {ret2, 3})); //return {ret2, 3}; } } void wypisz(int n, int gl) { if (n == 1) { if (gl == 0) printf("1"); return; } if (n == 2) { if (gl == 0) printf("1+1"); else printf("(1+1)"); return; } if (n == 3) { if (gl == 0) printf("1+1+1"); else printf("(1+1+1)"); return; } //int ileJed = M[n].first; //int divider = M[n].second; //cout << "n " << n << "divider " << divider << " " << n % divider << endl; //if (divider == 2) { if (gl > 0 && n % 2) printf("("); printf("(1+1)*"); wypisz(n / 2, gl + 1); if (n % 2 == 1) printf("+1"); if (gl > 0 && n % 2) printf(")"); } /*else { if (gl > 0 && n % 3) printf("("); printf("(1+1+1)*"); wypisz(n / 3, gl + 1); if (n % 3 == 1) printf("+1"); if (n % 3 == 2) printf("+1+1"); if (gl > 0 && n % 3) printf(")"); }*/ } int main() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); DBG(cout << " t " << t << " n " << n << " ile " << ile(n).first << " " << ile(n).second << endl;) /* if (ile(n).first > 100) printf("NIE\n"); else*/ { wypisz(n, 0); printf("\n"); } } return 0; } |
English