/*
Author: Nguyen Chi Thanh - High School for the Gifted - VNU.HCM (i2528)
*/
#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define fi first
#define se second
#define __builtin_popcount __builtin_popcountll
#define all(x) (x).begin(), (x).end()
#define BIT(x, i) (((x) >> (i)) & 1)
#define MASK(x) ((1ll << (x)))
#define debug(a, l, r) for (int _i = (l); _i <= (r); ++_i) cout << (a)[_i] << ' '; cout << '\n';
int n;
string A, B;
long long dp[20][2][2][11][11][2];
long long F(int i, int left, int right, int pre, int pre2, int leading) {
if (i == n + 1) return 1;
long long &memo = dp[i][left][right][pre + 1][pre2 + 1][leading];
if (memo != -1) return memo;
memo = 0;
int lo = (left ? 0 : A[i] - '0');
int hi = (right ? 9 : B[i] - '0');
for (int c = lo; c <= hi; ++c) {
int nxt_left = (left | (c > A[i] - '0'));
int nxt_right = (right | (c < B[i] - '0'));
int nxt_leading = leading && (c == 0);
if (c == pre || c == pre2) continue;
memo += F(i + 1, nxt_left, nxt_right, nxt_leading ? -1 : c, pre, nxt_leading);
}
return memo;
}
long long G(long long a, long long b) {
assert(a <= b);
A = to_string(a);
B = to_string(b);
n = (int)A.size();
while (A.size() < B.size()) A = '0' + A;
A = ' ' + A; B = ' ' + B;
memset(dp, -1, sizeof dp);
return F(1, 0, 0, -1, -1, 1);
}
void solve() {
long long a, b; cin >> a >> b;
cout << G(a, b);
}
signed main() {
#ifdef NCTHANH
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(nullptr); cout.tie(nullptr);
solve();
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |