Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
1.0 kB
13
Indexable
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

// Generate all champ numbers up to the max limit of 10^18
set<ll> generateChampNumbers() {
    set<ll> champNumbers;
    string digits = "012345";
    do {
        for (int i = 1; i <= 6; ++i) {
            string current = digits.substr(0, i);
            if (current[0] != '0') { // skip leading 0
                ll num = stoll(current);
                champNumbers.insert(num);
            }
        }
    } while (next_permutation(digits.begin(), digits.end()));

    return champNumbers;
}

int countChampNumbersInRange(const set<ll> &champNumbers, ll x, ll y) {
    auto start = champNumbers.lower_bound(x);
    auto end = champNumbers.upper_bound(y);
    return distance(start, end);
}

int main() {
    int q;
    cin >> q;

    auto champNumbers = generateChampNumbers();

    while (q--) {
        ll x, y;
        cin >> x >> y;
        cout << countChampNumbersInRange(champNumbers, x, y) << endl;
    }

    return 0;
}