Advertisement
tien_noob

Counting Numbers - DP

May 25th, 2021 (edited)
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define task "TESTCODE"
  3. using namespace std;
  4. long long f[19][10][2][2];
  5. long long a, b;
  6. string s;
  7. long long Count(int i, int bef, bool zeros, bool low)
  8. {
  9.     if (i == s.length())
  10.     {
  11.         return 1;
  12.     }
  13.     long long &res = f[i][bef][zeros][low];
  14.     if (res != -1)
  15.     {
  16.         return res;
  17.     }
  18.     res = 0;
  19.     int lim = s[i] - '0';
  20.     if (low)
  21.     {
  22.         lim = 9;
  23.     }
  24.     for (int j = 0; j <= lim; ++ j)
  25.     {
  26.         if (j != bef || zeros == 0)
  27.         {
  28.             res += Count(i + 1, j, zeros | j, low | (j < s[i] - '0'));
  29.         }
  30.     }
  31.     return res;
  32. }
  33. long long Get(long long x)
  34. {
  35.     s = to_string(x);
  36.     memset(f, -1, sizeof(f));
  37.     return Count(0,0,0,0);
  38. }
  39. void read()
  40. {
  41.    cin >> a >> b;
  42. }
  43. void solve()
  44. {
  45.    cout << Get(b) - Get(a - 1);
  46. }
  47. int main()
  48. {
  49.     ios_base::sync_with_stdio(false);
  50.     cin.tie(nullptr);
  51.     //freopen(task".INP", "r", stdin);
  52.     //freopen(task".OUT", "w", stdout);
  53.     int t = 1;
  54.     bool typetest =false;
  55.     if (typetest)
  56.     {
  57.         cin >> t;
  58.     }
  59.     for (int _ = 1; _ <= t; ++ _ )
  60.     {
  61.         read();
  62.         solve();
  63.     }
  64.     return 0;
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement