Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- int count(vector<int> &digits, int idx, int digit_before, int less, int leading_zero) {
- if (idx == digits.size()) return 1;
- int cnt = 0;
- int k = less ? 9 : digits[idx];
- for (int i = 0; i <= k; i++) {
- int zero = (i == 0) ? leading_zero : 0;
- int new_less = (i == k) ? less : 1;
- if (!leading_zero && abs(digit_before - i) <= 1) continue;
- cnt += count(digits, idx + 1, i, new_less, zero);
- }
- return cnt;
- }
- int main() {
- int a, b;
- vector<int> b_digits{};
- vector<int> a_digits{};
- cin >> a >> b;
- while (b) {
- b_digits.push_back(b % 10);
- b /= 10;
- }
- a--;
- while (a) {
- a_digits.push_back(a % 10);
- a /= 10;
- }
- reverse(begin(a_digits), end(a_digits));
- reverse(begin(b_digits), end(b_digits));
- cout << count(b_digits, 0, 0, 0, 1) - count(a_digits, 0, 0, 0, 1) << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment