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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.io.IOException;
import java.io.InputStream;


/**
 * @author wojdo
 *
 */
public class row {

	public static void main(String[] args) throws IOException {
	
		InputStream is = System.in;
		long k = getInt(is);
		long a = getInt(is);
		if (a <= k) {
			a = k;
		} else {
			while (a % k != 0) {
				a++;
			}
		}
		long b = getInt(is);
		b = Math.min(b, 81 * String.valueOf(b).length() * k);
		
		long result = 0;
		for (long i = a; i <= b; i += k) {
			long numberLeft = i;
			long sumOfDigitSqares = i / k;
			while (numberLeft > 0) {
				long rest = numberLeft % 10;
				sumOfDigitSqares -= rest * rest;
				numberLeft /= 10;
				if (sumOfDigitSqares == 0) {
					if (numberLeft == 0) {
						result++;
					}
					break;
				} else if (sumOfDigitSqares < 0) {
					break;
				}
			}
		}
		
		System.out.println(result);
	}

	private static long getInt(InputStream is) throws IOException {
		
		long value = 0;
		int chr = is.read();
		while (chr >= '0' && chr <= '9') {
			value *= 10;
			value += chr - '0';
			chr = is.read();
		}
		
		if (chr == '\r') {
			is.read();
		}
		
		return value;
	}
}