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
import java.io.BufferedInputStream;
import java.math.BigInteger;
import java.util.Scanner;

public class row {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(new BufferedInputStream(System.in));
        BigInteger k = scanner.nextBigInteger();
        BigInteger a = scanner.nextBigInteger();
        BigInteger b = scanner.nextBigInteger();

        int counter = 0;

        for (long i = 1; i <= 10 * 81; i++) {
            BigInteger candidate = k.multiply(BigInteger.valueOf(i));
            if (sumOfSquaresOfDigits(candidate) == i && candidate.compareTo(a) >= 0 && candidate.compareTo(b) <= 0) {
                counter++;
            }
        }
        System.out.println(counter);
    }

    private static int sumOfSquaresOfDigits(BigInteger candidate) {
        int sum = 0;
        while (candidate.compareTo(BigInteger.ZERO) > 0) {
            int digit = candidate.remainder(BigInteger.TEN).intValue();
            sum += digit * digit;
            candidate = candidate.divide(BigInteger.TEN);
        }
        return sum;
    }


}