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
#include <iostream>
#include <algorithm>
#include <limits.h>
using namespace std;

unsigned short curr_digit;
unsigned short curr_digit2;

long long GetDigitSquareSum(unsigned long long n, long long limit) {
	if (n < 10) {
		return n * n;
	}
	else {
		curr_digit = (n % 10);
		curr_digit2 = curr_digit * curr_digit;
		if (limit - curr_digit >= 0) {
			return curr_digit2 + GetDigitSquareSum((n - curr_digit) / 10, limit - curr_digit);
		}
	}
	return LLONG_MIN;
}

int main(int arc, char **argv) {
	long long k = 0, a = 0, b = 0;
	cin >> k >> a >> b;
	
	if (k == 0)
		return 0;
	
	long long limit = 0;
	unsigned short result = 0;
	unsigned long long first_index = max(k, k * (a / k));
	for (unsigned long long i = first_index; i <= b; i += k) {
		limit = i / k;
		if (GetDigitSquareSum(i, limit) == limit)
			result++;
	}
	cout << result;

	return 0;
}