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.
Track only two totals: how many slices Hao eats and how many slices Alex eats. The leftover pile is just delaying slices until later.
On every day where Hao eats, Hao gets and Alex gets , with . So Alex eats at least as many slices as Hao on each split day.
The process always ends with or slices, and Alex eats those all by himself. So Alex gets at least one more slice than Hao overall.
Since all slices are eaten, if Hao eats and Alex eats , then and . This gives .
That upper bound is reachable. Use induction: choose a valid carried pile , split the other slices as evenly as possible between Hao and Alex, and pick 's parity so the floor terms add exactly to .
Let Hao's total be and Alex's total be .
The whole problem is secretly about comparing those two numbers. The daily splitting rule says:
Hao eats , Alex eats , and survives.
So on every day where Hao gets anything, Alex gets at least as much as Hao. Then, when the remaining pile has at most slices, Alex eats all of it. Since the pile is positive, Alex gets at least one final slice that Hao never gets.
Therefore Alex's total is always at least one more than Hao's total:
Also, every slice is eventually eaten by exactly one of them:
Combine them:
So:
That gives an upper bound. Now we need to prove it is actually reachable, because an upper bound alone is just math cosplay.
Why the bound can be reached
Define as the maximum number of slices Hao can eat from slices. We claim:
The cases give , and works with the split .
For larger , choose the carried-over pile size so that:
This makes it possible for to be the largest group while still leaving at least one slice for both Hao and Alex.
Now split the remaining slices as evenly as possible:
Because , we have , so the split is valid.
After this day, Hao gets
and then the game continues from slices. By induction, the future can give him
So the total becomes:
The two numerators add to . This equals
as long as we avoid the annoying case where both and are odd. That bad case happens exactly when is odd and is even.
So choose , but if is odd and that is even, use instead. This is still a valid carried pile, and now the floors add perfectly.
Thus the upper bound is reachable, so the answer is exactly:
In C++, integer division already floors, so print:
(n - 1) / 2That's it. The pizza story is doing a lot of dramatic acting for a one-line formula.
#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;
cin >> n;
cout << (n - 1) / 2 << '\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;
cin >> n;
cout << (n - 1) / 2 << '\n';
}
return 0;
}