import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class jed { public static final Chunk _0 = new Chunk("", 0); public static final Chunk _1 = new Chunk("1", 1); public static final Chunk _2 = new Chunk("1+1", 2); public static final Chunk _3 = new Chunk("1+1+1", 3); public static final Chunk _4 = new Chunk("1+1+1+1", 4); public static final Chunk _5 = new Chunk("1+1+1+1+1", 5); public static final Chunk _6 = new Chunk("(1+1)*(1+1+1)", 5); public static final Chunk _7 = new Chunk("(1+1)*(1+1+1)+1", 6); public static final Chunk _8 = new Chunk("(1+1)*(1+1+1+1)", 6); public static final Chunk _9 = new Chunk("(1+1+1)*(1+1+1)", 6); public static final Chunk NIE = new Chunk("NIE", -1); public static void main(String[] args) { final Scanner in = new Scanner(System.in); final int i1 = in.nextInt(); List<Long> toProcess = new ArrayList<>(i1); for (int i = 0; i < i1; i++) { toProcess.add(in.nextLong()); } for (Long toProces : toProcess) { System.out.println(jedynkuj(toProces)); } System.exit(0); } private static Chunk jedynkuj(final long num) { if (num < 10) { switch ((int) num) { case 0: return _0; case 1: return _1; case 2: return _2; case 3: return _3; case 4: return _4; case 5: return _5; case 6: return _6; case 7: return _7; case 8: return _8; case 9: return _9; } } final List<Long> czynniki = naCzynniki(num); StringBuilder sb = new StringBuilder(); int count = 0; for (Long czynnik : czynniki) { final Chunk chunk; final boolean deep = czynnik > 9 && czynnik % 2 == 1; if (deep) { count++; chunk = jedynkuj(czynnik - 1); } else { chunk = jedynkuj(czynnik); } if (NIE == chunk) { return NIE; } count += chunk.no; if (count > 100) { return NIE; } if (sb.length() > 0) { sb.append("*"); } if (deep) { sb.append("(1+(").append(chunk.txt).append("))"); } else { sb.append("(").append(chunk.txt).append(")"); } } return new Chunk(sb.toString(), count); } private static List<Long> naCzynniki(long x) { long i, e; List<Long> res = new ArrayList<>(); i = 2; e = (int) (Math.sqrt(x)); while (i <= e) { while ((x % i) == 0) { x /= i; e = (int) (Math.sqrt(x)); res.add(i); } i++; } if (x > 1) res.add(x); return res; } private static class Chunk { public String txt; public int no; public Chunk(String txt, int no) { this.txt = txt; this.no = no; } @Override public String toString() { return txt; } } }
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 149 150 151 152 153 154 | import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class jed { public static final Chunk _0 = new Chunk("", 0); public static final Chunk _1 = new Chunk("1", 1); public static final Chunk _2 = new Chunk("1+1", 2); public static final Chunk _3 = new Chunk("1+1+1", 3); public static final Chunk _4 = new Chunk("1+1+1+1", 4); public static final Chunk _5 = new Chunk("1+1+1+1+1", 5); public static final Chunk _6 = new Chunk("(1+1)*(1+1+1)", 5); public static final Chunk _7 = new Chunk("(1+1)*(1+1+1)+1", 6); public static final Chunk _8 = new Chunk("(1+1)*(1+1+1+1)", 6); public static final Chunk _9 = new Chunk("(1+1+1)*(1+1+1)", 6); public static final Chunk NIE = new Chunk("NIE", -1); public static void main(String[] args) { final Scanner in = new Scanner(System.in); final int i1 = in.nextInt(); List<Long> toProcess = new ArrayList<>(i1); for (int i = 0; i < i1; i++) { toProcess.add(in.nextLong()); } for (Long toProces : toProcess) { System.out.println(jedynkuj(toProces)); } System.exit(0); } private static Chunk jedynkuj(final long num) { if (num < 10) { switch ((int) num) { case 0: return _0; case 1: return _1; case 2: return _2; case 3: return _3; case 4: return _4; case 5: return _5; case 6: return _6; case 7: return _7; case 8: return _8; case 9: return _9; } } final List<Long> czynniki = naCzynniki(num); StringBuilder sb = new StringBuilder(); int count = 0; for (Long czynnik : czynniki) { final Chunk chunk; final boolean deep = czynnik > 9 && czynnik % 2 == 1; if (deep) { count++; chunk = jedynkuj(czynnik - 1); } else { chunk = jedynkuj(czynnik); } if (NIE == chunk) { return NIE; } count += chunk.no; if (count > 100) { return NIE; } if (sb.length() > 0) { sb.append("*"); } if (deep) { sb.append("(1+(").append(chunk.txt).append("))"); } else { sb.append("(").append(chunk.txt).append(")"); } } return new Chunk(sb.toString(), count); } private static List<Long> naCzynniki(long x) { long i, e; List<Long> res = new ArrayList<>(); i = 2; e = (int) (Math.sqrt(x)); while (i <= e) { while ((x % i) == 0) { x /= i; e = (int) (Math.sqrt(x)); res.add(i); } i++; } if (x > 1) res.add(x); return res; } private static class Chunk { public String txt; public int no; public Chunk(String txt, int no) { this.txt = txt; this.no = no; } @Override public String toString() { return txt; } } } |