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
#include <iostream>
#include <set>

using namespace std;

int f(unsigned long long a)
{
	int res = 0;
	while (a != 0)
	{
		res += (a % 10) * (a % 10);
		a /= 10;
	}
	return res;
}

int main()
{
	unsigned long long k, a, b;
	cin >> k >> a >> b;
	unsigned long long lower_limit = 1;
	set<unsigned long long> result;
	for (int i = 0; i < 19; i++)
	{
		unsigned long long upper_limit = (i + 1) * 81;
		if (upper_limit + 10 >= lower_limit / k)
		{
			for (unsigned long long j = (lower_limit / k) * k; j < lower_limit * 10; j += k)
			{
				if (!(j <= b))
				{
					i = 50;
					break;
				}
				if (f(j) == j / k && j >= a) {
					result.insert(j);
				}
			}
		}
		lower_limit *= 10;
	}
	cout << result.size() << endl;
	return 0;
}