#P7040. Constructing Deterministic Java2016 Programs

    ID: 20247 Type: Default 1000ms 256MiB

Constructing Deterministic Java2016 Programs

Constructing Deterministic Java2016 Programs

In the esoteric language Java2016, every value is a positive integer in the range \(0\) to \(255\) (both inclusive). The language supports no literal constants; instead, the only available operand is the random operator ? which produces a uniformly distributed random integer in \(0,1,2,\dots,255\). The built‐in operators (addition, subtraction, multiplication, division, min, and max) are deterministic, and macro definitions are allowed. Each macro definition has the form

[ \begin{aligned} \langle macrodef \rangle \quad &::=\quad \langle macro \rangle \texttt{=} \langle expression \rangle,\ \langle macro \rangle \quad &::=\quad a \ldots z. \end{aligned} ]

NOTE: A macro is expanded by textual substitution when used with its name. However, if you prefix the macro name with a dollar sign (for example, $a) then it indicates a frozen use, i.e. the value computed at the time of definition is reused. (This special frozen reference is allowed for this problem.)

A valid Java2016 program consists of zero or more macro definitions followed by an expression. For example, the following program

a = ? max ?
($a/$a)

defines a macro a by evaluating the expression ? max ? once. The frozen macro reference $a then returns that frozen value. In this example, $a/$a evaluates to \(1\) provided that \($a\) is nonzero (if \($a = 0\), a division crash occurs). Since the probability that ? max ? equals \(0\) is extremely small (only when both random values are \(0\)), $a/$a succeeds with probability very nearly \(100%\).


John is planning to add probabilistic constants to Java2016. For each constant \(c\) (with \(0 \le c \le 255\)) he needs a program that evaluates to \(c\) with a success probability of at least \(\frac{1}{2}\) (crashes count as failures). Because there are no literal constants in the language, you must build the desired constant from random values and operators.

A standard way to do this is as follows:

  1. Define a macro a whose definition is ? max ?. The frozen macro reference $a will then yield a fixed random number (almost surely nonzero).
  2. Use the expression $a/$a to obtain \(1\) with probability nearly \(100%\) (since division by \($a\) will crash only if \($a = 0\), which happens with negligible probability).
  3. Construct the target constant \(c\) by summing \(1\)'s. That is, if \(c \ge 1\) then compute \[ \underbrace{1+1+\cdots+1}_{c\;times} \] (using the fact that addition is performed modulo \(256\)). For \(c = 0\) you may simply use ($a - $a) which is always \(0\) (if no crash occurs).

Your task is to write a program that reads an integer \(c\) (on one line) and outputs a valid Java2016 program that evaluates to \(c\) with success probability at least \(\frac{1}{2}\) (i.e. at least \(50%\)).


Input Format:

The input consists of a single integer \(c\) where \(0 \le c \le 255\).

Output Format:

Output a valid Java2016 program. The program must begin with a macro definition for a as
a = ? max ?
and then an expression built using the frozen macro reference $a and the available operators (+, -, *, /, min, max) so that it evaluates to the constant \(c\) with success probability at least \(50%\).

Note: When constructing the expression, use left-associative nesting for additions. For example, if \(c = 5\), a correct output would be:

a = ? max ?
((($a/$a)+($a/$a))+($a/$a))+($a/$a)+($a/$a)

(Extra parentheses are allowed.)

inputFormat

The input contains a single integer \(c\) (\(0 \le c \le 255\)).

Example Input:
5

outputFormat

Output a valid Java2016 program that, with probability at least \(50%\), evaluates to \(c\). The program must start with the macro definition a = ? max ? followed by an expression constructed by using the frozen macro reference $a and addition. For \(c = 0\), you can output ($a-$a); for \(c \ge 1\), output a left-associated nested addition of \(c\) copies of $a/$a.

Example Output for input 5:
 a = ? max ?
 (((( $a/$a + $a/$a ) + $a/$a ) + $a/$a ) + $a/$a )

sample

0
a = ? max ?

(aa-a)

</p>