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.
Ignore all piles except the one that might become . Splitting unrelated piles cannot help create this pile faster.
After splits along a chosen ancestry path, the pile you get is one leaf of the full split tree of depth .
If you split the original pile fully for levels, you create piles whose sizes differ by at most .
So at depth , every possible pile size is either or . That nukes the fake “graph search” idea from orbit.
Try until the values are tiny. Since , about checks per test case is plenty. The first where matches one of those two values is the answer.
The only pile that matters is the future pile of size . If it exists after some sequence of operations, it has an ancestry chain:
Each arrow is one split of the current ancestor pile. Any splits done elsewhere are just burning minutes for no benefit, so the answer is exactly the minimum depth where can appear in the binary split tree rooted at .
Now the key observation: after exactly levels of splitting, the original pile has effectively been divided into almost-equal parts. Therefore every pile at depth has size either
Why? For , there is just one pile of size , so it is true. If the claim is true for a pile size , splitting it creates and , which are exactly the two closest integer halves. Repeating this keeps the final pieces as balanced as possible. Equivalently, after splits, the descendant piles must sum to and differ by at most , so their only possible values are the floor and ceiling of .
So for each depth , we only need to check:
The first such is optimal because we scan depths in increasing order.
For integer arithmetic,
where .
Edge cases:
Complexity per test case is , with constant memory. Basically tiny. The CPU will not even notice this problem happened.
#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;
int ans = -1;
for (int d = 0; d <= 31; d++) {
ll m = 1LL << d;
ll lo = n / m;
ll hi = (n + m - 1) / m;
if (k == lo || k == hi) {
ans = d;
break;
}
}
cout << ans << '\n';
}
return 0;
}#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;
int ans = -1;
for (int d = 0; d <= 31; d++) {
ll m = 1LL << d;
ll lo = n / m;
ll hi = (n + m - 1) / m;
if (k == lo || k == hi) {
ans = d;
break;
}
}
cout << ans << '\n';
}
return 0;
}