Under construction

Still being built. Things may change

Compiler

Time limit2sMemory limit512 MB

Summary
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 NN, print a program that displays NN. Many different programs display the same number, so the output section pins down exactly one of them.

Input

  • One line containing the number NN to display (0≤N≤2550 \le N \le 255).

Output

Print the program fixed by the rule below, one instruction per line.

If N=0N = 0, print ZE A on the first line and DI A on the second line.

If N≥1N \ge 1, the program keeps 1 in register A and builds NN in register X. For 1≤v≤2551 \le v \le 255 define f(v)f(v) by f(1)=1f(1) = 1, f(2)=2f(2) = 2, f(3)=3f(3) = 3, and

f(v)=min⁡2≤m≤v(m+(v mod m)+f(⌊v/m⌋))(v≥4).f(v) = \min_{2 \le m \le v} \left( m + (v \bmod m) + f(\lfloor v / m \rfloor) \right) \quad (v \ge 4).

Collect a list of pairs like this. Start with v=Nv = N, and while v≥4v \ge 4, take the smallest mm that attains the minimum in f(v)f(v), append the pair (m,v mod m)(m, v \bmod m) to the list, and replace vv by ⌊v/m⌋\lfloor v / m \rfloor. Let aa be the value of vv once it reaches 3 or less, and let (m1,d1),…,(mk,dk)(m_1, d_1), \dots, (m_k, d_k) be the appended pairs listed in reverse, so the pair appended last comes first.

The program consists of, in this order:

  1. one line ST A,
  2. aa lines PH A, then a−1a - 1 lines AD, then one line PL X,
  3. for i=1,2,…,ki = 1, 2, \dots, k in this order: mim_i lines PH X, then did_i lines PH A, then mi+di−1m_i + d_i - 1 lines AD, then one line PL X,
  4. one line DI X.

Register X starts at aa and follows v←miv+div \leftarrow m_i v + d_i for i=1,…,ki = 1, \dots, k, so it holds NN when DI X runs. This program never exceeds 40 instructions.

Examples6

  1. Example 1

    Input
    2
    
    Expected output
    ST A
    PH A
    PH A
    AD
    PL X
    DI X
    
  2. Example 2

    Input
    5
    
    Expected output
    ST A
    PH A
    PH A
    AD
    PL X
    PH X
    PH X
    PH A
    AD
    AD
    PL X
    DI X
    
  3. Example 3

    Input
    0
    
    Expected output
    ZE A
    DI A
    
  4. Example 4

    Input
    1
    
    Expected output
    ST A
    PH A
    PL X
    DI X
    
  5. Example 5

    Input
    3
    
    Expected output
    ST A
    PH A
    PH A
    PH A
    AD
    AD
    PL X
    DI X
    
  6. Example 6

    Input
    255
    
    Expected output
    ST A
    PH A
    PH A
    PH A
    AD
    AD
    PL X
    PH X
    PH X
    PH A
    AD
    AD
    PL X
    PH X
    PH X
    PH X
    AD
    AD
    PL X
    PH X
    PH X
    AD
    PL X
    PH X
    PH X
    PH A
    AD
    AD
    PL X
    PH X
    PH X
    PH X
    AD
    AD
    PL X
    DI X