Compiler
Time limit2sMemory limit512 MB
Print the specific 40-instruction-bounded program that displays N on a one-register display, following the stated decomposition rule.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Math, Implementation
- Solved
- No attempts yet
Problem
A small processor was built for one job: show a single number on a laser display board.
Its memory is three 8-bit registers named A, X, and Y, plus a stack with no depth limit. When the program starts, the registers hold unknown values and the stack is empty. A sum keeps only its lowest 8 bits.
The processor understands six instructions.
PH <reg>: push the value of the register (A, X, or Y) onto the stack.PL <reg>: pop the value on top of the stack into the register. If the stack is empty, the program stops.AD: pop two values off the stack, then push the lowest 8 bits of their sum.ZE <reg>: set the register to zero.ST <reg>: set the register to one.DI <reg>: send the value of the register to the laser display board and exit.
At most 40 instructions fit on the disk, and a line after the 40th one is never executed.
Given a number , print a program that displays . Many different programs display the same number, so the output section pins down exactly one of them.
Input
- One line containing the number to display ().
Output
Print the program fixed by the rule below, one instruction per line.
If , print ZE A on the first line and DI A on the second line.
If , the program keeps 1 in register A and builds in register X. For define by , , , and
Collect a list of pairs like this. Start with , and while , take the smallest that attains the minimum in , append the pair to the list, and replace by . Let be the value of once it reaches 3 or less, and let be the appended pairs listed in reverse, so the pair appended last comes first.
The program consists of, in this order:
- one line
ST A, - lines
PH A, then linesAD, then one linePL X, - for in this order: lines
PH X, then linesPH A, then linesAD, then one linePL X, - one line
DI X.
Register X starts at and follows for , so it holds when DI X runs. This program never exceeds 40 instructions.