Submission

Status:

PPPPPPPPPP

Subtask/Task Score:

100/100

Score: 100

User: anonymous

Problemset: Croissaria Number

Language: cpp

Time: 0.166 second

Submitted On: 2026-11-01 19:40:23

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

ll exact[20];
void pre() {
    exact[1] = 0;
    for(int len = 2;len <= 18;len++){
        exact[len] = 0;
        for(int i=1;i<=9;i++){
            ll ways = 1;
            for(int j=0;j<len-1;j++) ways *= i;

            exact[len] += ways;
        }
    }
}

string s;
ll dp[20][2][11];

ll sol(int pos,int ti,int first){
    if(pos == (int)s.size()) return 1;

    ll &res = dp[pos][ti][first];
    if(res != -1) return res;
    res = 0;

    int up = ti ? (s[pos] - '0') : 9;

    for(int i=0;i<=up;i++)
    {
        if(i < first){
            int nti = ti && (i == up);
            res += sol(pos + 1, nti ,first);
        }
    }
    return res;
}
ll Hippopotomonstrosquipedaliophobia_same(ll num){
    if (num < 10) return 0;
        s = to_string(num);
    int len = s.size();

        memset(dp, -1, sizeof(dp));

    ll ans = 0;
    int first_up = s[0] - '0';

    for (int first = 1; first < first_up; first++) {
        ll ways = 1;
        for (int i = 0; i < len - 1; i++) ways *= first;
        ans += ways;
    }

    ans += sol(1, 1, first_up);
    return ans;
}

ll Hippopotomonstrosquipedaliophobia(ll num){
    if(num < 10) return 0;

    string t = to_string(num);
    int len = t.size();

    ll ans = 0;
    for (int i = 2; i < len; i++) ans += exact[i];
    ans += Hippopotomonstrosquipedaliophobia_same(num);

    return ans;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    pre();
    int n; cin >> n;

    while (n--){
        ll l,r; cin >> l >> r;
        cout << Hippopotomonstrosquipedaliophobia(r) - Hippopotomonstrosquipedaliophobia(l-1) << '\n';
    }
}