Official

H - Cabbage Master Editorial by en_translator


In the following editorials, we require the knowledge of Hall’s marriage theorem and Fast Zeta/Möbius transformation.

For explanation, we regard the order of \(B_j\) cabbages from company \(j\) as mutually distinguishable \(B_j\) orders of \(1\) cabbage.
Also, let \(O\) be the set of all orders from all companies. That is, denoting by \((i, j)\) the \(i\)-th company’s \(j\)-th “one-cabbage order,” \(O\) is a set of \(\sum_{i = 1}^M B_i\) orders, consisting of \(\lbrace (1, 1), (1, 2) \ldots, (1, B_1), (2, 1), (2, 2), \ldots, (2, B_2), \ldots, (M, 1), (M, 2), \ldots, (M, B_M) \rbrace\).
Also, let \(V\) be the set of all brands of cabbage. Namely, \(V = \lbrace \) Brand \(1, \) Brand \(2, \ldots, \) Brand \(N \rbrace\).

The necessary and sufficient conditions of Takahashi’s being called a Cabbage Master

We will consider the necessary and sufficient conditions of Takahashi’s being called a Cabbage Master, that is, all the orders in the set \(O\) can be satisfied.
For an order \(x \in O\), let \(S_x\) be the “set of brands of cabbage that can be used for Order \(x\).” That is, if Order \(x\) is from Company \(j\), then \(S_x = \lbrace \) Brand \( i: c_{i, j} = 1\rbrace\).
Also, for a set of orders \(T \subseteq O\), we define a set of brands a \(S_T\) by \(S_T= \bigcup_{x \in T} S_x\). Namely, \(S_T\) is “the set of brands, each of which can be used for at least one order in \(T\).”
By Hall’s marriage theorem, all the orders in set \(O\) can be satisfied if and only if

for all \(T \subseteq O\), \(f(S_T) \geq |T|\).

Here, \(|\cdot|\) denotes the number of elements in a set, and \(f(S)\) denotes the number of total number of cabbages belonging to \(S\), that is, \(f(S) = \sum_{\text{ Brand }i \in S} A_i\).
The necessary and sufficient conditions above can be rephrased as follows.

\(\min_{T \subseteq O, T \neq \emptyset} \lbrace f(S_T) - |T| \rbrace \geq 0\)

Note that for \(T = \emptyset\), \(f(S_T) - |T| = 0 \geq 0\).
We can further transform this as

\(\min_{T \subseteq O, T \neq \emptyset} \lbrace f(S_T) - |T| \rbrace\)
\(= \min_{T \subseteq O, T \neq \emptyset} \min_{S \subseteq V, S \supseteq S_T} \lbrace f(S) - |T|\rbrace\)
\(= \min_{S \subseteq V} \min_{T \subseteq O, T \neq \emptyset, S _T \subseteq S} \lbrace f(S) - |T|\rbrace\)
\(= \min_{S \subseteq V} \lbrace f(S) - \max_{T \subseteq O, T \neq \emptyset, S_T \subseteq S} |T|\rbrace\).

Here, the set \(T\) such that \(T \subseteq O, T \neq \emptyset, S_T \subseteq S\) has the maximum element if \(T\) is \(\lbrace x \in O : S_x \subseteq S \rbrace\), the set of all order \(x\) such that \(S_x \subseteq S\).
So we have \(\max_{T \subseteq O, T \neq \emptyset, S_T \subseteq S} |T| = |\lbrace x \in O : S_x \subseteq S \rbrace|\). Therefore, we can say that \(\min_{T \subseteq O, T \neq \emptyset} \lbrace f(S_T) - |T| \rbrace = \min_{S \subseteq V} \lbrace f(S) - g(S) \rbrace\), where \(g(S) = |\lbrace x \in O : S_x \subseteq S \rbrace|\). (Note that if \(\lbrace x \in O : S_x \subseteq S \rbrace = \emptyset\), we define \(g(S)\) to be \(-\infty\), not \(0\). This corresponds to the case where the range of \(\max\), \(T \subseteq O, T \neq \emptyset, S _T \subseteq S\), is empty.)

