CSES - Shared codeLink to this code:
https://cses.fi/paste/36f9c1e1bd402e3f84cf97/
// हर हर महादेव
#include <bits/stdc++.h>
#define int long long int
using namespace std;
const int inf = 1e18;
int64_t power(int64_t x, int64_t y){
int64_t res = 1;
if (x == 0) return 0;
while (y > 0){
if (y & 1)
res = (res*x);
y = y >> 1;
x = (x*x);
}
return res;
}
map<string,int> mp;
int get(string s){
if(mp.find(s) != mp.end()){
return mp[s];
}
int n = s.size();
int &ans = mp[s];
bool ok = true;
for(int i = 0; i < n; i++){
for(int j = (i == 0); j < s[i] - '0'; j++){
if(i == 0 || s[i-1] != j + '0'){
ans += power(9,n-i-1);
}
}
if(i != 0 && s[i] == s[i-1]){
ok = false;
break;
}
}
ans += ok;
if(n > 1){
ans += get(string(n-1,'9'));
}
return ans;
}
int get(int x){
if(x <= 0){
return 0;
}
return get(to_string(x));
}
int32_t main(){
ios::sync_with_stdio(false);
cin.tie(0);
int a,b;
cin >> a >> b;
cout << get(b) - get(a-1) + (a == 0);
return 0;
}