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

using namespace std;

int main() {
    unsigned long long l, r;
    int counter = 0;
    cin >> l >> r;
    while (l != r + 1) {
        if (l < 10) {
            counter++;
            l++;
        }
        else {
            int word_count = 0;
            int sum_of_digits = 0;
            string l_tostr = to_string(l);
            for (char b : l_tostr) {
                sum_of_digits += (b - '0');
            }
            for (char a : l_tostr) {
                if (a == '0') break;
                else if (a == '1') word_count++;
                else if (a == '2') {
                    if (l_tostr[l_tostr.length() - 1] == '0') word_count++;
                    else {
                        if ((l_tostr[l_tostr.length() - 1] - '0') % 2 == 0) word_count++;
                    }
                } else if (a == '3') {
                    if (sum_of_digits % 3 == 0) word_count++;
                } else if (a == '4') {
                    if (stoi(l_tostr.substr(l_tostr.length() - 2)) % 4 == 0) {
                        word_count++;
                    }
                } else if (a == '5') {
                    if (l_tostr[l_tostr.length() - 1] == '0' || l_tostr[l_tostr.length() - 1] == '5') word_count++;
                } else if (a == '6') {
                    if (l_tostr[l_tostr.length() - 1] == '0') {
                        if (sum_of_digits % 3 == 0) word_count++;
                    }
                    else {
                        if (((l_tostr[l_tostr.length() - 1] - '0') % 2 == 0) && sum_of_digits % 3 == 0) word_count++;
                    }
                } else if (a == '8') {
                    if (l_tostr.length() >= 3) {
                        if (stoi(l_tostr.substr(l_tostr.length() - 3)) % 8 == 0) word_count++;
                    }
                    else {
                        if (stoi(l_tostr) % 8 == 0) word_count++;
                    }
                } else if (a == '9') {
                    if (sum_of_digits % 9 == 0) word_count++;
                } else if (a == '7') {
                    if (l % 7 == 0) word_count++;
                }
            }
            if (word_count == l_tostr.length()) counter++;
            l++;
        }
    }
    cout << counter;
}