tien_noob

An example of dp-digit

May 4th, 2021 (edited)
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. long long a, k, dp[20][10][2][2], tmp;
  4. string s;
  5. void read()
  6. {
  7.     cin >> k >> a;
  8. }
  9. long long Count(int pos, int prenum, bool zero, bool low)
  10. {
  11.     if (pos == s.length())
  12.     {
  13.         return 1;
  14.     }
  15.     long long &res = dp[pos][prenum][zero][low];
  16.     if (res != -1)
  17.     {
  18.         return res;
  19.     }
  20.     res = 0;
  21.     int lim = s[pos] - '0';
  22.     if (low)
  23.     {
  24.         lim = 9;
  25.     }
  26.     for (int j = 0; j <= lim; ++ j)
  27.     {
  28.         if (abs(j - prenum) <= 1 || (zero == 0))
  29.         {
  30.             res += Count(pos + 1, j, zero | j, low | (j < (s[pos] - '0')));
  31.         }
  32.     }
  33.     return res;
  34. }
  35. long long Get(long long x)
  36. {
  37.     memset(dp, -1, sizeof(dp));
  38.     s = to_string(x);
  39.     return Count(0,0,0,0);
  40. }
  41. bool check(long long x)
  42. {
  43.     long long cnt = Get(x);
  44.     return cnt - tmp >= k;
  45. }
  46. void solve()
  47. {
  48.     long long low = a + 1, high = 1e18;
  49.     tmp = Get(a);
  50.     while (low <= high)
  51.     {
  52.         long long mid = (low + high)/2;
  53.         if (check(mid))
  54.         {
  55.             high = mid - 1;
  56.         }
  57.         else
  58.         {
  59.             low = mid + 1;
  60.         }
  61.     }
  62.     cout << low;
  63. }
  64. int main()
  65. {
  66.     ios_base::sync_with_stdio(false);
  67.     cin.tie(nullptr);
  68.     read();
  69.     solve();
  70. }
  71.  
Add Comment
Please, Sign In to add comment