forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximum-number-that-sum-of-the-prices-is-less-than-or-equal-to-k.cpp
129 lines (121 loc) · 4.12 KB
/
maximum-number-that-sum-of-the-prices-is-less-than-or-equal-to-k.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Time: O(max(logk, x) * log((logk) / x))
// Space: O((logk) / x)
// bit manipulation, binary search, combinatorics
class Solution {
public:
long long findMaximumNumber(long long k, int x) {
const auto& binary_search_right = [](long long left, long long right, const auto& check) {
while (left <= right) {
const auto mid = left + (right - left) / 2;
if (!check(mid)) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return right;
};
long long result = 0, prefix_cnt = 0;
vector<long long> lookup = {0};
for (int i = 0; (lookup.back() << x) + (1ll << (i + x - 1)) <= k; i += x) {
lookup.emplace_back((lookup.back() << x) + (1ll << (i + x - 1)));
}
const auto& count = [&](int l) {
return (prefix_cnt << (x * l)) + lookup[l];
};
while (k >= prefix_cnt) {
const auto& l = binary_search_right(1, size(lookup) - 1, [&](int l) {
return count(l) <= k;
});
long long cnt = count(l);
int i = x * l;
const int c = min(cnt ? static_cast<int>(__lg(k / cnt)) : x - 1, x - 1);
cnt <<= c;
i += c;
k -= cnt;
result += 1ll << i;
if ((i + 1) % x == 0) {
++prefix_cnt;
}
}
return result - 1;
}
};
// Time: O(max(logk, x) * (max(logk, x) / x))
// Space: O(1)
// bit manipulation, combinatorics
class Solution2 {
public:
long long findMaximumNumber(long long k, int x) {
long long result = 0, prefix_cnt = 0;
while (k >= prefix_cnt) {
long long cnt = prefix_cnt;
int i = 0;
for (; (cnt << x) + (1ll << (i + x - 1)) <= k; i += x) {
cnt = (cnt << x) + (1ll << (i + x - 1));
}
const int c = min(cnt ? static_cast<int>(__lg(k / cnt)) : x - 1, x - 1);
cnt <<= c;
i += c;
k -= cnt;
result += 1ll << i;
if ((i + 1) % x == 0) {
++prefix_cnt;
}
}
return result - 1;
}
};
// Time: O(max(logk, x)^2)
// Space: O(1)
// bit manipulation, combinatorics
class Solution3 {
public:
long long findMaximumNumber(long long k, int x) {
long long result = 0, prefix_cnt = 0;
while (k >= prefix_cnt) {
long long cnt = prefix_cnt;
int i = 0;
for (; (cnt << 1) + ((i + 1) % x == 0 ? 1ll << i : 0) <= k; ++i) {
cnt = (cnt << 1) + ((i + 1) % x == 0 ? 1ll << i : 0);
}
k -= cnt;
result += 1ll << i;
if ((i + 1) % x == 0) {
++prefix_cnt;
}
}
return result - 1;
}
};
// Time: O(max(logk, x) * (max(logk, x) / x))
// Space: O(1)
// bit manipulation, binary search, combinatorics
class Solution4 {
public:
long long findMaximumNumber(long long k, int x) {
const auto& binary_search_right = [](long long left, long long right, const auto& check) {
while (left <= right) {
const auto mid = left + (right - left) / 2;
if (!check(mid)) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return right;
};
const auto& count = [&](long long v) {
long long cnt = 0;
for (int i = 0; (1ll << (i + x - 1)) <= v; i += x) {
const long long q = (v + 1) / (1ll << ((i + x - 1) + 1));
const long long r = (v + 1) % (1ll << ((i + x - 1) + 1));
cnt += q * 1 * (1ll << (i + x - 1)) + max(r - 1 * (1ll << (i + x - 1)), 0ll);
}
return cnt;
};
return binary_search_right(1, max(k << 2, 1ll << x), [&](long long v) {
return count(v) <= k;
}); // right bound is verified by checking all possible (k, v) values, or just set right = solution.findMaximumNumber(10**15, 8) <= 10**15
}
};