#include <stdio.h> #include <string> using namespace std; int t; long long int k; long long int trojki[20]; long long int wsp[20]; int deg; string wyraz; string prefiks; int main() { trojki[0] = 1; for(int i = 1; i < 20; i++) trojki[i] = 3*trojki[i-1]; scanf("%d", &t); for(int i = 0; i < t; i++) { scanf("%lld", &k); for(int j = 0; j < 20; j++) wsp[j] = 0; deg = -1; wyraz.clear(); prefiks.clear(); if(k >= 27) { for(int j = 19; j >= 0; j--) { wsp[j] = k/(trojki[j]); k -= (wsp[j]*trojki[j]); if(deg == -1 && wsp[j] > 0) deg = j; } if(wsp[deg] == 1 && wsp[deg-1] == 0) wyraz = "1*(1+1+1)"; else if(wsp[deg] == 1 && wsp[deg-1] == 1) wyraz = "1*(1+1+1)+1"; else if(wsp[deg] == 1 && wsp[deg-1] == 2) wyraz = "1*(1+1+1)+(1+1)"; else if(wsp[deg] == 2 && wsp[deg-1] == 0) wyraz = "(1+1)*(1+1+1)"; else if(wsp[deg] == 2 && wsp[deg-1] == 1) wyraz = "(1+1)*(1+1+1)+1"; else if(wsp[deg] == 2 && wsp[deg-1] == 2) wyraz = "(1+1)*(1+1+1)+(1+1)"; for(int j = 0; j < deg-1; j++) prefiks += "(1+1+1)*("; prefiks += wyraz; prefiks += ")"; for(int j = deg-2; j >= 0; j--) { if(wsp[j] == 0) prefiks += ")"; if(wsp[j] == 1) prefiks += "+1)"; if(wsp[j] == 2) prefiks += "+(1+1))"; } prefiks.erase(prefiks.length()-1, 1); printf("%s\n", prefiks.c_str()); } else { prefiks = "1"; for(int i = 0; i < k-1; i++) prefiks += "+1"; printf("%s\n", prefiks.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 67 68 69 70 71 72 73 74 | #include <stdio.h> #include <string> using namespace std; int t; long long int k; long long int trojki[20]; long long int wsp[20]; int deg; string wyraz; string prefiks; int main() { trojki[0] = 1; for(int i = 1; i < 20; i++) trojki[i] = 3*trojki[i-1]; scanf("%d", &t); for(int i = 0; i < t; i++) { scanf("%lld", &k); for(int j = 0; j < 20; j++) wsp[j] = 0; deg = -1; wyraz.clear(); prefiks.clear(); if(k >= 27) { for(int j = 19; j >= 0; j--) { wsp[j] = k/(trojki[j]); k -= (wsp[j]*trojki[j]); if(deg == -1 && wsp[j] > 0) deg = j; } if(wsp[deg] == 1 && wsp[deg-1] == 0) wyraz = "1*(1+1+1)"; else if(wsp[deg] == 1 && wsp[deg-1] == 1) wyraz = "1*(1+1+1)+1"; else if(wsp[deg] == 1 && wsp[deg-1] == 2) wyraz = "1*(1+1+1)+(1+1)"; else if(wsp[deg] == 2 && wsp[deg-1] == 0) wyraz = "(1+1)*(1+1+1)"; else if(wsp[deg] == 2 && wsp[deg-1] == 1) wyraz = "(1+1)*(1+1+1)+1"; else if(wsp[deg] == 2 && wsp[deg-1] == 2) wyraz = "(1+1)*(1+1+1)+(1+1)"; for(int j = 0; j < deg-1; j++) prefiks += "(1+1+1)*("; prefiks += wyraz; prefiks += ")"; for(int j = deg-2; j >= 0; j--) { if(wsp[j] == 0) prefiks += ")"; if(wsp[j] == 1) prefiks += "+1)"; if(wsp[j] == 2) prefiks += "+(1+1))"; } prefiks.erase(prefiks.length()-1, 1); printf("%s\n", prefiks.c_str()); } else { prefiks = "1"; for(int i = 0; i < k-1; i++) prefiks += "+1"; printf("%s\n", prefiks.c_str()); } } return 0; } |