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;
typedef long long ll;
int32_t main () {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll L, R;
cin >> L >> R;
auto get = [&](ll eded) -> ll {
if (eded == -1LL) return 0;
string x = to_string(eded);
if (x.size() == 1) {
return x[0] - '0' + 1LL;
}
reverse (x.begin(), x.end());
const int MAXN = x.size();
const int MAXM = 10;
vector<vector<vector<array<ll, 2>>>> dp(MAXN + 1, vector<vector<array<ll, 2>>> (MAXM, vector<array<ll, 2>>(MAXM, {0, 0})));
for (int a = 0;a < MAXM;a ++) {
for (int b = 0;b < MAXM;b ++) {
if (a != b) {
dp[2][a][b][0] = 1;
if (x[1] - '0' >= a || (x[1] - '0' == a && x[0] - '0' >= b)) {
dp[2][a][b][1] = 1;
}
}
}
}
for (int i = 3;i <= MAXN;i ++) {
for (int a = 0;a < MAXM;a ++) {
for (int b = 0;b < MAXM;b ++) if (a != b) {
for (int c = 0;c < MAXM;c ++) if (a != c && b != c) {
if (a < x[i - 1] - '0' || (a == x[i - 1] - '0' && b < x[i - 2] - '0') || (a == x[i - 1] - '0' && b == x[i - 2] - '0' && c < x[i - 3] - '0')) {
dp[i][a][b][0] += dp[i - 1][b][c][0];
dp[i][a][b][1] += dp[i - 1][b][c][0];
} else if ((a == x[i - 1] - '0' && b == x[i - 2] - '0' && c == x[i - 3] - '0')) {
dp[i][a][b][0] += dp[i - 1][b][c][0];
dp[i][a][b][1] += dp[i - 1][b][c][1];
} else {
dp[i][a][b][0] += dp[i - 1][b][c][0];
}
}
}
}
}
ll ans = 10;
for (int i = 2;i <= MAXN;i ++) {
for (int a = 1;a < MAXM;a ++) {
for (int b = 0;b < MAXM;b ++) if (a != b) {
if (i < MAXN || (a < x[i - 1] - '0' || (a == x[i - 1] - '0' && b < x[i - 2] - '0'))) {
ans += dp[i][a][b][0];
} else if ((a == x[i - 1] - '0' && b == x[i - 2] - '0')) {
ans += dp[i][a][b][1];
}
}
}
}
return ans;
};
cout << get (R) - get (L - 1) << "\n";
// cout << get (R) << " " << get(L - 1) << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |