import java.math.BigInteger; import java.util.Scanner; public class row { static int MAX_EXP = 18; private BigInteger f(BigInteger n) { BigInteger sumOfSquares = BigInteger.ZERO; while (n.compareTo(BigInteger.ZERO) > 0) { BigInteger lastDigit = n.remainder(BigInteger.valueOf(10)); sumOfSquares = sumOfSquares.add(lastDigit.multiply(lastDigit)); n = n.add(lastDigit.negate()).divide(BigInteger.valueOf(10)); } return sumOfSquares; } public int solution(BigInteger k, BigInteger a, BigInteger b) { int result = 0; for (int fn = 0; fn <= 9*9*MAX_EXP; fn++) { BigInteger possibleCandidate = k.multiply(BigInteger.valueOf(fn)); if (possibleCandidate.compareTo(a) >= 0 && possibleCandidate.compareTo(b) <= 0 && k.multiply(f(possibleCandidate)).equals(possibleCandidate)) { result++; } } return result; } public static void main(String[] args) { BigInteger k, a, b; Scanner scanner = new Scanner(System.in); k = new BigInteger(scanner.next()); a = new BigInteger(scanner.next()); b = new BigInteger(scanner.next()); scanner.close(); int result = new row().solution(k, a, b); System.out.println(result); } }
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 | import java.math.BigInteger; import java.util.Scanner; public class row { static int MAX_EXP = 18; private BigInteger f(BigInteger n) { BigInteger sumOfSquares = BigInteger.ZERO; while (n.compareTo(BigInteger.ZERO) > 0) { BigInteger lastDigit = n.remainder(BigInteger.valueOf(10)); sumOfSquares = sumOfSquares.add(lastDigit.multiply(lastDigit)); n = n.add(lastDigit.negate()).divide(BigInteger.valueOf(10)); } return sumOfSquares; } public int solution(BigInteger k, BigInteger a, BigInteger b) { int result = 0; for (int fn = 0; fn <= 9*9*MAX_EXP; fn++) { BigInteger possibleCandidate = k.multiply(BigInteger.valueOf(fn)); if (possibleCandidate.compareTo(a) >= 0 && possibleCandidate.compareTo(b) <= 0 && k.multiply(f(possibleCandidate)).equals(possibleCandidate)) { result++; } } return result; } public static void main(String[] args) { BigInteger k, a, b; Scanner scanner = new Scanner(System.in); k = new BigInteger(scanner.next()); a = new BigInteger(scanner.next()); b = new BigInteger(scanner.next()); scanner.close(); int result = new row().solution(k, a, b); System.out.println(result); } } |