This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
long long dp[18][11][11];
string s;
long long rec(int i, int j, int k, bool lower, bool valid) {
if (i == 18) {
return valid;
}
if (dp[i][j][k] != -1 && lower && valid) {
return dp[i][j][k];
}
int m = lower ? 9 : s[i] - '0';
long long res = 0;
for (int c = 0; c <= m; ++c) {
if (c ^ j && c ^ k) {
bool lo = lower || c < m, ok = valid || c;
res += rec(i + 1, ok ? c : 10, j, lo, ok);
}
}
return lower && valid ? dp[i][j][k] = res : res;
}
long long calc(long long x) {
if (x < 0) {
return x;
}
s = to_string(x);
while (s.size() < 18) {
s = '0' + s;
}
return rec(0, 10, 10, 0, 0);
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
long long a, b; cin >> a >> b;
memset(dp, -1, sizeof(dp));
cout << calc(b) - calc(a - 1);
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |