EGOI 2026 Editorial - Cakes


Task Author: Hazem Issa

Subtask 1: N=1

In this subtask, there is one topping type with count a1. 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 Kj cakes with equal tastiness b, each cake must contain exactly b toppings, so: a1=Kjb.

Thus, the answer is YES if and only if a1 is divisible by Kj which is true if a1modKij=0.


Subtask 2: Q=1, Kj=2

In this subtask, we need two cakes with equal tastiness. Let m be the amount of the topping that we have most of, and let s be the amount of the topping we have the second most of or 0 for N=1. We do a case-analysis on whether m is even or odd.

Case 1: m is even

If m is even, we can distribute all the toppings equally among both cakes. This results in a common tastiness of b=m/2:

The topping type with count m can contribute b copies to each cake, making both cakes reach tastiness at least b. In addition, the common tastiness will not be larger than b, since any other topping will appear at most m/2=b times on each cake.

Thus, in this case, the answer is always YES.

Case 2: m is odd

If m is odd, the smallest possible b that can fit all of topping m into two cakes without exceeding b is b=m2. With this b, the largest type can create only one full block of size b, so only one cake can “reach” tastiness b using that type. To make the other cake also reach b, we need another type with at least b copies: sbsm2.

Solution


Subtask 3: Q5, small limits (N1000), (ai1000)

Let m=maxai. We try all possible values for the common tastiness b[1..m] for each query. To check whether a fixed b is possible, we need to make the following checks:

  1. No type exceeds b on any cake (capacity check).
    A topping type i with count ai can be distributed across Kj cakes with at most b per cake if and only if aiKjb.
    It suffices to check the maximum: mKjb.

  2. Every cake achieves tastiness b (winner-block check).
    A cake that has tastiness b needs at least one topic with exactly b times on it.
    Type i can supply ai/b disjoint blocks of size b, each block can serve as the “winner” for one cake.
    So we need: T(b)=i=1NaibKj.

A cake has tastiness b only if some topping type appears exactly b 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 b for every query in O(NQmaxai).

Solution

For each query, iterate through b=1..m and check:

If any b passes, answer YES, otherwise NO.


The runningtime of this subtask is O(NQmax(ai)).

Subtask 4: Q5

From now on, we want to avoid trying all b.

Observe that due to the capacity check mKjb, any valid b must satisfy: bmKj.

Furthermore, the number of winner blocks T(b)=i=1Naib is non-increasing as b increases: the larger b, the fewer winner blocks we get. So among all feasible b, the best chance to have T(b)Kj is at the smallest one that passes the capacity check: bj=mKj.

Solution

It is enough for each query to check just bj: Output YES if and only if T(bj)=i=1NaibjKj.

The running time of this subtask is O(NQ).


Full Solution

Observation 1 (each query depends only on bj)

Each query Kj only needs:

Thus, we do not need to re-compute T(bj) for every query: Multiple queries may share the same value of bj. Thus, we can save already-computed values of T(b) and reuse them.

Observation 2 (few distinct bj)

Over the different Kj, the value mKj takes only O(m) distinct values.
So we will compute T(b) only O(m) times.

Solution

For each query:

  1. Compute bj=mKj.
  2. If T(bj) was not computed yet, compute it once in O(N).
  3. Answer YES if and only if T(bj)Kj.

The total running time is O(NNr of distinct bj+Q)=O(Nm+Q).