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

int main(void)
{
    unsigned long long k, a, b, i, n, s, d, r;

    scanf("%llu%llu%llu", &k, &a, &b);

    a += k-1;
    a /= k;
    b /= k;
    r  = 0;

    if (b > 1500)
        b   = 1500;

    for (i = a; i <= b; i++) {
        n   = i * k;
        s   = 0;

        while (n) {
            d   = n % 10;
            n   /= 10;
            s   += d * d;
        }

        if (s == i)
            r++;
    }

    printf("%llu\n", r);
    return  0;
}