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
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
//
//  main.cpp
//  spoj2
//
//  Created by Piotrek on 23.09.2015.
//  Copyright 2015 Piotrek. All rights reserved.
//

#include <iostream>
#include <math.h>

using namespace std;

long long pow10(short int i){
    switch(i){
        case 0:
            return 1;
            break;
        case 1:
            return 10;
            break;
        case 2:
            return 100;
            break;
        case 3:
            return 1000;
            break;
        case 4:
            return 10000;
            break;
        case 5:
            return 100000;
            break;
        case 6:
            return 1000000;
            break;
        case 7:
            return 10000000;
            break;
        case 8:
            return 100000000;
            break;
        case 9:
            return 1000000000;
            break;
        case 10:
            return 10000000000;
            break;
        case 11:
            return 100000000000;
            break;
        case 12:
            return 1000000000000;
            break;
        case 13:
            return 10000000000000;
            break;
        case 14:
            return 100000000000000;
            break;
        case 15:
            return 1000000000000000;
            break;
        case 16:
            return 10000000000000000;
            break;
        case 17:
            return 100000000000000000;
            break;
        case 18:
            return 1000000000000000000;
            break;
        default:
            return 1;
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    unsigned long long k, a, b, s, i=1, j=0;
    short l=0, licz;
    cin >> k >> a >> b;
    while (a<=b) {
        if(a%k==0){
            s=0;
            while (a/i>0) {
                i*=10;
                l++;
            }
            for (licz=l-1; licz>=0; licz--) {
                s+=pow((a/pow10(licz))%10, 2);
            }
            if(s*k==a) {
                //cout << a << endl;
                j++;
            }
            a++;
        }
        else a++;
    }
    cout << j;
    return 0;
}