#include<algorithm> #include<cassert> #include<cctype> #include<cmath> #include<cstdio> #include<cstdlib> #include<cstring> #include<deque> #include<sstream> #include<iostream> #include<list> #include<map> #include<queue> #include<set> #include<string> #include<vector> using namespace std; typedef long long LL; typedef long double LD; #define dprintf(...) fprintf(stderr, __VA_ARGS__) int cond = 1; #define DB(X) {if(cond){cerr<<"Line:"<<__LINE__<<", "<<#X<<" = "<<X<<endl;}} const int nm = 4*1e4; int d[nm + 6], w[nm + 6]; string r[nm + 6]; string get1(int k); string get(int k, int wi) { if (k < nm) { if (r[k] != "" || k == 0) { return r[k]; } } if (k == wi) { ostringstream ss; if (k == 1) { return r[k] = "1"; } ss << "(1"; for(int i = 0; i < k - 1; ++i) { ss << "+1"; } return r[k] = (ss.str() + ')'); } string a = get1(wi); string b = get1(k/wi); string c = get1(k % wi); if (b != "") { a = '(' + a + "*" + b + ')'; } if (c == "") { return a; } return '(' + a + "+" + c + ')'; } string get1(int k) { return get(k, w[k]); } string solve() { int k; cin >> k; int resw = k; if (k < nm) { if (d[k] > 100) { return "NIE"; } resw = w[k]; } else { int res = k; for(int j = 1; j*j <= k; ++j) { if (k/j >= nm) { continue; } int c = d[j] +d[k/j] + d[k %j]; if (c < res) { res = c; resw = j; } } if (res > 100) { return "NIE"; } } return get(k, resw); } int main() { for(int i = 0; i < nm; ++i) { d[i] = w[i] = 0; r[i] = ""; } d[1] = w[1] = 1; for(int i = 2; i < nm; ++i) { int j = 1; d[i] = w[i] = i; while (j*j <= i) { int c = (d[j] + d[i/j] + d[i % j]); if (c < d[i]) { d[i] = c; w[i] = j; } j++; } } int t; cin >> t; for(int x = 1; x <= t; ++x){ cout << solve() << endl;//result } 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 | #include<algorithm> #include<cassert> #include<cctype> #include<cmath> #include<cstdio> #include<cstdlib> #include<cstring> #include<deque> #include<sstream> #include<iostream> #include<list> #include<map> #include<queue> #include<set> #include<string> #include<vector> using namespace std; typedef long long LL; typedef long double LD; #define dprintf(...) fprintf(stderr, __VA_ARGS__) int cond = 1; #define DB(X) {if(cond){cerr<<"Line:"<<__LINE__<<", "<<#X<<" = "<<X<<endl;}} const int nm = 4*1e4; int d[nm + 6], w[nm + 6]; string r[nm + 6]; string get1(int k); string get(int k, int wi) { if (k < nm) { if (r[k] != "" || k == 0) { return r[k]; } } if (k == wi) { ostringstream ss; if (k == 1) { return r[k] = "1"; } ss << "(1"; for(int i = 0; i < k - 1; ++i) { ss << "+1"; } return r[k] = (ss.str() + ')'); } string a = get1(wi); string b = get1(k/wi); string c = get1(k % wi); if (b != "") { a = '(' + a + "*" + b + ')'; } if (c == "") { return a; } return '(' + a + "+" + c + ')'; } string get1(int k) { return get(k, w[k]); } string solve() { int k; cin >> k; int resw = k; if (k < nm) { if (d[k] > 100) { return "NIE"; } resw = w[k]; } else { int res = k; for(int j = 1; j*j <= k; ++j) { if (k/j >= nm) { continue; } int c = d[j] +d[k/j] + d[k %j]; if (c < res) { res = c; resw = j; } } if (res > 100) { return "NIE"; } } return get(k, resw); } int main() { for(int i = 0; i < nm; ++i) { d[i] = w[i] = 0; r[i] = ""; } d[1] = w[1] = 1; for(int i = 2; i < nm; ++i) { int j = 1; d[i] = w[i] = i; while (j*j <= i) { int c = (d[j] + d[i/j] + d[i % j]); if (c < d[i]) { d[i] = c; w[i] = j; } j++; } } int t; cin >> t; for(int x = 1; x <= t; ++x){ cout << solve() << endl;//result } return 0; } |