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
import java.util.Scanner;

public class row {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        String[] parts = input.split(" ");

        long k = Long.parseLong(parts[0]);
        long a = Long.parseLong(parts[1]);
        long b = Long.parseLong(parts[2]);

        int num_of_digits = Long.toString(b).length();
        int max = num_of_digits * 81;

        int count = 0;
        for (int fn = 1; fn <= max; ++fn) {
            long k_fn = k * fn;
            if (k_fn >= a && k_fn <= b) {
                long sum = 0;
                long n = k_fn;
                while (n > 0) {
                    long d = n % 10;
                    sum += d * d;
                    n = n / 10;
                }

                if (sum == fn)
                    count++;
            }
        }

        System.out.print(count);
    }
}