danielvitor23

Space Elevator

Oct 1st, 2020 (edited)
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long int i64;
  5. typedef pair<int, int> ii;
  6. typedef pair<i64, i64> ll;
  7. typedef vector<int> vi;
  8. typedef vector<i64> vi64;
  9. typedef vector<ii> vii;
  10. typedef vector<ll> vll;
  11. typedef vector<vi> vvi;
  12.  
  13. const double eps = 1e-9;
  14.  
  15. #define eq(a, b) (abs(a - b) < eps)
  16. #define lt(a, b) ((a + eps) < b)
  17. #define gt(a, b) (a > (b + eps))
  18. #define le(a, b) (a < (b + eps))
  19. #define ge(a, b) ((a + eps) > b)
  20.  
  21. #define fastIO() ios_base::sync_with_stdio(0), cin.tie(0)
  22. #define all(x) (x).begin(), (x).end()
  23. #define rall(x) (x).rbegin(), (x).rend()
  24. #define ms(a, x) memset(a, x, sizeof(a))
  25. #define len(x) (x).size()
  26. #define pb push_back
  27. #define eb emplace_back
  28. #define fi first
  29. #define se second
  30.  
  31. const int dtx[] = { 0, 0, -1, 1, 1, -1,  1, -1};
  32. const int dty[] = {-1, 1,  0, 0, 1, -1, -1,  1};
  33. const int dtxc[] = {1,  1, 2,  2, -1, -1, -2, -2};
  34. const int dtyc[] = {2, -2, 1, -1,  2, -2,  1, -1};
  35.  
  36. const double pi = acos(-1.0);
  37. const int inf = 0x3f3f3f3f;
  38. const int maxn = 2e5+5;
  39. const int mod = 1e9+7;
  40.  
  41. string s;
  42.  
  43. i64 timer;
  44. i64 seen[20][2][2][11];
  45. i64 dp[20][2][2][11];
  46.  
  47. i64 solve(int idx = 0, int can = 0, int put = 0, int last = 0) {
  48.     if (idx == (int)s.size()) return put;
  49.     if (seen[idx][can][put][last] == timer) return dp[idx][can][put][last];
  50.     seen[idx][can][put][last] = timer;
  51.     i64 ans = 0LL;
  52.     int mx = (can ? 9 : s[idx]-'0');
  53.     if (!put or !idx) {
  54.         for (int i = 0; i <= mx; ++i) {
  55.             if (i == 4) continue;
  56.             ans += solve(idx + 1, can | (i != mx), (i != 0), i);
  57.         }
  58.     } else {
  59.         for (int i = 0; i <= mx; ++i) {
  60.             if (i == 4 or (last == 1 and i == 3)) continue;
  61.             ans += solve(idx + 1, can | (i != mx), 1, i);
  62.         }
  63.     }
  64.     return dp[idx][can][put][last] = ans;
  65. }
  66.  
  67. unsigned long long int top, lo, hi, best, ans;
  68.  
  69. int main() {
  70.     fastIO();
  71.     while (cin >> top) {
  72.         lo = 0LL;
  73.         hi = 1e19;
  74.         while (lo <= hi) {
  75.             best = lo + (hi - lo)/2LL;
  76.             ++timer;
  77.             s = to_string(best);
  78.             if (solve() >= top) {
  79.                 ans = best;
  80.                 hi = best - 1LL;
  81.             } else {
  82.                 lo = best + 1LL;
  83.             }
  84.         }
  85.         cout << ans << '\n';
  86.     }
  87.     return 0;
  88. }
  89.  
  90.  
Advertisement
Add Comment
Please, Sign In to add comment