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
#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

template<typename TH>
void debug_vars(const char* data, TH head){
    cerr << data << "=" << head << "\n";
}

template<typename TH, typename... TA>
void debug_vars(const char* data, TH head, TA... tail){
    while(*data != ',') cerr << *data++;
    cerr << "=" << head << ",";
    debug_vars(data+1, tail...);
}

#ifdef LOCAL
#define debug(...) debug_vars(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#endif

/////////////////////////////////////////////////////////


class counter {
  private:
    int state;
  public:
    counter() : state(0) {}
    counter(int first) : state(first) {}
    counter& operator++(){ state++; return *this; }
    int operator*(){ return state; }
    bool operator!=(const counter& other) const { return (state != other.state); }
};



int main(){
    LL k, a, b;
    cin >> k >> a >> b;
    const LL MaxRange = 10000;

    LL range = min(MaxRange, b/k)+2;
    
    auto addSqrEl = [](LL acc, LL v){ return acc+(v-'0')*(v-'0'); };

    cout << accumulate(counter(), counter(range), 0,
            [&](int cur, int val){
                if(k*val < a || k*val > b) return cur;
                string s = to_string(k*val);
                if(accumulate(s.begin(), s.end(), 0LL, addSqrEl) == val)
                    cerr << k*val << endl;
                return cur + (accumulate(s.begin(),s.end(),0LL,addSqrEl) == val);
            } ) << endl;
}