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

using namespace std;
long long maxF(long long x)
{
    return 81*ceil(log10((double)x));
}
unsigned int sumOfSquaredDigits(long long n)
{
    unsigned int sum=0,r;
    while (n!=0)
    {
        r=n%10;
        n=n/10;
        sum+=r*r;
    }
    return sum;
}

int main()
{
    long long k,a,b,n=0;
    cin >> k >> a >> b;
    if (k==1)
    {
        if (a==1)
            cout << 1 << endl;
        else
            cout << 0 << endl;
    }
    else
        for (long long i = ((a % k > 0) + a / k ) * k; i<=min(k*maxF(b),b); i+=k)
            if (k*sumOfSquaredDigits(i)==i) n++;
    cout << n << endl;
    return 0;
}