Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma GCC optimize("Ofast")
- #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
- #pragma GCC optimize("unroll-loops")
- #include <bits/stdc++.h>
- using namespace std;
- #ifdef TOWRIST
- #define debug(...) cout << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__);
- #else
- #define debug(...);
- #endif
- 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...); }
- #define int long long
- #define all(x) x.begin(), x.end()
- #define double long double
- #define endl '\n'
- #define ff first
- #define ss second
- #define v vector
- using pii = pair<int, int>;
- const bool multipleTestCases = false;
- const int SUM_OFFSET = 200;
- int dp[20][2][2][2][2][400];
- bool isPrime(int num)
- {
- return num == 2 or num == 3 or num == 5 or num == 7;
- }
- void solve()
- {
- int L, R, K, S, MN, MX; cin >> L >> R >> K >> S >> MN >> MX;
- auto f = [&](int num) {
- auto s = to_string(num);
- memset(dp, -1, sizeof dp);
- function<int(int, bool, bool, bool, bool, int)> g = [&](int pos, bool tight, bool lead, bool mn, bool mx, int sum) -> int
- {
- if(pos == s.size())
- {
- return mn and mx and abs(sum) <= S;
- }
- int &ans = dp[pos][tight][lead][mn][mx][sum + SUM_OFFSET];
- if(ans != -1) return ans;
- int lb = 0;
- int ub = tight ? s[pos] - '0' : 9;
- ans = 0;
- for(int i = lb; i <= ub; ++i)
- {
- int tight_ = tight and i == ub;
- int lead_ = lead and i == 0;
- int mn_ = !lead_ and (mn or i == MN);
- int mx_ = !lead_ and (mx or i == MX);
- int sum_ = isPrime(i) ? sum + i : sum - i;
- if(lead_ or (MN <= i and i <= MX)) {
- ans += g(pos + 1, tight_, lead_, mn_, mx_, sum_);
- }
- }
- return ans;
- };
- return g(0, true, true, false, false, 0);
- };
- K += f(L - 1);
- int low = L - 1;
- int high = R + 1;
- while(high - low > 1)
- {
- int mid = (low + high) / 2;
- if(f(mid) >= K)
- {
- high = mid;
- }
- else
- {
- low = mid;
- }
- }
- if(f(high) != K)
- {
- cout << -1 << endl;
- return;
- }
- cout << high << endl;
- }
- signed main()
- {
- cin.tie(nullptr)->sync_with_stdio(false);
- int t = 1;
- multipleTestCases and cin >> t;
- for (int i = 1; i <= t; i++)
- {
- // cout << "Case #" << i << ": ";
- solve();
- }
- }
- /*
- nayak: 1003113210023121
- 2nd team: 1003113210023121
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement