# FFCOMB - Editorial

Author: Sergey Kulik
Tester: Misha Chorniy
Editorialist: Pawel Kacprzak

EASY

### PREREQUISITES:

Dynamic programming, bits

### PROBLEM:

There are N meals to buy numbered from 1 to N. The i-th meal can be bought separately for price C_i. Meals can also be bought in sets. There are M sets and each set consists of some meals. The prices of the i-th set is P_i. The goal is to buy all N meals for the lowest possible price. In one test file there are multiple test cases to handle.

### QUICK EXPLANATION:

The main idea is to use dynamic programming and bitmasks to compute dp[mask] as the smallest possible cost of buying elements set in the mask.

### EXPLANATION:

In subtasks 1 and 2 methods based on computing the exactly value of dp_1 defined in the explanation for subtask 3 below might be used. In particular let dp[mask] be the lowest price to buy meals in mask. Then one possible idea for lower constrains is to iterate for all possible masks and for each single one, iterate over all possible seats of meals and try to lower the cost of meals covered by the current mask and the set of meals by combining their costs.

First of all, notice that buying all meals is always possible, because they all can be bought separately. However, the price can be reduced by buying the meals in sets.

Let dp_1[mask_1] be the smallest price for buying meals in mask_1 using a single purchase - so using either a single purchase of a separated meal or a single purchase of a set of meals

At the beginning, we can set values of dp_1 and dp_2 to some extremely large values in order to simplify the implementation.

The first step towards the solution is to compute dp_1 values for all possible 2^N masks. Later we are going to use these values to compute values in dp_2.

Let mask_{separete, i} be the mask corresponding the meal i. Moreover, let mask_{set, i} be the mask corresponding to the i-th set of meals. Initially, we set:

dp_1[mask_{separete,i}] = C_i for all i = 1, 2, \ldots, N.

Then, we set:

dp_1[mask_{set, i}] = \min(dp_1[mask_{set, i}], P_i) for i = 1, 2, \ldots, M.

Since buying a set of B meals might be the cheapest way to buy a set of A meals for A < B, then we are going to try to lower these costs as follows:

for (int mask = (1 << n) - 1; mask >= 0; —mask) {
for (int j = 0; j < n; ++j) {
if (mask & (1 << j)) {
}
}
}


The thing to notice there is that we iterate from the masks containing the most meals to the masks containing less meals in order to try to reduce their prices. Notice that \texttt{mask ^ (1 << j)} is the mask with the j-th bit set to 0.

After than, we are ready to compute the values of dp_2. The idea is very simple, for a given mask we are going to iterate over all submasks of mask and ty to add a single meal to them to expand it to mask:

dp_2[0] = 0;
}
}


The above method has time complexity O(2^N \cdot N + 3^N), where the first part is the complexity of calculating dp_1 values and the second part is the complexity of calculating dp_2 values. While the complexity of the first part is quite obvious, the complexity of the second part needs an explanation. For each mask, we iterate over all its submasks. We know that there are \binom{N}{k} masks with k ones and each such mask has 2^{N-k} submasks. Thus the number of iterations during computation of dp_2 can be measure as \sum\limits_{k=0}^N \binom{N}{k} \cdot 2^{N-k} = 3^N.

### AUTHOR’S AND TESTER’S SOLUTIONS:

Setter’s solution can be found here.
Tester’s solution can be found here.

3 Likes

How the complexity is O(N*2^N) ?
It look to me something of order *O(N 2^N + 3^N) .

2 Likes

Weak tests?

2 Likes

The solution above mentioned is not O(2^N * N) but actually O(3^N).
While calculating dp_2, for each mask a traversal is made on each subtask, which actually gives a complexity of O(3^N - 2^N + 1) as per the given code. Even the tester’s code makes 387158346 iterations for line 48-53. Please check and provide a O(2^N * N) solution if there is possible.

1 Like

How did you simplify the summation to 3 ^ N ?

Nice editorial

I thought of the bitmask dp method, but couldn’t figure out anything to reduce the complexity!

can anyone tell me why it is O(2^n*n) for dp1 instead of O(2^n).

@student2 First 2^n or (1st for loop) is used for generating all combination of generating bits in binary form and other one (2nd for loop) is used for manipulating these bits i.e., doing manipulation on individual bits!

I hope you get this… otherwise feel free to ask

Thank you bansal.Can anyone tell me what are the submasks and how they can be computed

could anyone explain in dp1 why we are considering to minimize A meals by just reducing 1 meals from B meals.
suppose we have meals 3,4,5 for price 10 and meals 3,4,5,6,7 for price 5 then we have to just take second set price for both set.BTW I solved it by assigning like this but I don’t understand in editorial.

How this statement ‘The thing to notice there is that we iterate from the masks containing the most meals to the masks containing less meals in order to try to reduce their prices.’ holds true? We are iterating in descending order, that does’nt mean that all numbers containing highest number of bits come first. consider 4 and 3. 4 has 1 bit where 3 contains 2 bits

Here is my solution, https://www.codechef.com/viewsolution/12155819 well commented.

Link to setter’s solution is broken.

Thanks, I updated it and added a description why the complexity of the second part is O(3^N)

You’re right, I made an update and added a detailed explanation for this part.

2^{18}*18 + 3^{18} = 392139081 > 2*10^8, how does it pass within the time limit? Does the present server compute faster than 10^8 operations also?

^^ Same question. I also tried a 3**n approach (which was wrong) but it still gave tle: https://www.codechef.com/viewsolution/11980847

@vsp4, your code is recursive may be that is why it gave TLE, but still why does 3^n pass?

Binomial theorem. \sum_{k=0}^{n} \binom{n}{k} \cdot 2^k \cdot 1^{n-k} = (2+1)^n

1 Like

10^8 Operations per s is just a rough estimate. If you have a cache friendly algorithm with simple operations you can get much higher. On the old servers cache friendliness seemed to be paramount. In some problem I computed the transpose of a 1000x1000 int matrix in the straightforward naive fashion. It took on the order of a second!

2 Likes