EGOI 2026 Editorial - Cakes
Task Author: Hazem Issa
Subtask 1:
In this subtask, there is one topping type with count . Since there is only one type, the tastiness of a cake equals the number of toppings placed on it (all are the same type).
To make cakes with equal tastiness , each cake must contain exactly toppings, so:
Thus, the answer is YES if and only if is divisible by which is true if .
Subtask 2:
In this subtask, we need two cakes with equal tastiness. Let be the amount of the topping that we have most of, and let be the amount of the topping we have the second most of or for . We do a case-analysis on whether is even or odd.
Case 1: is even
If is even, we can distribute all the toppings equally among both cakes. This results in a common tastiness of :
The topping type with count can contribute copies to each cake, making both cakes reach tastiness at least . In addition, the common tastiness will not be larger than , since any other topping will appear at most times on each cake.
Thus, in this case, the answer is always YES.
Case 2: is odd
If is odd, the smallest possible that can fit all of topping into two cakes without exceeding is
With this , the largest type can create only one full block of size , so only one cake can “reach” tastiness using that type. To make the other cake also reach , we need another type with at least copies:
Solution
- If is even: YES
- If is odd: YES if and only if , else NO
Subtask 3: , small limits
Let . We try all possible values for the common tastiness for each query. To check whether a fixed is possible, we need to make the following checks:
-
No type exceeds on any cake (capacity check).
A topping type with count can be distributed across cakes with at most per cake if and only if .
It suffices to check the maximum:
-
Every cake achieves tastiness (winner-block check).
A cake that has tastiness needs at least one topic with exactly times on it.
Type can supply disjoint blocks of size , each block can serve as the “winner” for one cake.
So we need:
A cake has tastiness only if some topping type appears exactly times on it and no other topic appears more often. Checking both of the above conditions suffices.
For small limits, we can test these for all for every query in .
Solution
For each query, iterate through and check:
If any passes, answer YES, otherwise NO.
The runningtime of this subtask is .
Subtask 4:
From now on, we want to avoid trying all .
Observe that due to the capacity check , any valid must satisfy:
Furthermore, the number of winner blocks
is non-increasing as increases: the larger , the fewer winner blocks we get. So among all feasible , the best chance to have is at the smallest one that passes the capacity check: .
Solution
It is enough for each query to check just : Output YES if and only if
The running time of this subtask is .
Full Solution
Observation 1 (each query depends only on )
Each query only needs:
Thus, we do not need to re-compute for every query: Multiple queries may share the same value of . Thus, we can save already-computed values of and reuse them.
Observation 2 (few distinct )
Over the different , the value takes only distinct values.
So we will compute only times.
Solution
For each query:
- Compute .
- If was not computed yet, compute it once in .
- Answer YES if and only if .
The total running time is