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.
The layer sizes are fixed: . The only choice is whether the top layer starts white or dark.
For a fixed number of layers , the chocolate needed by one color is the sum of every other power of two. The other color gets the remaining alternating powers.
You don't need fancy math here. Since , the answer is tiny: after about 21 layers the total cake size already blows past the available chocolate.
Try building the cake one layer at a time for both possible starting colors. Keep two running sums: white used and dark used.
For each test case, brute force upward and check both patterns: top white and top dark. If either pattern fits, that number of layers is possible. The maximum possible one is the answer.
Core Idea
The cake shape is completely fixed. If it has layers, their sizes are:
The only freedom Monocarp has is the color of the top layer. After that, everything is forced because colors must alternate.
So there are only two possible valid color patterns:
That's it. No sneaky arrangement. No "maybe put two dark layers together because it helps" nonsense. The statement explicitly says alternating, so adjacent equal colors are illegal.
Why Brute Force Is Enough
The constraints are tiny in the important way: .
The total chocolate needed for layers is:
Even with both chocolate supplies combined, we have at most kg. Since , the answer is around at most. That is baby brute force territory.
For every test case, we can simulate layers from top to bottom. For each possible starting color, keep track of how much white and dark chocolate has been used so far.
When adding layer , its size is .
If the top starts white:
If the top starts dark, the roles swap.
After adding each layer, check whether the used white chocolate is at most and the used dark chocolate is at most . If yes, update the answer.
Algorithm
For each test case:
ans = 0.white and dark usage.white <= a and dark <= b, update ans.ans.We can loop for, say, to layers. That is more than enough because is way beyond the constraints. Using long long keeps everything chill.
Correctness Proof
We prove the algorithm outputs the maximum possible number of layers.
First, for any fixed number of layers, the layer sizes are fixed by the statement: the top layer has size , and every next layer is twice as large. Therefore the sizes must be exactly .
Second, once the color of the top layer is chosen, every other layer color is forced because adjacent layers must alternate. Thus there are exactly two possible valid colorings for any : one starting with white and one starting with dark.
The algorithm checks both of these possible starting colors. For each one, it simulates the exact chocolate usage layer by layer. Therefore, for every possible valid cake, the algorithm checks that cake's chocolate requirements.
Whenever the algorithm says layers are possible, it has found one of the two valid alternating colorings whose white usage is at most and dark usage is at most . So that cake is truly buildable.
Whenever some -layer cake is buildable, it must use one of the two starting colors, and the algorithm checks that exact pattern. So the algorithm will mark as possible.
Since the algorithm records the largest possible it sees, it outputs exactly the maximum number of layers.
Complexity
For each test case, we check only a constant number of layers, about , for two starting colors.
So the time complexity is:
with a very small constant.
Memory usage is:
#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 a, b;
cin >> a >> b;
int ans = 0;
for (int start = 0; start < 2; start++) {
ll white = 0, dark = 0;
for (int i = 0; i < 31; i++) {
ll size = 1LL << i;
bool whiteLayer = ((i % 2) == start);
if (whiteLayer) white += size;
else dark += size;
if (white <= a && dark <= b) {
ans = max(ans, i + 1);
}
}
}
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 a, b;
cin >> a >> b;
int ans = 0;
for (int start = 0; start < 2; start++) {
ll white = 0, dark = 0;
for (int i = 0; i < 31; i++) {
ll size = 1LL << i;
bool whiteLayer = ((i % 2) == start);
if (whiteLayer) white += size;
else dark += size;
if (white <= a && dark <= b) {
ans = max(ans, i + 1);
}
}
}
cout << ans << '\n';
}
}