eolymp
bolt
Try our new interface for solving problems
Problems

Arithmetically Challenged

Arithmetically Challenged

Challenge \textbf{24} is a popular mathematics game used in many grade schools. In each game, contestants are given a card with four positive integers \textit{\textbf{i_1}}\textit{, }\textit{\textbf{i_2}}\textit{, }\textit{\textbf{i_3}}\textit{, }\textit{\textbf{i_4}} on it, and the first one who can use all of these numbers and any combination of the four basic arithmetic operations to get \textbf{24} wins. Each of the numbers \textit{\textbf{i_1}}\textit{, }\textit{\textbf{i_2}}\textit{, }\textit{\textbf{i_3}}\textit{, }\textit{\textbf{i_4}} must be used exactly once. Division can be used only if the divisor evenly divides the dividend (i.e., you can perform \textbf{6}/\textbf{2} but not \textbf{6}/\textbf{4}). For example, if the card contains the numbers \textbf{7}, \textbf{2}, \textbf{5} and \textbf{1}, possible solutions are (\textbf{7}-\textbf{2})·\textbf{5}-\textbf{1} or (\textbf{7}+\textbf{1})·(\textbf{5}-\textbf{2}). Hmmm . . . this sounds like a source of a good programming problem. Write a program that determines the longest consecutive sequence of integers that can be obtained by different ways of arithmetically combining the four integers. For example, with \textbf{7}, \textbf{2}, \textbf{5} and \textbf{1} the longest consecutive sequence is \textbf{-18} to \textbf{26} (yes, we're allowing final results to be negative). The "\textbf{+}" and "\textbf{-}" operators must be used as binary operators, not as unary signs. \InputFile Each test case will consist of a single line containing the four, not necessarily distinct, positive integers, none of which will exceed \textbf{100}. A line containing four \textbf{0}’s will terminate input. \OutputFile For each test case, output the case number and the longest consecutive sequence of obtainable values, in the format shown in the sample output. If there is more than one longest consecutive sequence, use the one with the largest first value.
Time limit 1 second
Memory limit 32 MiB
Input example #1
7 2 5 1
8 15 38 3
0 0 0 0
Output example #1
Case 1: -18 to 26
Case 2: 150 to 153
Source 2009 East Central North America, October 31, Problem A