#include <cstdio> #include <cstdlib> #include <climits> #include <cmath> #include <vector> using namespace std; #if 0 #define dbg(...) fprintf(stderr, __VA_ARGS__) #define out(...) #else #define dbg(...) #define out(...) printf(__VA_ARGS__) #endif typedef vector<int> vi_t; static long long ARRAY[] = { 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000 }; static void prn1(const char* prefix, int x, const char* suffix) { out(prefix); for (int i = 1; i < x; ++i) { out("1+"); } out("1"); out(suffix); } static void addSep(vi_t& seps, char sep) { seps.push_back(sep); dbg("%c", sep); } static void addItem(vi_t& items, int item, int& sum) { dbg("%d", item); items.push_back(item); switch (item) { case 1: case 2: case 3: case 5: sum += item; break; case 4: sum += 4; break; case 6: sum += 5; break; case 7: sum += 6; break; case 8: sum += 6; break; case 9: sum += 6; break; case 10: sum += 7; break; } } static void fix(const char* prefix, int item, const char* suffix) { switch (item) { case 1: case 2: case 3: case 5: prn1(prefix, item, suffix); return; case 7: out(prefix); prn1("(", 2, ")*"); prn1("(", 3, ")+"); prn1("", 1, ""); out(suffix); return; case 4: out(prefix); prn1("(", 2, ")*"); prn1("(", 2, ")"); out(suffix); return; case 6: out(prefix); prn1("(", 2, ")*"); prn1("(", 3, ")"); out(suffix); return; case 8: out(prefix); prn1("(", 2, ")*"); prn1("(", 4, ")"); out(suffix); return; case 9: out(prefix); prn1("(", 3, ")*"); prn1("(", 3, ")"); out(suffix); return; case 10: out(prefix); prn1("(", 3, ")*"); prn1("(", 3, ")+"); prn1("", 1, ""); out(suffix); return; } } static int exe(long long k, int& sum, vi_t& items, vi_t& seps) { if (sum > 0) { addSep(seps, '+'); } // replace with binary search for (int i = 1; i < sizeof(ARRAY) / sizeof(int); ++i) { if (ARRAY[i - 1] <= k && k < ARRAY[i]) { int a = ARRAY[i - 1]; int p = k / ARRAY[i - 1]; if (p > 1) { addItem(items, k / ARRAY[i - 1], sum); addSep(seps, (a > 1 ? '*' : '+')); } while (a > 1) { addItem(items, 10, sum); a /= 10; if (a > 1) { addSep(seps, '*'); } } int r = k % ARRAY[i - 1]; if (a > 0) { if (r > 0 && r < 10) { addSep(seps, '+'); addItem(items, r, sum); } } dbg("\n"); return r >= 10 ? r : 0; } } return -1; } static void doExe(int k) { vi_t items; vi_t seps; bool add = true; int r = k; int sum = 0; while (r != 0) { r = exe(r, sum, items, seps); if (r == -1) { break; } if (sum > 100) { out("NIE\n"); return; } } int size = items.size(); int ssize = seps.size(); for (int i = 0; i < size; ++i) { dbg("%d", items[i]); if (items[i] == 1) { prn1("", items[i], ""); } else { fix("(", items[i], ")"); } if (i < ssize) { dbg("%c", seps[i]); out("%c", seps[i]); } } out("\n"); dbg("\n<<<END\n"); } int main() { int t; scanf("%d", &t); for (int i = 0; i < t; ++i) { int k, a, b, r = 0; scanf("%d", &k); if (k <= 100) { prn1("", k, "\n"); } else { doExe(k); } } 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 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 | #include <cstdio> #include <cstdlib> #include <climits> #include <cmath> #include <vector> using namespace std; #if 0 #define dbg(...) fprintf(stderr, __VA_ARGS__) #define out(...) #else #define dbg(...) #define out(...) printf(__VA_ARGS__) #endif typedef vector<int> vi_t; static long long ARRAY[] = { 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000 }; static void prn1(const char* prefix, int x, const char* suffix) { out(prefix); for (int i = 1; i < x; ++i) { out("1+"); } out("1"); out(suffix); } static void addSep(vi_t& seps, char sep) { seps.push_back(sep); dbg("%c", sep); } static void addItem(vi_t& items, int item, int& sum) { dbg("%d", item); items.push_back(item); switch (item) { case 1: case 2: case 3: case 5: sum += item; break; case 4: sum += 4; break; case 6: sum += 5; break; case 7: sum += 6; break; case 8: sum += 6; break; case 9: sum += 6; break; case 10: sum += 7; break; } } static void fix(const char* prefix, int item, const char* suffix) { switch (item) { case 1: case 2: case 3: case 5: prn1(prefix, item, suffix); return; case 7: out(prefix); prn1("(", 2, ")*"); prn1("(", 3, ")+"); prn1("", 1, ""); out(suffix); return; case 4: out(prefix); prn1("(", 2, ")*"); prn1("(", 2, ")"); out(suffix); return; case 6: out(prefix); prn1("(", 2, ")*"); prn1("(", 3, ")"); out(suffix); return; case 8: out(prefix); prn1("(", 2, ")*"); prn1("(", 4, ")"); out(suffix); return; case 9: out(prefix); prn1("(", 3, ")*"); prn1("(", 3, ")"); out(suffix); return; case 10: out(prefix); prn1("(", 3, ")*"); prn1("(", 3, ")+"); prn1("", 1, ""); out(suffix); return; } } static int exe(long long k, int& sum, vi_t& items, vi_t& seps) { if (sum > 0) { addSep(seps, '+'); } // replace with binary search for (int i = 1; i < sizeof(ARRAY) / sizeof(int); ++i) { if (ARRAY[i - 1] <= k && k < ARRAY[i]) { int a = ARRAY[i - 1]; int p = k / ARRAY[i - 1]; if (p > 1) { addItem(items, k / ARRAY[i - 1], sum); addSep(seps, (a > 1 ? '*' : '+')); } while (a > 1) { addItem(items, 10, sum); a /= 10; if (a > 1) { addSep(seps, '*'); } } int r = k % ARRAY[i - 1]; if (a > 0) { if (r > 0 && r < 10) { addSep(seps, '+'); addItem(items, r, sum); } } dbg("\n"); return r >= 10 ? r : 0; } } return -1; } static void doExe(int k) { vi_t items; vi_t seps; bool add = true; int r = k; int sum = 0; while (r != 0) { r = exe(r, sum, items, seps); if (r == -1) { break; } if (sum > 100) { out("NIE\n"); return; } } int size = items.size(); int ssize = seps.size(); for (int i = 0; i < size; ++i) { dbg("%d", items[i]); if (items[i] == 1) { prn1("", items[i], ""); } else { fix("(", items[i], ")"); } if (i < ssize) { dbg("%c", seps[i]); out("%c", seps[i]); } } out("\n"); dbg("\n<<<END\n"); } int main() { int t; scanf("%d", &t); for (int i = 0; i < t; ++i) { int k, a, b, r = 0; scanf("%d", &k); if (k <= 100) { prn1("", k, "\n"); } else { doExe(k); } } return 0; } |