#include <stdio.h> #include <vector> #include <math.h> #include <map> using namespace std; char output[1000000]; int pos; int oneCounter; vector<int> factorize(int n) { int sq = sqrt(n)+1; vector<int> result; int k = 2; while (n > 1 && k <= sq) { while ((n%k) == 0) { result.push_back(k); n /= k; } k++; } if (n != 1) result.push_back(n); return result; } void put(char c) { output[pos++] = c; if (c == '1') oneCounter++; } void printSum(int n) { if (n == 0) return; if (n == 1) put('1'); put('('); for (int i = 0; i < n; ++i) { put('1'); if (i < n-1) put('+'); } put(')'); } void doStuff(int k) { vector<int> f = factorize(k); for (int i = 0; i < f.size(); ++i) { if (f[i] > 6) { put('('); put('1'); put('+'); doStuff(f[i]-1); put(')'); } else { printSum(f[i]); } if (i < f.size()-1) put('*'); } } void singleCase(int k) { if (k == 1) { printf("1\n"); return; } pos = 0; oneCounter = 0; doStuff(k); put('\0'); if (oneCounter > 100) printf("NIE\n"); else printf("%s\n", output); } void scanSingleCase() { int k; scanf("%d", &k); singleCase(k); } int main() { int t; scanf("%d", &t); while (t--) scanSingleCase(); 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 | #include <stdio.h> #include <vector> #include <math.h> #include <map> using namespace std; char output[1000000]; int pos; int oneCounter; vector<int> factorize(int n) { int sq = sqrt(n)+1; vector<int> result; int k = 2; while (n > 1 && k <= sq) { while ((n%k) == 0) { result.push_back(k); n /= k; } k++; } if (n != 1) result.push_back(n); return result; } void put(char c) { output[pos++] = c; if (c == '1') oneCounter++; } void printSum(int n) { if (n == 0) return; if (n == 1) put('1'); put('('); for (int i = 0; i < n; ++i) { put('1'); if (i < n-1) put('+'); } put(')'); } void doStuff(int k) { vector<int> f = factorize(k); for (int i = 0; i < f.size(); ++i) { if (f[i] > 6) { put('('); put('1'); put('+'); doStuff(f[i]-1); put(')'); } else { printSum(f[i]); } if (i < f.size()-1) put('*'); } } void singleCase(int k) { if (k == 1) { printf("1\n"); return; } pos = 0; oneCounter = 0; doStuff(k); put('\0'); if (oneCounter > 100) printf("NIE\n"); else printf("%s\n", output); } void scanSingleCase() { int k; scanf("%d", &k); singleCase(k); } int main() { int t; scanf("%d", &t); while (t--) scanSingleCase(); return 0; } |