Given a target constant c, print 20 fixed macro definitions and then build one expression by appending a macro for each set bit of c.
Easy1ImplementationSimulationBit manipulationStringNo attempts yetTime limit2sMemory limit512 MBJava2K is an esoteric programming language whose built-in functions only have a certain probability of doing what you intend. To practice, John designed a much simpler language called Java2016. Its operators are deterministic, while their operands are random. Every value in Java2016 is an integer between 0 and 255, inclusive.
Java2016 has six operators on three levels of precedence.
<expression> ::= <expression> min <sum> | <expression> max <sum> | <sum>
<sum> ::= <sum> + <term> | <sum> - <term> | <term>
<term> ::= <term> * <factor> | <term> / <factor> | <factor>
<factor> ::= ( <expression> ) | ? | <macro>
The operators min and max are the usual minimum and maximum. Addition, subtraction and multiplication are computed modulo 256. Division rounds towards zero, and the program crashes if the divisor is 0. The value ? is drawn uniformly at random from 0 to 255, and every occurrence of ? in an expression is independent of the others.
A program consists of zero or more macro definitions followed by the resulting expression.
<macrodef> ::= <macro> = <expression>
<macro> ::= a | b | ... | z
A macro has to be defined before its first use and may not be redefined. Each use of a macro expands to its definition. For example, after a = ? max ?, the expression (a max a) / a expands to ((? max ?) max (? max ?)) / (? max ?), and the six occurrences of ? in the expanded form are independent. The expression ? / ? / ? evaluates to 0 with probability 98.2%, and it crashes with probability 0.8%.
John wants to add probabilistic constants to Java2016. For each value he needs a program that evaluates to that value, without crashing, with probability at least 1/2. A crash counts as a failure.
The first line contains the target constant c. (0≤c≤255)
Many programs evaluate to c with probability at least 1/2, so the answer is fixed by the following rule. Print exactly the program built this way.
First print these 20 macro definitions, exactly as shown.
a = ? max ?
b = a max a
c = b max b
d = c max c
e = d max d
f = e max e
g = f max f
h = g max g
i = h max h
j = i max i
k = j max j
l = k max k
m = l / l
n = m + m
o = n + n
p = o + o
q = p + p
r = q + q
s = r + r
t = s + s
Then print the resulting expression on the last line. It starts with m - m. For i=0,1,…,7 in this order, if the digit of 2i in the binary representation of c is 1, append + and one macro name to the expression. The macro names for 2i are m, n, o, p, q, r, s, t, for 1 up to 128 in that order.
Put one space on each side of every operator and of =, as in the block above. The program built this way evaluates to c with probability greater than 0.9999, and its length without spaces is at most 143 characters.