Advertisement
tepyotin2

Counting Numbers

Jul 3rd, 2025
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7. long long dp[19][10][2][2];
  8.  
  9. long long solve(size_t n, int pre, string s, bool is_zero, bool tight, string viz = "") {
  10.     if (n==s.size()) {
  11.         return 1;
  12.     }
  13.     if (pre!=-1 && dp[n][pre][is_zero][tight]) {
  14.         cout << "dp_viz: " << viz << '\n';
  15.         return dp[n][pre][is_zero][tight];
  16.     }
  17.  
  18.     int limit = tight ? s[n]-'0' : 9;
  19.     long long ans = 0;
  20.     for (int i=0; i<=limit; i++) {
  21.         if (i==pre && !is_zero) continue;
  22.         cout << "viz: " << viz+to_string(i) << '\n';
  23.         if (i<limit) {
  24.             ans += solve(n+1, i, s, is_zero && i==0, false, viz+to_string(i));
  25.         } else {
  26.             ans += solve(n+1, i, s, is_zero && i==0, tight, viz+to_string(i));
  27.         }
  28.     }
  29.  
  30.     if (pre==-1) return ans;
  31.     return dp[n][pre][is_zero][tight] = ans;
  32. }
  33.  
  34.  
  35. int main() {
  36.     ios_base::sync_with_stdio(0), cin.tie(0);
  37.  
  38.     long long a, b;
  39.     cin >> a >> b;
  40.  
  41.     a -= 1;
  42.  
  43.     string sa = to_string(a);
  44.     string sb = to_string(b);
  45.  
  46.     long long rb = solve(0, -1, sb, true, true);
  47.  
  48.     memset(dp, 0, sizeof(dp));
  49.  
  50.     long long ra = solve(0, -1, sa, true, true);
  51.  
  52.     cout << rb - ra << '\n';
  53.  
  54.     return 0;
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement