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;
using ll = long long;
using pl = pair<ll, ll>;
using pi = pair<int, int>;
#define vt vector
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(), x.end()
#define size(x) ((int) (x).size())
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define F0R(i, b) FOR (i, 0, b)
#define endl '\n'
const ll INF = 1e18;
const int inf = 1e9;
template<template<typename> class Container, typename T>
ostream& operator<<(ostream& os, Container<T> o) {
os << "{";
int g = size(o);
for (auto i : o) os << i << ((--g) == 0 ? "" : ", ");
os << "}";
return os;
}
void _print() {
cerr << "\n";
}
template<typename T, typename ...V>
void _print(T t, V... v) {
cerr << t; if (sizeof...(v)) cerr << ", "; _print(v...);
}
#ifdef LOCAL
#define dbg(x...) cerr << #x << " = "; _print(x);
#else
#define dbg(x...)
#define cerr if (0) std::cerr
#endif
/*
we just need to remember the last and second digits
*/
using t4 = tuple<int, int, int, int>;
map<t4, ll> memo;
vt<ll> digs;
// which are we placing
ll dp(int i, int last, int second_last, int tight) {
if (i == -1) return 1;
auto it = memo.find({i, last, second_last, tight});
if (it != memo.end()) return it->s;
int has_not_placed_digit = last >= 10;
ll ans = 0;
if (tight) {
F0R (j, digs[i] + 1) {
if (j == last || j == second_last) continue;
ans += dp(i - 1, j + (has_not_placed_digit && j == 0 ? 10 : 0), last, j == digs[i]);
}
} else {
F0R (j, 10) {
if (j == last || j == second_last) continue;
ans += dp(i - 1, j + (has_not_placed_digit && j == 0 ? 10 : 0), last, 0);
}
}
return memo[{i, last, second_last, tight}] = ans;
}
ll count(ll x) {
if (x < 0) return 0;
digs.clear();
memo.clear();
while (x) digs.pb(x % 10), x /= 10;
return dp(size(digs) - 1, 10, 10, 1);
}
main() {
cin.tie(0)->sync_with_stdio(0);
ll a, b; cin >> a >> b;
cout << count(b) - count(a - 1) << endl;
}
Compilation message (stderr)
numbers.cpp:87:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
87 | main() {
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |