ICPC 2011 · Problem A · To Add or to Multiply

35th ICPC · Orlando, United States

Statement

Problem ID: addmul

The Industrial Computer Processor Company offers very fast, special purpose processing units tailored to customer needs. Processors of the aa-C-mm family (such as the 1-C-2 and the 5-C-3) have an instruction set with only two different operations:

A add aa M multiply by mm

The processor receives an integer, executes a sequence of A and M operations (the program) that modifies the input, and outputs the result. For example, the 1-C-2 processor executing the program AAAM with the input 22 yields the output 1010 (the computation is 2345102\to 3\to 4\to 5\to 10), while the 5-C-3 processor yields 5151 with the same program and input (271217512\to 7\to 12\to 17\to 51).

You are an aa-C-mm 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 pp, qq, rr, and ss and the following conditions:

  1. The input is guaranteed to be a number between pp and qq. 2. The output must be some number between rr and ss.

Given an aa-C-mm processor and the numbers pp, qq, rr, and ss, your job is to construct the shortest aa-C-mm program which, for every input xx such that pxqp\le x\le q, yields some output yy such that rysr \le y \le 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 As and Ms.

Input

The input contains several test cases. Each test case is given by a line with the six integers aa, mm, pp, qq, rr, and ss as described above (1a,m,p,q,r,s1091\le a, m, p, q, r, s\le 10^{9}, pqp\le q and rsr \le s).

The last test case is followed by a line with six zeros.

Output

For each test case, display its case number followed by the best program as described above. Display the word “empty” if the best program uses no operations. Display the word “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 “nnA” and strings of the form “nnM”, where n>0n > 0. Strings of the former type indicate nn consecutive A operations, and strings of the latter type indicate nn consecutive M operations.

Follow the format of the sample output.

ICPC 2011 World Finals Problem A: To Add or to Multiply

Sample Input

1 2 2 3 10 20
1 3 2 3 22 33
3 2 2 3 4 5
5 3 2 3 2 3
0 0 0 0 0 0

Sample Output

Case 1: 1A 2M
Case 2: 1M 2A 1M
Case 3: impossible
Case 4: empty

ICPC 2011 World Finals Problem A: To Add or to Multiply

No official solution in the source collection.