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
#include <stdio.h>
#include <string.h>

unsigned long a, k, b;

unsigned int getLength(unsigned long val)
{
	int retval;
	
	retval = 1;
	while(val > 9)
	{
		val /= 10;
		retval++;
	}
	return retval;
}

unsigned int f(unsigned long val)
{
	unsigned retval = 0;
	int pom;

	do {
		pom = val % 10;
		retval += pom * pom;
		val /= 10;
	} while(val > 0);
	return retval;	
}

int main ()
{	
	unsigned int iloscCyfr;
	unsigned long loopMin, loopMax, i;
	unsigned result = 0;

	scanf("%ld %ld %ld", &k, &a, &b );
	iloscCyfr = getLength(b);
	loopMax = 9 * 9 * iloscCyfr;	// maksymalna wartość f(n)
	if (b/k < loopMax)
		loopMax = b;
	else 
		loopMax *= k;
	loopMin = a / k ; // minimalna wartość wielokrotności k 
	if(loopMin * k < a)
		loopMin++;
	loopMin *= k;	// minimalna wartość liczby n	
	for( i = loopMin; i <= loopMax; i+= k)
	{
		if(f(i) * k == i)
		{
			result++;		
//			printf("\n%d", i);
		}
	}
	printf("%d", result);
	return 0;
}