#include <cstdio> #include <iostream> #include <cmath> #include <string> #include <vector> #include <algorithm> #include <map> using namespace std; //111011100110101100101000000000 map<long, string> cache; bool isCached(long k) { map<long, string>::iterator it = cache.find(k); return (it != cache.end()); } string solve(long k) { if (k == 1) { return "1"; } else if (k == 2) { return "(1+1)"; } else if (k == 3) { return "(1+1+1)"; } else if (k == 4) { return "(1+1+1+1)"; } else if (k == 5) { return "(1+1+1+1+1)"; } else if (k == 6) { return "(1+1)*(1+1+1)"; } if (isCached(k)) { return cache[k]; } long r = floor(sqrt(k)); for (int i = 2; i <= r; i++) { if (k % i == 0) { string result = solve(i) + "*" + solve(k / i); cache[k] = result; return result; } } string result = "(1+" + solve(k - 1) + ")"; cache[k] = result; return result; } int main() { long t; scanf("%ld\n", &t); for (int i = 0; i < t; i++) { long k; scanf("%ld\n", &k); string r = solve(k); size_t n = count(r.begin(), r.end(), '1'); if (n > 100) { printf("NIE\n"); } else { // printf("result: %ld, %lu, %s\n", k, n, r.c_str()); printf("%s\n", r.c_str()); } } 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 | #include <cstdio> #include <iostream> #include <cmath> #include <string> #include <vector> #include <algorithm> #include <map> using namespace std; //111011100110101100101000000000 map<long, string> cache; bool isCached(long k) { map<long, string>::iterator it = cache.find(k); return (it != cache.end()); } string solve(long k) { if (k == 1) { return "1"; } else if (k == 2) { return "(1+1)"; } else if (k == 3) { return "(1+1+1)"; } else if (k == 4) { return "(1+1+1+1)"; } else if (k == 5) { return "(1+1+1+1+1)"; } else if (k == 6) { return "(1+1)*(1+1+1)"; } if (isCached(k)) { return cache[k]; } long r = floor(sqrt(k)); for (int i = 2; i <= r; i++) { if (k % i == 0) { string result = solve(i) + "*" + solve(k / i); cache[k] = result; return result; } } string result = "(1+" + solve(k - 1) + ")"; cache[k] = result; return result; } int main() { long t; scanf("%ld\n", &t); for (int i = 0; i < t; i++) { long k; scanf("%ld\n", &k); string r = solve(k); size_t n = count(r.begin(), r.end(), '1'); if (n > 100) { printf("NIE\n"); } else { // printf("result: %ld, %lu, %s\n", k, n, r.c_str()); printf("%s\n", r.c_str()); } } return 0; } |