import java.util.Arrays; import java.util.Scanner; import java.util.SortedSet; import java.util.TreeSet; import java.util.stream.Collectors; import java.util.stream.Stream; public class jed { private static final int[] PRIMES = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}; private static final TreeSet<Product> PRODUCTS_OF_PRIMES = generate(); public final static String solve(int k) { if (k <= 100) { return representPrime(k); } Product closestPrimeProduct = PRODUCTS_OF_PRIMES.floor(new Product(k, null)); if (closestPrimeProduct != null && closestPrimeProduct.getProductValue() == k) { return representAsOnes(closestPrimeProduct); } else { SortedSet<Product> neighbourhood = PRODUCTS_OF_PRIMES.subSet(new Product(k - 99, null), new Product(k, null)); for (Product product : neighbourhood) { int neededSum = k - product.getProductValue(); if (neededSum + onesSum(product.getChosenIndexes()) <= 100) { return representAsOnes(product) + "+" + Stream.generate(() -> "1").sequential().limit(neededSum).collect(Collectors.joining("+")); } } } return "NIE"; } private final static String representAsOnes(Product closestPrimeProduct) { int[] chosenIndexes = closestPrimeProduct.getChosenIndexes(); return Arrays.stream(chosenIndexes).sequential() .mapToObj(index -> "(" + representPrime(PRIMES[index]) + ")") .collect(Collectors.joining("*")); } private final static String representPrime(int prime) { return Stream.generate(() -> "1").sequential().limit(prime).collect(Collectors.joining("+")); } private final static class Product { private final int productValue; private final int[] chosenIndexes; public Product(int productValue, int[] chosenIndexes) { this.productValue = productValue; this.chosenIndexes = chosenIndexes; } public int getProductValue() { return productValue; } public int[] getChosenIndexes() { return chosenIndexes; } } private final static TreeSet<Product> generate() { int length = 2; TreeSet<Product> products = new TreeSet<>((o1, o2) -> Integer.compare(o1.getProductValue(), o2.getProductValue())); int currentIndex = 0; while (true) { int[] chosenIndexes = new int[length]; int productsSize = currentIndex; int end = PRIMES.length - 1; while (true) { long product = product(chosenIndexes); if (onesSum(chosenIndexes) <= 100 && product <= 1000000000) { currentIndex++; products.add(new Product((int) product, chosenIndexes.clone())); } int i = findFirstNotEnded(chosenIndexes, end); if (i == -1) { break; } chosenIndexes[i]++; for (int j = i - 1; j >= 0; j--) { chosenIndexes[j] = chosenIndexes[i]; } if (onesSum(chosenIndexes) > 100) { end = chosenIndexes[i]; } } if (productsSize == currentIndex) { break; } length++; } return products; } private final static int findFirstNotEnded(int[] chosenIndexes, int end) { for (int i = 0; i < chosenIndexes.length; i++) { if (chosenIndexes[i] < end) { return i; } } return -1; } private static long product(int[] chosenIndexes) { long product = 1; for (int chosenIndex : chosenIndexes) { product *= PRIMES[chosenIndex]; } return product; } private final static int onesSum(int[] chosenIndexes) { int sum = 0; for (int chosenIndex : chosenIndexes) { sum += PRIMES[chosenIndex]; } return sum; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); in.nextLine(); for (int i = 0; i < t; i++) { System.out.println(solve(in.nextInt())); in.nextLine(); } } }
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 155 156 157 158 159 160 161 162 163 164 | import java.util.Arrays; import java.util.Scanner; import java.util.SortedSet; import java.util.TreeSet; import java.util.stream.Collectors; import java.util.stream.Stream; public class jed { private static final int[] PRIMES = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}; private static final TreeSet<Product> PRODUCTS_OF_PRIMES = generate(); public final static String solve(int k) { if (k <= 100) { return representPrime(k); } Product closestPrimeProduct = PRODUCTS_OF_PRIMES.floor(new Product(k, null)); if (closestPrimeProduct != null && closestPrimeProduct.getProductValue() == k) { return representAsOnes(closestPrimeProduct); } else { SortedSet<Product> neighbourhood = PRODUCTS_OF_PRIMES.subSet(new Product(k - 99, null), new Product(k, null)); for (Product product : neighbourhood) { int neededSum = k - product.getProductValue(); if (neededSum + onesSum(product.getChosenIndexes()) <= 100) { return representAsOnes(product) + "+" + Stream.generate(() -> "1").sequential().limit(neededSum).collect(Collectors.joining("+")); } } } return "NIE"; } private final static String representAsOnes(Product closestPrimeProduct) { int[] chosenIndexes = closestPrimeProduct.getChosenIndexes(); return Arrays.stream(chosenIndexes).sequential() .mapToObj(index -> "(" + representPrime(PRIMES[index]) + ")") .collect(Collectors.joining("*")); } private final static String representPrime(int prime) { return Stream.generate(() -> "1").sequential().limit(prime).collect(Collectors.joining("+")); } private final static class Product { private final int productValue; private final int[] chosenIndexes; public Product(int productValue, int[] chosenIndexes) { this.productValue = productValue; this.chosenIndexes = chosenIndexes; } public int getProductValue() { return productValue; } public int[] getChosenIndexes() { return chosenIndexes; } } private final static TreeSet<Product> generate() { int length = 2; TreeSet<Product> products = new TreeSet<>((o1, o2) -> Integer.compare(o1.getProductValue(), o2.getProductValue())); int currentIndex = 0; while (true) { int[] chosenIndexes = new int[length]; int productsSize = currentIndex; int end = PRIMES.length - 1; while (true) { long product = product(chosenIndexes); if (onesSum(chosenIndexes) <= 100 && product <= 1000000000) { currentIndex++; products.add(new Product((int) product, chosenIndexes.clone())); } int i = findFirstNotEnded(chosenIndexes, end); if (i == -1) { break; } chosenIndexes[i]++; for (int j = i - 1; j >= 0; j--) { chosenIndexes[j] = chosenIndexes[i]; } if (onesSum(chosenIndexes) > 100) { end = chosenIndexes[i]; } } if (productsSize == currentIndex) { break; } length++; } return products; } private final static int findFirstNotEnded(int[] chosenIndexes, int end) { for (int i = 0; i < chosenIndexes.length; i++) { if (chosenIndexes[i] < end) { return i; } } return -1; } private static long product(int[] chosenIndexes) { long product = 1; for (int chosenIndex : chosenIndexes) { product *= PRIMES[chosenIndex]; } return product; } private final static int onesSum(int[] chosenIndexes) { int sum = 0; for (int chosenIndex : chosenIndexes) { sum += PRIMES[chosenIndex]; } return sum; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); in.nextLine(); for (int i = 0; i < t; i++) { System.out.println(solve(in.nextInt())); in.nextLine(); } } } |