#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll x, y;
ll dp[22][12][12][2];
ll pow10[22];
ll cur;
int vals[22];
int getdig(ll a, int dig) {
return (a/pow10[dig])%10;
}
ll rec(int pos, int i ,int j, int mode) {
if(pos < 0) return 1;
if(dp[pos][i][j][mode] != -1) return dp[pos][i][j][mode];
dp[pos][i][j][mode] = 0;
if(mode) {
int cdig = vals[pos];
for(int k=0;k<cdig;k++) {
dp[pos][i][j][mode] += rec(pos-1, j, k, 0) * (ll)(i != k && j != k);
}
dp[pos][i][j][mode] += rec(pos-1, j, cdig, 1) * (ll)(i != cdig && j != cdig);
} else {
for(int k=0;k<=9;k++)
dp[pos][i][j][mode] += rec(pos-1, j, k, 0) * (ll)(i != k && j != k);
}
return dp[pos][i][j][mode];
}
int gsz(ll a) {
int sum = 0;
while(a) {a/=10; sum++;}
return sum;
}
ll ans(ll a) {
if(a == 0) return 1;
if(a < 0) return 0;
memset(dp, -1, sizeof dp);
int sz = gsz(a)-1;
ll ans = 0;
for(int i=0;i<=18;i++) vals[i] = getdig(a, i);
for(int i=1;i<vals[sz];i++) ans += rec(sz-1, 10, i, 0);
ans += (sz == 0 ? 0 : rec(sz-1, 10, 10, 0));
//cout << ans << '\n';
ans += rec(sz-1, 10, vals[sz], 1);
return ans;
}
int main() {
pow10[0] = 1;
for(int i=1;i<=18;i++) pow10[i] = pow10[i-1] * 10;
cin >> x >> y;
cout << ans(y) - ans(x-1);
}
Compilation message (stderr)
numbers.cpp:7:4: warning: built-in function 'pow10' declared as non-function [-Wbuiltin-declaration-mismatch]
7 | ll pow10[22];
| ^~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |