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

public class row {

	public static void main(String... args) {
		@SuppressWarnings("resource")
		Scanner scanner = new Scanner(System.in);

		String line = scanner.nextLine();
		String[] values = line.split(" ");

		long k = Long.valueOf(values[0]);
		long a = Long.valueOf(values[1]);
		long b = Long.valueOf(values[2]);

		List<Long> accepted = new ArrayList<>();

		for (long i = a; i <= b; i++) {
			if (k * calculate(i) == i) {
				accepted.add(i);
			}
		}

		System.out.println(accepted.size());

	}

	public static long calculate(Long n) {
		List<Long> digits = new ArrayList<Long>();
		while (n > 0) {
			long d = n / 10;
			long k = n - d * 10;
			n = d;
			digits.add(k);
		}

		long value = 0;
		for (Long digit : digits) {
			value = value + (digit * digit);
		}
		return value;
	}

}