#P16623. [GKS 2017 #C] The 4M Corporation

[GKS 2017 #C] The 4M Corporation

Problem Description

The 4M Corporation has hired you to organize their departments and allocate headcount. You will create at least one department, and each department will receive some positive integer number of employees. It will not be easy, though — you have four different bosses, and each has given you a different instruction:

  1. The department with the fewest employees must have exactly MINIMUM employees.
  2. The department with the most employees must have exactly MAXIMUM employees.
  3. The average number of employees across all departments must be exactly MEAN.
  4. The median of the number of employees across all departments must be exactly MEDIAN. As a reminder, the median of a list is the value that, when the list is sorted in nondecreasing order, is in the center (for a list of odd length) or is the average of the two values in the center (for a list of even length).

Moreover, for the sake of efficiency, it is best to avoid creating too many departments. What is the smallest number of departments that you can create, if it is possible to satisfy your bosses' requests?

Input Format

The first line of the input gives the number of test cases, TT. TT test cases follow. Each consists of four integers: MINIMUM, MAXIMUM, MEAN, and MEDIAN, in that order.

Output Format

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1), and yy is either the minimum possible number of departments, or IMPOSSIBLE if it is impossible to satisfy all four bosses' requests.

5
6 4 5 1
7 7 8 8
2 2 2 2
3 7 5 5
1 4 3 4
Case #1: IMPOSSIBLE
Case #2: IMPOSSIBLE
Case #3: 1
Case #4: 2
Case #5: 3

Hint

Sample Case #1 is IMPOSSIBLE because the maximum value cannot be smaller than the minimum value.

Sample Case #2 is IMPOSSIBLE because the mean and median cannot be larger than the maximum value.

In Sample Case #3, you can create a single department with 22 employees. This satisfies all four bosses: the department with the fewest employees has exactly 22, the department with the most employees has exactly 22, and the mean and median are both 22.

In Sample Case #4, you can create one department with 33 employees and another department with 77 employees. Note that it would not suffice to create only one department with 55 employees, because then the department with the fewest employees would not have exactly 33 and the department with the most employees would not have exactly 77.

For Sample Case #5, you can create one department with 11 employee and two more departments with 44 employees each.

Limits

1T1001 \le T \le 100.

Small dataset (Test set 1 - Visible)

1MINIMUM81 \le \mathbf{MINIMUM} \le 8.

1MAXIMUM81 \le \mathbf{MAXIMUM} \le 8.

1MEAN81 \le \mathbf{MEAN} \le 8.

1MEDIAN81 \le \mathbf{MEDIAN} \le 8.

The constraints for the Small dataset guarantee that the answer is either IMPOSSIBLE or is less than 1414.

Large dataset (Test set 2 - Hidden)

1MINIMUM100001 \le \mathbf{MINIMUM} \le 10000.

1MAXIMUM100001 \le \mathbf{MAXIMUM} \le 10000.

1MEAN100001 \le \mathbf{MEAN} \le 10000.

1MEDIAN100001 \le \mathbf{MEDIAN} \le 10000.