This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
#define pb push_back
#define pf push_front
#define mp make_pair
#define f first
#define s second
#define sz(x) int(x.size())
template<class T> using V = vector<T>;
using pi = pair<int, int>;
using vi = V<int>;
using vpi = V<pi>;
using ll = long long;
using pl = pair<ll, ll>;
using vpl = V<pl>;
using vl = V<ll>;
using db = long double;
template<class T> using pq = priority_queue<T, V<T>, less<T>>;
const int MOD = 1e9 + 7;
const ll INFL = ll(1e17);
const int LG = 19;
using H = array<int, 6>;
int main() {
cin.tie(0)->sync_with_stdio(0);
string L, R; cin >> L >> R;
while(sz(L) < sz(R)) L = "0"+L;
ll ans = 0;
if (L == string(sz(L), '0')) {
L.back() = '1';
ans = 1;
}
map<H, ll> MEMO;
function<ll(int, int, int, int, int, int)> DP = [&](int i, int gl, int lr, int nonz, int a, int b) {
H cur = {i, gl, lr, nonz, a, b};
if (MEMO.find(cur) != MEMO.end()) return MEMO[cur];
// cout << i << " " << gl << " " << lr << " " << nonz << " " << a << " " << b << endl;
if (i == sz(L)) {
// cout << a << " " << b << endl;
return MEMO[cur] = 1LL;
}
ll RES = 0;
for(int c = 0; c <= 9; c++) {
int ngl = (gl || c > (L[i]-'0'));
int nlr = (lr || c < (R[i]-'0'));
// cout << L[i] << " " << c << " " << R[i] << endl;
if (!gl && c < (L[i]-'0')) continue;
if (!lr && c > (R[i]-'0')) continue;
if (!nonz && c == 0) {
RES += DP(i+1, ngl, nlr, 0, -2, -1);
continue;
}
if (a == c || b == c) continue;
RES += DP(i+1, ngl, nlr, 1, b, c);
}
return MEMO[cur] = RES;
};
ans += DP(0, 0, 0, 0, -2, -1);
cout << ans << nl;
exit(0-0);
}
/*
123456789 987654321
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |