Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. /* package codechef; // don't place package name! */
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.util.HashMap;
  6. import java.util.HashSet;
  7. import java.util.InputMismatchException;
  8. import java.util.Map;
  9. import java.util.Set;
  10.  
  11. /* Name of the class has to be "Main" only if the class is public. */
  12. public class Main {
  13. public static void main(String[] args) throws IOException {
  14. final InputReader reader = new InputReader(System.in);
  15. int t = reader.readInt();
  16. StringBuilder sb = new StringBuilder();
  17.  
  18. for (int tItr = 0; tItr < t; tItr++) {
  19. long k = reader.readLong();
  20. long result = getDistinceDigitUpToK(k);
  21. sb.append(result).appendCodePoint('\n');
  22. }
  23.  
  24. System.out.print(sb.toString());
  25. reader.close();
  26. }
  27.  
  28. private static long getDistinceDigitUpToK(long k) {
  29. Map<Long, Long> map = new HashMap<Long, Long>();
  30. long limit = (long) Math.pow(10, k);
  31. for (long i = 0; i < limit; i++) {
  32. long signFront = i;
  33. long signBack = limit - i - 1;
  34. long digitCount = getDigitCount(signFront, signBack);
  35. Long count = map.getOrDefault(digitCount, 0L);
  36. map.put(digitCount, count + 1);
  37. }
  38. return map.get(2L);
  39. }
  40.  
  41. private static long getDigitCount(long signFront, long signBack) {
  42.  
  43. Set<Long> set = new HashSet<Long>();
  44. if (signFront == 0 || signBack == 0) {
  45. set.add(0L);
  46. }
  47. while (signFront > 0) {
  48. long digit = signFront % 10;
  49. set.add(digit);
  50. signFront = signFront / 10;
  51. }
  52.  
  53. while (signBack > 0) {
  54. long digit = signBack % 10;
  55. set.add(digit);
  56. signBack = signBack / 10;
  57. }
  58.  
  59. return set.size();
  60. }
  61.  
  62. }
  63.  
  64. class InputReader {
  65. private final InputStream stream;
  66. private final byte[] buf = new byte[1024];
  67.  
  68. private int curChar;
  69.  
  70. private int numChars;
  71.  
  72. public InputReader(InputStream stream) {
  73. this.stream = stream;
  74. }
  75.  
  76. public int read() {
  77. if (numChars == -1)
  78. throw new InputMismatchException();
  79. if (curChar >= numChars) {
  80. curChar = 0;
  81. try {
  82. numChars = stream.read(buf);
  83. } catch (IOException e) {
  84. throw new InputMismatchException();
  85. }
  86. if (numChars <= 0)
  87. return -1;
  88. }
  89. return buf[curChar++];
  90. }
  91.  
  92. public int readInt() {
  93. int c = read();
  94. while (isSpaceChar(c))
  95. c = read();
  96. int sgn = 1;
  97. if (c == '-') {
  98. sgn = -1;
  99. c = read();
  100. }
  101. int res = 0;
  102. do {
  103. if (c < '0' || c > '9')
  104. throw new InputMismatchException();
  105. res *= 10;
  106. res += c - '0';
  107. c = read();
  108. } while (!isSpaceChar(c));
  109. return res * sgn;
  110. }
  111.  
  112. public boolean isSpaceChar(int c) {
  113. return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  114. }
  115.  
  116. public String readString() {
  117. int c = read();
  118. while (isSpaceChar(c))
  119. c = read();
  120. StringBuilder res = new StringBuilder();
  121. do {
  122. res.appendCodePoint(c);
  123. c = read();
  124. } while (!isSpaceChar(c));
  125. return res.toString();
  126. }
  127.  
  128. public long readLong() {
  129. int c = read();
  130. while (isSpaceChar(c))
  131. c = read();
  132. int sgn = 1;
  133. if (c == '-') {
  134. sgn = -1;
  135. c = read();
  136. }
  137. long res = 0;
  138. do {
  139. if (c < '0' || c > '9')
  140. throw new InputMismatchException();
  141. res *= 10;
  142. res += c - '0';
  143. c = read();
  144. } while (!isSpaceChar(c));
  145. return res * sgn;
  146. }
  147.  
  148. public void close() throws IOException {
  149. stream.close();
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement