Editorial for ECOO '21 P4 - Chris' Candy


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Authors: AQT, Riolku

The key observation here is how many combinations a given output has. Consider the sample output grouped by type: 2 9 9 9. The counts of the elements look like this:

Copy
2 : 1
9 : 3

Now for each element, recall that two sets with the same amount of all elements are the same. Assuming the rest of our elements have K combinations, and our last element has 3 total, we have 4K total sets. Why? We can choose to have any number from 0,1,2,3 of our last element, and no matter which we choose, we have K combinations for the rest of our elements, for a total of 4K combinations.

Let us have M distinct elements, each with counts Ci. Then the total number of combinations is:

i=1M(Ci+1)1

We subtract the empty combination here.

As an example suppose we are given K=7, as in the sample input. Then we need:

i=1M(Ci+1)1=7 i=1M(Ci+1)=8

Well, since we get to choose M, what if we just factor 8?

And indeed, the sample input has C={1,3}. Another valid output for K=7 is 1 2 3, which has C={1,1,1}.

This should give us the understanding to solve the first subtask. We can simply output K of the same integer, since K is so small.

For the remaining subtasks, we note that we can find the sum of C to be minimal in order to check if it fits under the constraint N105. In other words, we must find a set of numbers Ci such that

i=1M(Ci+1)=K+1

and

i=1MCi

is minimum. By making a change of variable of Ci+1=Di, it follows that

i=1MDi=K+1

and we are trying to minimize

i=1MDi1

Now consider that there exists a Di such that there exist positive integers a,b where ab=Di. We can try to immediately minimize the sum by replacing Di with a and b. In other words, we will replace Di with a and b when

ab1>a1+b1abab+1>0(a1)(b1)>0

The only positive integer solutions to the inequality are when a,b2, so it is always better to replace Di with a and b. Thus, the optimal solution should have all of Di not factorable into a,b2 meaning that all Di should be prime. So, after factoring K into its prime factors, we can compare the sum against 105.

The second subtask rewards contestants who made these observations but implemented an inefficient solution, potentially by factoring in linear time.

The final subtask can be solved by factoring in O(K) time or other efficient methods. Implementation details are left as an exercise to the reader.

Time Complexity: O(K)


Comments

There are no comments at the moment.