import java.io.*; import java.util.Arrays; import java.util.stream.IntStream; import java.math.BigDecimal; import java.math.BigInteger; import java.util.StringTokenizer; public class row { static int[] squares = IntStream.range(0, 10).map(n -> n*n).toArray(); static int sumOfDigitSquares(int number) { int length = String.valueOf(number).length(); int[] digits = new int[length]; while (number > 0) { digits[--length] = number % 10; number /= 10; } return Arrays.stream(digits).map(n -> squares[n]).sum(); } public static void main(String[] args) { // FastReader fr = FastReader.from("51 5000 10000"); FastReader fr = FastReader.from(System.in); int k = fr.nextInt(); int a = fr.nextInt(); int b = fr.nextInt(); int counter = 0; for (int i = a; i <= b; i++) { if (k*sumOfDigitSquares(i) == i) counter++; } System.out.println(counter); } } /** * FastReader class helps to read input in the form of words * from an {@link InputStream}. Good to use as a parser. * <p></p> * Usage: * <pre> * Assuming an input stream with the following lines: * asd xxx * 123 * {@code * final FastReader fastReader = FastReader.from(System.in); * final String s1 = fastReader.next(); * final String s2 = fastReader.next(); * final int n = fastReader.nextInt(); * ... * } * </pre> * * @author Murat Derya Ozen * @since: 9/28/13 1:50 PM */ final class FastReader { private final BufferedReader bufferedReader; /* legacy class preferred over String#split and Scanner for performance */ private StringTokenizer tokenizer; private FastReader(final BufferedReader bufferedReader) { this.bufferedReader = bufferedReader; this.tokenizer = null; } /** * Returns a {@link FastReader} instance that reads input from {@code inputStream}. * * @param inputStream * @return Returns a {@link FastReader} instance that reads input from {@code inputStream}. */ public static final FastReader from(final InputStream inputStream) { return new FastReader(new BufferedReader(new InputStreamReader(inputStream))); } public static final FastReader from(String string) { return new FastReader(new BufferedReader(new StringReader(string))); } /** * Returns the next word acquired by {@link StringTokenizer}. * Moves on to the next line if the current line has been processed. * * @return Returns the next word acquired by {@link StringTokenizer}, * or null if end of stream has been reached. * @throws RuntimeException If {@link java.io.BufferedReader#readLine()} throws an {@link IOException}. */ public String next() { return tokenize() ? tokenizer.nextToken() : null; } /** * Checks to see if there are any more words left in the {@code inputStream}. * Can be used to check if end of stream has been reached, as well. * If required, reads another line from the {@code inputStream}; i.e this operation * might perform an I/O; possibly block if end of stream is not reached but stream * is not yet available to yield a new line. * * @return Returns true if there are more words to read in the {@code inputStream} * and end of stream has not been reached. False otherwise. * @throws RuntimeException If {@link java.io.BufferedReader#readLine()} throws an {@link IOException}. */ public boolean canReadMore() { return tokenize(); } private boolean tokenize() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { // read a line, see if end of stream has been reached String line = null; try { if ((line = bufferedReader.readLine()) == null) return false; } catch (IOException unexpected) { throw new RuntimeException(unexpected); } tokenizer = new StringTokenizer(line); } return true; } /** * Returns the next {@code int} acquired by {@link StringTokenizer} * using {@link Integer#parseInt(String)} on {@link #next()}. * Moves on to the next line if the current line has been processed. * * @return Returns the next {@code int} acquired by {@link StringTokenizer}. * @throws RuntimeException If {@link java.io.BufferedReader#readLine()} throws an {@link IOException}. * @throws NumberFormatException If an invalid input is encountered or end of stream has been reached. */ public int nextInt() { return Integer.parseInt(next()); } /** * Returns the next {@code long} acquired by {@link StringTokenizer} * using {@link Long#parseLong(String)} on {@link #next()}. * Moves on to the next line if the current line has been processed. * * @return Returns the next {@code long} acquired by {@link StringTokenizer}. * @throws RuntimeException If {@link java.io.BufferedReader#readLine()} throws an {@link IOException}. * @throws NumberFormatException If an invalid input is encountered or end of stream has been reached. */ public long nextLong() { return Long.parseLong(next()); } /** * Returns the next {@code double} acquired by {@link StringTokenizer} * using {@link Double#parseDouble(String)} on {@link #next()}. * Moves on to the next line if the current line has been processed. * * @return Returns the next {@code double} acquired by {@link StringTokenizer}. * @throws RuntimeException If {@link java.io.BufferedReader#readLine()} throws an {@link IOException}. * @throws NumberFormatException If an invalid input is encountered or end of stream has been reached. */ public double nextDouble() { return Double.parseDouble(next()); } /** * Returns the next {@link BigDecimal} acquired by {@link StringTokenizer} * using BigDecimal's String constructor on {@link #next()}. * Moves on to the next line if the current line has been processed. * * @return Returns the next {@code BigDecimal} acquired by {@link StringTokenizer}. * @throws RuntimeException If {@link java.io.BufferedReader#readLine()} throws an {@link IOException}. * @throws NumberFormatException If an invalid input is encountered or end of stream has been reached. */ public BigDecimal nextBigDecimal() { return new BigDecimal(next()); } /** * Returns the next {@link BigInteger} acquired by {@link StringTokenizer} * using BigInteger's String constructor on {@link #next()}. * Moves on to the next line if the current line has been processed. * * @return Returns the next {@code BigInteger} acquired by {@link StringTokenizer}. * @throws RuntimeException If {@link java.io.BufferedReader#readLine()} throws an {@link IOException}. * @throws NumberFormatException If an invalid input is encountered or end of stream has been reached. */ public BigInteger nextBigInteger() { return new BigInteger(next()); } /** * Closes the input stream. * * @throws RuntimeException If {@link java.io.BufferedReader#readLine()} throws an {@link IOException}. * @see java.io.BufferedReader#close() */ public void close() { try { bufferedReader.close(); } catch (IOException unexpected) { throw new RuntimeException(unexpected); } } }
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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | import java.io.*; import java.util.Arrays; import java.util.stream.IntStream; import java.math.BigDecimal; import java.math.BigInteger; import java.util.StringTokenizer; public class row { static int[] squares = IntStream.range(0, 10).map(n -> n*n).toArray(); static int sumOfDigitSquares(int number) { int length = String.valueOf(number).length(); int[] digits = new int[length]; while (number > 0) { digits[--length] = number % 10; number /= 10; } return Arrays.stream(digits).map(n -> squares[n]).sum(); } public static void main(String[] args) { // FastReader fr = FastReader.from("51 5000 10000"); FastReader fr = FastReader.from(System.in); int k = fr.nextInt(); int a = fr.nextInt(); int b = fr.nextInt(); int counter = 0; for (int i = a; i <= b; i++) { if (k*sumOfDigitSquares(i) == i) counter++; } System.out.println(counter); } } /** * FastReader class helps to read input in the form of words * from an {@link InputStream}. Good to use as a parser. * <p></p> * Usage: * <pre> * Assuming an input stream with the following lines: * asd xxx * 123 * {@code * final FastReader fastReader = FastReader.from(System.in); * final String s1 = fastReader.next(); * final String s2 = fastReader.next(); * final int n = fastReader.nextInt(); * ... * } * </pre> * * @author Murat Derya Ozen * @since: 9/28/13 1:50 PM */ final class FastReader { private final BufferedReader bufferedReader; /* legacy class preferred over String#split and Scanner for performance */ private StringTokenizer tokenizer; private FastReader(final BufferedReader bufferedReader) { this.bufferedReader = bufferedReader; this.tokenizer = null; } /** * Returns a {@link FastReader} instance that reads input from {@code inputStream}. * * @param inputStream * @return Returns a {@link FastReader} instance that reads input from {@code inputStream}. */ public static final FastReader from(final InputStream inputStream) { return new FastReader(new BufferedReader(new InputStreamReader(inputStream))); } public static final FastReader from(String string) { return new FastReader(new BufferedReader(new StringReader(string))); } /** * Returns the next word acquired by {@link StringTokenizer}. * Moves on to the next line if the current line has been processed. * * @return Returns the next word acquired by {@link StringTokenizer}, * or null if end of stream has been reached. * @throws RuntimeException If {@link java.io.BufferedReader#readLine()} throws an {@link IOException}. */ public String next() { return tokenize() ? tokenizer.nextToken() : null; } /** * Checks to see if there are any more words left in the {@code inputStream}. * Can be used to check if end of stream has been reached, as well. * If required, reads another line from the {@code inputStream}; i.e this operation * might perform an I/O; possibly block if end of stream is not reached but stream * is not yet available to yield a new line. * * @return Returns true if there are more words to read in the {@code inputStream} * and end of stream has not been reached. False otherwise. * @throws RuntimeException If {@link java.io.BufferedReader#readLine()} throws an {@link IOException}. */ public boolean canReadMore() { return tokenize(); } private boolean tokenize() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { // read a line, see if end of stream has been reached String line = null; try { if ((line = bufferedReader.readLine()) == null) return false; } catch (IOException unexpected) { throw new RuntimeException(unexpected); } tokenizer = new StringTokenizer(line); } return true; } /** * Returns the next {@code int} acquired by {@link StringTokenizer} * using {@link Integer#parseInt(String)} on {@link #next()}. * Moves on to the next line if the current line has been processed. * * @return Returns the next {@code int} acquired by {@link StringTokenizer}. * @throws RuntimeException If {@link java.io.BufferedReader#readLine()} throws an {@link IOException}. * @throws NumberFormatException If an invalid input is encountered or end of stream has been reached. */ public int nextInt() { return Integer.parseInt(next()); } /** * Returns the next {@code long} acquired by {@link StringTokenizer} * using {@link Long#parseLong(String)} on {@link #next()}. * Moves on to the next line if the current line has been processed. * * @return Returns the next {@code long} acquired by {@link StringTokenizer}. * @throws RuntimeException If {@link java.io.BufferedReader#readLine()} throws an {@link IOException}. * @throws NumberFormatException If an invalid input is encountered or end of stream has been reached. */ public long nextLong() { return Long.parseLong(next()); } /** * Returns the next {@code double} acquired by {@link StringTokenizer} * using {@link Double#parseDouble(String)} on {@link #next()}. * Moves on to the next line if the current line has been processed. * * @return Returns the next {@code double} acquired by {@link StringTokenizer}. * @throws RuntimeException If {@link java.io.BufferedReader#readLine()} throws an {@link IOException}. * @throws NumberFormatException If an invalid input is encountered or end of stream has been reached. */ public double nextDouble() { return Double.parseDouble(next()); } /** * Returns the next {@link BigDecimal} acquired by {@link StringTokenizer} * using BigDecimal's String constructor on {@link #next()}. * Moves on to the next line if the current line has been processed. * * @return Returns the next {@code BigDecimal} acquired by {@link StringTokenizer}. * @throws RuntimeException If {@link java.io.BufferedReader#readLine()} throws an {@link IOException}. * @throws NumberFormatException If an invalid input is encountered or end of stream has been reached. */ public BigDecimal nextBigDecimal() { return new BigDecimal(next()); } /** * Returns the next {@link BigInteger} acquired by {@link StringTokenizer} * using BigInteger's String constructor on {@link #next()}. * Moves on to the next line if the current line has been processed. * * @return Returns the next {@code BigInteger} acquired by {@link StringTokenizer}. * @throws RuntimeException If {@link java.io.BufferedReader#readLine()} throws an {@link IOException}. * @throws NumberFormatException If an invalid input is encountered or end of stream has been reached. */ public BigInteger nextBigInteger() { return new BigInteger(next()); } /** * Closes the input stream. * * @throws RuntimeException If {@link java.io.BufferedReader#readLine()} throws an {@link IOException}. * @see java.io.BufferedReader#close() */ public void close() { try { bufferedReader.close(); } catch (IOException unexpected) { throw new RuntimeException(unexpected); } } } |