fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define ld long double
  4. #define lli long long int
  5. #define ulli unsigned long long
  6. #define freo(task) if(fopen(task".inp", "r")){ freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); }
  7. #define pb push_back
  8. #define bit(x, i) ((x >> i) & 1)
  9. #define FOR(i, a, b) for(int i=(a); i<=(b); i++)
  10. #define FORD(i, a, b) for(int i=(a); i>=(b); i--)
  11. #define oo 1e18
  12. #define debug cout<<"VUDEPTRAI\n";
  13. using namespace std;
  14. mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
  15. ll rand(ll l, ll r) { assert(l <= r); return uniform_int_distribution<ll>(l, r)(rd); }
  16. int a, b;
  17. lli dp[20][180][2];
  18. void get(lli x, vector<int> &digit){
  19. while(x){
  20. digit.pb(x%10);
  21. x/=10;
  22. }
  23. }
  24. lli digitsolve(int i, int sum, bool tight, vector<int> &digit){
  25. if(i<0)
  26. return sum;
  27. if(!tight && dp[i][sum][tight]!=-1)
  28. return dp[i][sum][tight];
  29. lli ans=0;
  30. int maxk=(tight ? digit[i] : 9);
  31. FOR(k, 0, maxk){
  32. bool newtight=tight && (k==maxk);
  33. ans+=digitsolve(i-1, sum+k, newtight, digit);
  34. }
  35. if(!tight)
  36. dp[i][sum][tight]=ans;
  37. return ans;
  38. }
  39. lli solve(int a, int b){
  40. memset(dp, -1, sizeof(dp));
  41. vector<int> digitA;
  42. get(a-1, digitA);
  43. vector<int> digitB;
  44. get(b, digitB);
  45. return digitsolve(digitB.size()-1, 0, 1, digitB)-digitsolve(digitA.size()-1, 0, 1, digitA);
  46. }
  47. int main(){
  48. ios::sync_with_stdio(false);
  49. cin.tie(nullptr);
  50. // freo("");
  51. while(cin>>a>>b){
  52. if(a==-1 && b==-1)
  53. return 0;
  54. cout<<solve(a, b)<<'\n';
  55. }
  56. // cerr << "\n" << 1.0 * clock() / CLOCKS_PER_SEC << "s ";
  57. return 0;
  58. }
  59. /*
  60.  
  61.  
  62.  
  63. */
  64.  
Success #stdin #stdout 0.01s 5308KB
stdin
Standard input is empty
stdout
Standard output is empty