eolymp
bolt
Try our new interface for solving problems
Problems

To Add or to Multiply

To Add or to Multiply

The Industrial Computer Processor Company offers very fast, special purpose processing units tailored to customer needs. Processors of the \textbf{a-C-m} family (such as the \textbf{1-C-2} and the \textbf{5-C-3}) have an instruction set with only two different operations: \textbf{A add a} \textbf{M multiply by m} The processor receives an integer, executes a sequence of \textbf{A} and \textbf{M} operations (the program) that modifies the input, and outputs the result. For example, the \textbf{1-C-2} processor executing the program \textbf{AAAM} with the input \textbf{2} yields the output \textbf{10} (the computation is \textbf{2} → \textbf{3} → \textbf{4} → \textbf{5} → \textbf{10}), while the \textbf{5-C-3} processor yields \textbf{51} with the same program and input (\textbf{2} → \textbf{7} → \textbf{12} → \textbf{17} → \textbf{51}). You are an \textbf{a-C-m} programmer assigned to a top secret project. This means that you have not been told the precise computation your program should perform. But you are given particular values \textbf{p}, \textbf{q}, \textbf{r}, and \textbf{s} and the following conditions: \begin{enumerate} \item The input is guaranteed to be a number between \textbf{p} and \textbf{q}. \item The output must be some number between \textbf{r} and \textbf{s}. \end{enumerate} Given an \textbf{a-C-m} processor and the numbers \textbf{p}, \textbf{q}, \textbf{r}, and \textbf{s}, your job is to construct the shortest \textbf{a-C-m} program which, for every input \textbf{x} such that \textbf{p} ≤ \textbf{x} ≤ \textbf{q}, yields some output such that \textbf{r} ≤ \textbf{y} ≤ \textbf{s}. If there is more than one program of minimum length, choose the one that come first lexicographically, treating each program as a string of \textbf{As} and \textbf{Ms}. \InputFile The input contains several test cases. Each test case is given by a line with the six integers \textbf{a}, \textbf{m}, \textbf{p}, \textbf{q}, \textbf{r}, and \textbf{s} as described above (\textbf{1} ≤ \textbf{a}, \textbf{m}, \textbf{p}, \textbf{q}, \textbf{r}, \textbf{s} ≤ \textbf{10^9}, \textbf{p} ≤ \textbf{q} and \textbf{r} ≤ \textbf{s}). The last test case is followed by a line with six zeros. \OutputFile For each test case, display its case number followed by the best program as described above. Display the word "\textbf{empty}" if the best program uses no operations. Display the word "\textbf{impossible}" if there is no program meeting the specifications. Display the program as a sequence of space-separated strings, alternating between strings of the form "\textbf{nA}" and strings of the form "\textbf{nM}", where \textbf{n} > \textbf{0}. Strings of the former type indicate \textbf{n} consecutive \textbf{A} operations, and strings of the latter type indicate consecutive \textbf{M} operations. Follow the format of the sample output.
Time limit 3 seconds
Memory limit 256 MiB
Input example #1
1 2 2 3 10 20
0 0 0 0 0 0
Output example #1
Case 1: 1A 2M
Source ICPC 2011 World Finals