import java.util.HashMap; import java.util.Scanner; public class jed { static final HashMap<Integer, Integer> T = new HashMap<>(); static { T.put(2, 1); T.put(4, 2); T.put(8, 3); T.put(16, 4); T.put(32, 5); T.put(64, 6); T.put(128, 7); T.put(256, 8); T.put(512, 9); T.put(1024, 10); T.put(2048, 11); T.put(4096, 12); T.put(8192, 13); T.put(16384, 14); T.put(32768, 15); T.put(65536, 16); T.put(131072, 17); T.put(262144, 18); T.put(524288, 19); T.put(1048576, 20); T.put(2097152, 21); T.put(4194304, 22); T.put(8388608, 23); T.put(16777216, 24); T.put(33554432, 25); T.put(67108864, 26); T.put(134217728, 27); T.put(268435456, 28); T.put(536870912, 29); } static final Scanner scanner = new Scanner(System.in); static final StringBuilder sb = new StringBuilder(100); public static void main(String[] args) { int t = scanner.nextInt(); for (int i = 0; i < t; i++) { testCase(); } } private static void testCase() { int k = scanner.nextInt(); sb.setLength(0); if (k == 1) { print("1"); } else if (isPowerOfTwo(k)) { printPowerOfTwo(k); } else if (isPowerOfTwo(k - 1)) { print("1+"); printPowerOfTwo(k - 1); } else { int mod, val = k, closings = 0, counter = 0; boolean firstRun = true; while (val > 1) { mod = val % 3; val = val / 3; if (!firstRun) { print("*"); if (mod > 0 && val >= 1) { print("("); } } if (mod == 1) { print("1"); counter += 1; } else if (mod == 2) { print("(1+1)"); counter += 2; } if (val >= 1) { if (mod > 0) { print("+"); } print("(1+1+1)"); counter += 3; } if (firstRun) { firstRun = false; } else if (mod > 0 && val >= 1) { closings += 1; } if (counter > 100) { sb.setLength(0); sb.append("NIE"); flush(); return; } } for (int i = 0; i < closings; ++i) { print(")"); } } flush(); } static boolean isPowerOfTwo(int x) { return (x & (x - 1)) == 0; } static void printPowerOfTwo(int k) { for (int i = 0; i < T.get(k) - 1; ++i) { print("(1+1)*"); } print("(1+1)"); } static void print(String str) { sb.append(str); } static void flush() { System.out.println(sb); } }
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 | import java.util.HashMap; import java.util.Scanner; public class jed { static final HashMap<Integer, Integer> T = new HashMap<>(); static { T.put(2, 1); T.put(4, 2); T.put(8, 3); T.put(16, 4); T.put(32, 5); T.put(64, 6); T.put(128, 7); T.put(256, 8); T.put(512, 9); T.put(1024, 10); T.put(2048, 11); T.put(4096, 12); T.put(8192, 13); T.put(16384, 14); T.put(32768, 15); T.put(65536, 16); T.put(131072, 17); T.put(262144, 18); T.put(524288, 19); T.put(1048576, 20); T.put(2097152, 21); T.put(4194304, 22); T.put(8388608, 23); T.put(16777216, 24); T.put(33554432, 25); T.put(67108864, 26); T.put(134217728, 27); T.put(268435456, 28); T.put(536870912, 29); } static final Scanner scanner = new Scanner(System.in); static final StringBuilder sb = new StringBuilder(100); public static void main(String[] args) { int t = scanner.nextInt(); for (int i = 0; i < t; i++) { testCase(); } } private static void testCase() { int k = scanner.nextInt(); sb.setLength(0); if (k == 1) { print("1"); } else if (isPowerOfTwo(k)) { printPowerOfTwo(k); } else if (isPowerOfTwo(k - 1)) { print("1+"); printPowerOfTwo(k - 1); } else { int mod, val = k, closings = 0, counter = 0; boolean firstRun = true; while (val > 1) { mod = val % 3; val = val / 3; if (!firstRun) { print("*"); if (mod > 0 && val >= 1) { print("("); } } if (mod == 1) { print("1"); counter += 1; } else if (mod == 2) { print("(1+1)"); counter += 2; } if (val >= 1) { if (mod > 0) { print("+"); } print("(1+1+1)"); counter += 3; } if (firstRun) { firstRun = false; } else if (mod > 0 && val >= 1) { closings += 1; } if (counter > 100) { sb.setLength(0); sb.append("NIE"); flush(); return; } } for (int i = 0; i < closings; ++i) { print(")"); } } flush(); } static boolean isPowerOfTwo(int x) { return (x & (x - 1)) == 0; } static void printPowerOfTwo(int k) { for (int i = 0; i < T.get(k) - 1; ++i) { print("(1+1)*"); } print("(1+1)"); } static void print(String str) { sb.append(str); } static void flush() { System.out.println(sb); } } |