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>
#define name "palinfree"
#define ll long long
#define db double
#define pb push_back
#define ii pair<int,int>
#define mp make_pair
#define fi first
#define se second
#define bit(x,i) (((x)>>(i))&1)
#define sz(s) (int)(s).size()
#define all(a) (a).begin(),(a).end()
#define rep(i,a,b) for(int i=(a), _b=(b); i<=_b; i++)
#define repd(i,a,b) for(int i=(a), _b=(b); i>=_b; i--)
#define _unique(x) (x).resize(unique(all(x)) - (x).begin())
using namespace std;
const int N = 1e5 + 3;
ll l, r;
ll f[25][2][25][13][13];
vector <int> digit;
ll Try (int i, bool isless, bool ok, int cnt, int pre1, int pre2, int s) {
if (i == -1) {
//if (ok) cout << s << "\n";
return ok;
}
if (isless && f[i][ok][cnt][pre1][pre2] != -1) return f[i][ok][cnt][pre1][pre2];
ll res = 0;
int mx = (isless ? 9 : digit[i]);
rep (j, 0, mx) {
if (!ok && j == 0) res += Try (i - 1, isless || j < mx, 0, 0, pre2, j, s * 10 + j);
else {
if (cnt <= 1) {
if (j != pre2) res += Try (i - 1, isless || j < mx, 1, cnt + 1, pre2, j, s * 10 + j);
}
else if (j != pre1 && j != pre2) res += Try (i - 1, isless || j < mx, 1, cnt + 1, pre2, j, s * 10 + j);
}
}
if (isless) f[i][ok][cnt][pre1][pre2] = res;
return res;
}
ll sol (ll x) {
digit.clear();
while (x > 0) digit.pb (x % 10), x /= 10;
return Try (sz (digit) - 1, 0, 0, 0, 0, 0, 0);
}
void solve (void) {
cin >> l >> r;
bool ok = 0;
if (l > r) swap (l, r), ok = 1;
rep (i, 0, 20) rep (ok, 0, 1) rep (cnt, 0, 20) rep (pre1, 0, 9) rep (pre2, 0, 9) f[i][ok][cnt][pre1][pre2] = -1;
ll res = sol (r) - sol (l - 1);
if (l == 0) res ++;
if (ok) res *= -1;
cout << res;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//freopen(name".inp","r",stdin);
//freopen(name".out","w",stdout);
solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |