Editorial for Google Code Jam '22 Round 1C Problem B - Squary
Submitting an official solution before solving the problem yourself is a bannable offence.
The multinomial expansion for the power of is the key to solving this problem. The expansion of the square of the sum of elements in the list looks like:
Let be the sum of elements, be the sum of squares of elements, and be the sum of all pairwise products of elements of the list . We can now rewrite the above equation as:
We can also observe the following about how the above values change when an additional element is added to the list :
Our task is to achieve , where is the extended list that we get by adding extra elements to . In other words, we want to make .
Test Set 1:
If we are allowed only a single addition, we must choose an element such that .
If , we can get a squary list whenever is an integer, which happens if and only if divides . In that case, is our answer.
If , then . Since we want , we need . This is possible only if , that is, if all elements in are zeros. In this case, we can choose any value as our answer. But if any element in is not zero, it is impossible to get a squary list with only one addition.
Test Set 2:
At first, the search space might seem hopelessly broad here. But we can observe (or surmise and then confirm) that it is always possible to get a squary list by adding only two elements:
After adding , we have
After adding , we have
Thus, the two numbers satisfy the condition . We can also see that since the numbers in the original list are each of magnitude no greater than , , and , both well within the limits.
Comments