Advertisement
Guest User

Untitled

a guest
Apr 14th, 2024
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. #pragma GCC optimize("Ofast")
  2. #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
  3. #pragma GCC optimize("unroll-loops")
  4.  
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7.  
  8. #ifdef TOWRIST
  9. #define debug(...) cout << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__);
  10. #else
  11. #define debug(...);
  12. #endif
  13.  
  14. template <typename T> std::ostream &operator<<(std::ostream &stream, const vector<T> &vec) {for (size_t i = 0; i < vec.size(); i++) { stream << vec[i]; if (i != vec.size() - 1) stream << ' '; }; return stream; } template <typename T> std::istream &operator>>(std::istream &stream, vector<T> &vec) {for (T &x : vec) stream >> x; return stream; } template <typename T, typename U> std::ostream &operator<<(std::ostream &stream, const pair<T, U> &pr) {stream << pr.first << ' ' << pr.second; return stream; } template <typename T, typename U> std::istream &operator>>(std::istream &stream, pair<T, U> &pr) {stream >> pr.first >> pr.second; return stream; } template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string &s) { return '"' + s + '"'; } string to_string(char c) {string s; s += c; return s; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "1" : "0"); } string to_string(vector<bool> v) {bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) {if (!first) {res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) {string res = ""; for (size_t i = 0; i < N; i++) {res += static_cast<char>('0' + v[i]); } return res; } template <typename A> string to_string(A v) {bool first = true; string res = "{"; for (const auto &x : v) {if (!first) {res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cout << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {cout << " " << to_string(H); debug_out(T...); }
  15.  
  16. #define int long long
  17. #define all(x) x.begin(), x.end()
  18. #define double long double
  19. #define endl '\n'
  20. #define ff first
  21. #define ss second
  22. #define v vector
  23.  
  24. using pii = pair<int, int>;
  25. const bool multipleTestCases = false;
  26.  
  27. const int SUM_OFFSET = 200;
  28.  
  29. int dp[20][2][2][2][2][400];
  30.  
  31. bool isPrime(int num)
  32. {
  33. return num == 2 or num == 3 or num == 5 or num == 7;
  34. }
  35.  
  36. void solve()
  37. {
  38. int L, R, K, S, MN, MX; cin >> L >> R >> K >> S >> MN >> MX;
  39.  
  40. auto f = [&](int num) {
  41. auto s = to_string(num);
  42. memset(dp, -1, sizeof dp);
  43.  
  44. function<int(int, bool, bool, bool, bool, int)> g = [&](int pos, bool tight, bool lead, bool mn, bool mx, int sum) -> int
  45. {
  46.  
  47. if(pos == s.size())
  48. {
  49. return mn and mx and abs(sum) <= S;
  50. }
  51.  
  52. int &ans = dp[pos][tight][lead][mn][mx][sum + SUM_OFFSET];
  53. if(ans != -1) return ans;
  54.  
  55. int lb = 0;
  56. int ub = tight ? s[pos] - '0' : 9;
  57.  
  58. ans = 0;
  59. for(int i = lb; i <= ub; ++i)
  60. {
  61. int tight_ = tight and i == ub;
  62. int lead_ = lead and i == 0;
  63. int mn_ = !lead_ and (mn or i == MN);
  64. int mx_ = !lead_ and (mx or i == MX);
  65. int sum_ = isPrime(i) ? sum + i : sum - i;
  66.  
  67. if(lead_ or (MN <= i and i <= MX)) {
  68. ans += g(pos + 1, tight_, lead_, mn_, mx_, sum_);
  69. }
  70. }
  71.  
  72. return ans;
  73. };
  74.  
  75. return g(0, true, true, false, false, 0);
  76. };
  77.  
  78.  
  79. K += f(L - 1);
  80.  
  81. int low = L - 1;
  82. int high = R + 1;
  83. while(high - low > 1)
  84. {
  85. int mid = (low + high) / 2;
  86.  
  87. if(f(mid) >= K)
  88. {
  89. high = mid;
  90. }
  91. else
  92. {
  93. low = mid;
  94. }
  95. }
  96.  
  97. if(f(high) != K)
  98. {
  99. cout << -1 << endl;
  100. return;
  101. }
  102.  
  103. cout << high << endl;
  104. }
  105.  
  106. signed main()
  107. {
  108. cin.tie(nullptr)->sync_with_stdio(false);
  109. int t = 1;
  110. multipleTestCases and cin >> t;
  111. for (int i = 1; i <= t; i++)
  112. {
  113. // cout << "Case #" << i << ": ";
  114. solve();
  115. }
  116. }
  117.  
  118. /*
  119.  
  120. nayak: 1003113210023121
  121. 2nd team: 1003113210023121
  122.  
  123. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement