Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Think of each set bit separately. A bit at position contributes to the score and costs from the sum budget.
Across the whole sequence, how many times can you use the same bit position? At most , because each of the numbers can contain that bit once.
So the problem becomes: you have items of cost , items of cost , items of cost , and so on. Every item gives exactly point.
When all items have equal value, there is no magic DP hiding here: buy the cheapest available bits first. Spending on a larger bit while a smaller bit is still available is just throwing budget into a hole.
Iterate costs . For each cost , take bits, add that to the answer, subtract their total cost, and continue.
A set bit is the real unit of this problem.
If some number contains bit , then that bit contributes exactly to and costs exactly in the total sum. So instead of thinking about whole numbers, think about buying individual bits.
For every bit position , we can use that bit in at most numbers, because each of the integers can contain bit at most once. That gives us this equivalent problem:
Every chosen bit gives score . We need maximize the number of chosen bits with total cost at most .
Now the greedy is forced: take cheaper bits before expensive bits. If we ever choose a bit of cost while some smaller-cost bit is still unused, swapping the expensive bit for the cheaper one keeps the score the same and uses no more budget. Usually it uses less. So any optimal solution can be transformed into one that takes bits in increasing cost order.
So we process costs
At cost , there are available bits. With remaining budget , we can afford at most of them, so we take
Add that amount to the answer and subtract its total cost from the remaining budget.
This also handles the annoying edge cases cleanly:
Why the selected bits can always become actual numbers: for each bit position, we choose it at most times, so assign those copies to distinct sequence positions. Different bit positions can go into the same number without conflict. This constructs valid non-negative integers with exactly the chosen total popcount.
The number of processed bit positions is , since costs double each time. Therefore each test case runs in
time and uses memory.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
ll n, k;
cin >> n >> k;
ll ans = 0;
for (ll cost = 1; cost <= n; cost <<= 1) {
ll take = min(k, n / cost);
ans += take;
n -= take * cost;
}
cout << ans << '\n';
}
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
ll n, k;
cin >> n >> k;
ll ans = 0;
for (ll cost = 1; cost <= n; cost <<= 1) {
ll take = min(k, n / cost);
ans += take;
n -= take * cost;
}
cout << ans << '\n';
}
}