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


const uint64_t K_CONST = 726216412500000ULL;
const uint64_t POWERS[10] = { 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 };


uint64_t f(uint64_t n)
{
    uint64_t res = 0;

    while (n > 0)
    {
        res += POWERS[n % 10];
        n /= 10;
    }

    return res;
}


int main()
{
    std::ios_base::sync_with_stdio(0);

    //
    uint64_t K = 0, A = 0, B = 0;

    std::cin >> K;
    std::cin >> A;
    std::cin >> B;

    //
    uint64_t max = B;
    if (K <= K_CONST) { max = std::min(max, K * 17 * POWERS[9]); }

    //
    uint64_t n = K;
    uint64_t res = 0;

    while (n <= max)
    {
        if (n >= A && K * f(n) == n) {
            res += 1;
        }

        n += K;
    }

    std::cout << res << std::endl;
    return 0;
}