Ones
Time limit1sMemory limit512 MB
For each k up to 1e9, output a 1-expression using only ones, +, *, and parentheses that evaluates to k with at most 100 ones, or NO.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Math, Backtracking, Greedy
- Solved
- No attempts yet
Problem
We define 1-expressions as numeric expressions that contain only ones, addition signs, multiplication signs, and parentheses. In such an expression no two digits may be adjacent: every two ones must be separated by an operator. Expressions follow the usual order of evaluation. For example, multiplication has higher priority than addition.
For example, each of the following 1-expressions evaluates to 6:
(1+1)*(1+1+1), (1+1+1)*(1+1)*1, ((1+1)+1)*(1+1), 1+1+1+1+1+1, 1+(1+(1+(1+(1+1)))).
Formally, the following grammar describes all valid 1-expressions :
E ::= 1 | E+E | E*E | (E+E) | (E*E)
Write a program that, given an integer (), outputs a 1-expression evaluating to that contains at most 100 ones.
Input
The first line of the input contains a single integer (), the number of testcases.
Each of the following lines describes a single testcase. The -th of these lines describes the -th test and contains a single integer ().
Output
You must output exactly lines.
If no 1-expression evaluates to and contains at most 100 ones, output NO on the -th line. Otherwise, that line must contain a valid expression. Do not print any spaces inside the expression. If there is more than one valid solution, print any of them.