Finally, all the orders in set \(O\) can be satisfied if and only if

\(\min_{S \subseteq V} \lbrace f(S) - g(S) \rbrace \geq 0\).

Finding the number of cabbages that Snuke eats

We will find the number of cabbages \(X\) that Snuke eats.
By the condition above, the combinations of cabbages that Snuke eats is “the one with minimum number such that \(f(S) - g(S) < 0\) for some \(S \subseteq V\).”
Such a combination can be obtained by finding one \(S \subseteq V\) that minimizes \(f(S) - g(S)\) (we denote it \(S_{\min}\)), and choosing \(\max(0, f(S_{\min}) - g(S_{\min})+1)\) cabbages whose Brands are in \(S_{\min}\).
Therefore, the number of cabbages \(X\) that Snuke eats is \(X =\max(0, \min_{S \subseteq V} \lbrace f(S) - g(S)\rbrace+1)\).

In order to find \(X\), it is sufficient to find \(f(S)\) and \(g(S)\) for each set \(S \subseteq V\).
The values of \(f(S)\) can be computed in a total of \(\mathrm{O}(N2^N)\) time.
To find \(g(S)\), we first compute for each \(S \subseteq V\)\(g'(S)\): the number of orders \(x\) such that \(g'(S)\).” This can be computed in a total of \(\mathrm{O}(NM)\) time by inspecting every company once each. By applying a Fast Zeta Transform to this \(g'\), we can obtain desired \(g\) in a total of \(O(N2^N)\) time.
Now we could find the number of cabbages \(X\) that Snuke will eat.

Computing the number of ways for Snuke to eat cabbages

Now we will find the number of ways for Snuke to eat cabbages.
If \(X = 0\), the only possible way is to “choose no cabbage.” Hereinafter we will consider \(X > 0\). As we have already explained in “Finding the number of cabbages that Snuke eats”, the combinations of Snuke’s choice of cabbages is obtained by “first choosing \(S \subseteq V\) that minimizes \(\lbrace f(S) - g(S) \rbrace \) (there may be multiple of them), and then choose \(X\) cabbages out of cabbages with brands included in that \(S\).”
This is equal to “the combination of \(X\) cabbages out of all the cabbages such that there exists \(S' \in \mathcal{F}\) including all the brands of cabbages chosen,” where \(\mathcal{F}\) is a family of sets that consists of all \(S \subseteq V\) that minimizes \(f(S) - g(S)\) (namely, \(\mathcal{F} := \arg\min_{S \subseteq V} \lbrace f(S) - g(S) \rbrace\)).
Therefore, if we denote by \(h(S)\) “the number of combinations of \(X\) cabbages out of all the cabbages such that the set of brands of cabbages chosen is exactly \(S\),” the desired answer \(Y\) can be expressed as \(Y = \sum_{S \subseteq V, \text{There exists }S' \in \mathcal{F} \text{ that contains } S} h(S)\).

We will consider how to compute this. First, for a set \(S \subseteq V\), in order to determine if there exists \(S' \in \mathcal{F}\) that contains \(S\), it is sufficient to know “the number of \(S' \in \mathcal{F}\) that includes \(S\).” This can be computed by Fast Zeta Transform in a total of \(\mathrm{O}(N2^N)\) time.
Next, we will explain how to find \(h(S)\) for each set \(S \subseteq V\). For each set \(S \subseteq V\), let \(h'(S)\) be “the number of combinations of \(X\) cabbages out of all the cabbages such that the set of brands of cabbages chosen is included in \(S\).” Then \(h'(S)\) can be obtained as a binomial coefficient \(\binom{f(S)}{X}\). By applying Fast Möbius Transform to this \(h'\), one can obtain desired \(h\) in a total of \(\mathrm{O}(N2^N)\) time.
Thus, we can compute \(Y\), the total number of combinations of cabbages for Snuke to eat.

By the discussion above, the problem can be solved in a total of \(\mathrm{O}(N2^N + NM)\) time.

posted:
last update: