// Karol Kosinski #include <cstdio> #define FR(i,a,b) for(int i=(a);i<(b);++i) //#define DEBUG(x...) printf(x) #define DEBUG(x...) using namespace std; const int NX = 100003, BX = 503; int Num[NX], Back[NX][2], bi, pars; char Buffer[BX], Sign[NX]; void preproc() { Num[1] = 1; FR(i,2,NX) { Num[i] = Num[i-1] + 1; Back[i][0] = 1; Back[i][1] = i-1; Sign[i] = '+'; for (int j=2;j*j<=i;++j) { if (i % j == 0) { int val = Num[j] + Num[i/j]; if (Num[i] > val) { Num[i] = val; Back[i][0] = j; Back[i][1] = i/j; Sign[i] = '*'; } } } //DEBUG("[%d] =%5d\n", i, Num[i]); } } void add_to_buffer(int n) { if (n == 1) { Buffer[bi++] = '1'; return; } Buffer[bi++] = '('; add_to_buffer(Back[n][0]); Buffer[bi++] = Sign[n]; add_to_buffer(Back[n][1]); Buffer[bi++] = ')'; } int div(int &n) { for (int j=2;j*j<=n;++j) if (n % j == 0) { Buffer[bi++] = '('; ++pars; DEBUG("( %d / %d )\n", n, j); add_to_buffer(j); Buffer[bi++] = '*'; n /= j; return Num[j]; } return 0; } int run(int n) { bi = pars = 0; FR(i,0,BX) Buffer[i] = 0; int res = 0; while (n >= NX) { int n0 = n; res += div(n); if (n0 == n) { Buffer[bi++] = '('; ++pars; DEBUG("( %d - 1 )\n", n); add_to_buffer(1); Buffer[bi++] = '+'; --n; res += div(n) + 1; } } DEBUG("( %d )\n", n); add_to_buffer(n); FR(i,0,pars) Buffer[bi++] = ')'; return res + Num[n]; } void testcase() { int k; scanf("%d", &k); int res = run(k); //printf("%d\n", res); printf("%s\n", Buffer); } int main() { preproc(); int z; scanf("%d", &z); while (z--) testcase(); 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 | // Karol Kosinski #include <cstdio> #define FR(i,a,b) for(int i=(a);i<(b);++i) //#define DEBUG(x...) printf(x) #define DEBUG(x...) using namespace std; const int NX = 100003, BX = 503; int Num[NX], Back[NX][2], bi, pars; char Buffer[BX], Sign[NX]; void preproc() { Num[1] = 1; FR(i,2,NX) { Num[i] = Num[i-1] + 1; Back[i][0] = 1; Back[i][1] = i-1; Sign[i] = '+'; for (int j=2;j*j<=i;++j) { if (i % j == 0) { int val = Num[j] + Num[i/j]; if (Num[i] > val) { Num[i] = val; Back[i][0] = j; Back[i][1] = i/j; Sign[i] = '*'; } } } //DEBUG("[%d] =%5d\n", i, Num[i]); } } void add_to_buffer(int n) { if (n == 1) { Buffer[bi++] = '1'; return; } Buffer[bi++] = '('; add_to_buffer(Back[n][0]); Buffer[bi++] = Sign[n]; add_to_buffer(Back[n][1]); Buffer[bi++] = ')'; } int div(int &n) { for (int j=2;j*j<=n;++j) if (n % j == 0) { Buffer[bi++] = '('; ++pars; DEBUG("( %d / %d )\n", n, j); add_to_buffer(j); Buffer[bi++] = '*'; n /= j; return Num[j]; } return 0; } int run(int n) { bi = pars = 0; FR(i,0,BX) Buffer[i] = 0; int res = 0; while (n >= NX) { int n0 = n; res += div(n); if (n0 == n) { Buffer[bi++] = '('; ++pars; DEBUG("( %d - 1 )\n", n); add_to_buffer(1); Buffer[bi++] = '+'; --n; res += div(n) + 1; } } DEBUG("( %d )\n", n); add_to_buffer(n); FR(i,0,pars) Buffer[bi++] = ')'; return res + Num[n]; } void testcase() { int k; scanf("%d", &k); int res = run(k); //printf("%d\n", res); printf("%s\n", Buffer); } int main() { preproc(); int z; scanf("%d", &z); while (z--) testcase(); return 0; } |