Guest User

Untitled

a guest
Nov 9th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. int count(vector<int> &digits, int idx, int digit_before, int less, int leading_zero) {
  8. if (idx == digits.size()) return 1;
  9.  
  10. int cnt = 0;
  11. int k = less ? 9 : digits[idx];
  12.  
  13. for (int i = 0; i <= k; i++) {
  14. int zero = (i == 0) ? leading_zero : 0;
  15. int new_less = (i == k) ? less : 1;
  16. if (!leading_zero && abs(digit_before - i) <= 1) continue;
  17. cnt += count(digits, idx + 1, i, new_less, zero);
  18. }
  19.  
  20. return cnt;
  21. }
  22.  
  23. int main() {
  24. int a, b;
  25. vector<int> b_digits{};
  26. vector<int> a_digits{};
  27.  
  28. cin >> a >> b;
  29.  
  30. while (b) {
  31. b_digits.push_back(b % 10);
  32. b /= 10;
  33. }
  34.  
  35. a--;
  36. while (a) {
  37. a_digits.push_back(a % 10);
  38. a /= 10;
  39. }
  40.  
  41. reverse(begin(a_digits), end(a_digits));
  42. reverse(begin(b_digits), end(b_digits));
  43.  
  44. cout << count(b_digits, 0, 0, 0, 1) - count(a_digits, 0, 0, 0, 1) << endl;
  45.  
  46. return 0;
  47. }
Add Comment
Please, Sign In to add comment