Stack Machine Programmer
Time limit1sMemory limit128 MB
Generate a stack-machine program that maps up to five given small input-output integer pairs to their exact outputs under strict operand and stack constraints.
- Level
Medium6 of 10
- Topics
- Simulation, Math, Implementation, Combinatorics
- Solved
- No attempts yet
Problem
Many cipher operations run much faster on specialized machines or automata. The catch is that someone still has to write programs for those machines. This problem asks you to write a program that automatically writes other programs.
Your task is to generate programs for the stack machine defined below.
The machine keeps a single stack of integers. A program is a sequence of commands, one per line, ending with END.
NUM X: push integer X (0 ≤ X ≤ 10^9).POP: remove the top element.INV: negate the top element.DUP: duplicate the top element.SWP: swap the top two elements.ADD,SUB,MUL,DIV,MOD: pop the top two values, apply the operation, and push the result. The second popped value is the left operand and the first popped value is the right operand.DIVandMODfail on division by zero.- Every intermediate and final value must have absolute value at most 10^9.
When a program runs, the stack initially contains only the input value Vi. After a successful run, the stack must contain exactly one value Ri.
Input
The input has several test cases. Each test case starts with an integer N (1 ≤ N ≤ 5), the number of input-output pairs that one generated program must satisfy. The next N lines each contain integers Vi and Ri. Vi (0 ≤ Vi ≤ 10) is an input value and Ri (0 ≤ Ri ≤ 20) is the required stack top for that input. All Vi in one test case are distinct.
Each test case is followed by one blank line. A line with N = 0 ends the input.
Output
For each test case, print one stack-machine program that maps every listed Vi to the corresponding Ri. The program must follow the command format, whitespace rules, length limits, and stack limits from the specification, and it must not fail at runtime.
Print one blank line after each program, including the last one.