## Google Code Jam '22 Round 1C Problem B - Squary

View as PDF

Points: 10 (partial)
Time limit: 10.0s
Memory limit: 1G

Problem types

Addition and squaring do not commute. That is, the square of the sum of all elements of a list of integers is not necessarily equal to the sum of the squares of those same elements. However, this is true for some lists; one example is , because . Let us call these lists squary.

Given a (not necessarily squary) list of relatively small integers, we want to know whether it is possible to add at least and at most more elements such that the final list is squary. Each added element must be an integer between and , inclusive, and these do not have to be distinct from each other or from the initial list's elements.

#### Input Specification

The first line of the input gives the number of test cases, . test cases follow. Each test case is described in two lines. The first line contains two integers and , the number of elements of the initial list and the maximum number of elements you may add, respectively. The second line contains integers , representing the elements of the initial list.

#### Output Specification

For each test case, output one line containing Case #x: y, where is the test case number (starting from ). If it is possible to add at least and at most elements (each an integer between and , inclusive) to the initial list such that the square of the sum of its elements equals the sum of the squares of its elements, should be , where and the values are the additional elements. If there is no way to accomplish this, should be IMPOSSIBLE.

#### Limits

.

.

, for all .

Test Set 1

Time limit: 5 seconds.

.

Test Set 2

Time limit: 10 seconds.

.

#### Sample Input 1

4
2 1
-2 6
2 1
-10 10
1 1
0
3 1
2 -2 2

#### Sample Output 1

Case #1: 3
Case #2: IMPOSSIBLE
Case #3: -1000000000000000000
Case #4: 2

#### Explanation for Sample 1

This Sample corresponds to the constraints of Test Set 1 and 2.

In Sample Case #1, we can end up with the example list given in the problem statement.

In Sample Case #2, we have to add exactly one element. If we call that element , the sum of the entire list is and its square is . The sum of the squares of all elements, on the other hand, is , so the case is impossible.

In Sample Case #3, any integer in the range is a valid answer.

In Sample Case #4, notice that the input might contain duplicate elements, and that it is valid to create even more duplicates with the elements you choose to add.

#### Sample Input 2

3
3 10
-2 3 6
6 2
-2 2 1 -2 4 -1
1 12
-5

#### Sample Output 2

Case #1: 0
Case #2: -1 15
Case #3: 1 1 1 1 1 1 1 1 1 1 1

#### Explanation for Sample 2

This Sample corresponds to the constraints of Test Set 2.

In Case #1 of the additional samples, we are given the example list from the problem statement, which is already squary, but we need to add at least one element to it. Adding a keeps the list squary.

In Case #3 of the additional samples, we present one of multiple possible valid answers. Notice that it is permissible to add fewer than elements; here is but we have only added elements.

#### Note

This problem has different time limits for different batches. If you exceed the Time Limit for any batch, the judge will incorrectly display >10.000s regardless of the actual time taken. Refer to the Limits section for batch-specific time limits.