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

public class row {

	private static int f(long n) {
		int res = 0;
		while (n > 0) {
			res += (n % 10) * (n % 10);
			n /= 10;
		}
		return res;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		long maxF = 18 * 81;

		Scanner sc = new Scanner(System.in);
		long k = sc.nextLong();
		long a = sc.nextLong();
		long b = sc.nextLong();
		sc.close();

		long maxN = 0;
		if (k >= (Long.MAX_VALUE/maxF))
			maxN = b;
		else 
			maxN = Math.min(b, maxF * k);
		long start = ((a % k) == 0) ? (a / k) : ((a / k) + 1);
		
		start *= k;		
		int res = 0;
		while ((start >= a) && (start <= maxN)){
			if (k*f(start) == start )
				res++;
			start += k;
		}

		System.out.println(res);
		System.exit(0);
	}

}