The Value of an Expression
Time limit1sMemory limit128 MB
Given a starting value a, use the operation x#y = (digit sum of x)*(max digit of y) + (min digit of y) and find the fewest operations to reach K, or report NEVAR.
- Level
Medium7 of 10
- Topics
- BFS, Math, Implementation, Greedy
- Solved
- No attempts yet
Problem
The operation is defined on any two positive integers as follows.
For positive integers and ,
For example, , whereas .
An expression (in this problem) is one of the following:
- the single variable
a, whose value is a positive integer, or - something of the form
(expression # expression).
For example, the following are all valid expressions:
a(a#a)((a#a)#a)(a#((a#a)#((a#a)#a)))
The value of an expression is determined by the value of a together with the operation : the expression consisting of just a has value , and (E1 # E2) has the value obtained by applying to the values of its two subexpressions.
Given the value of , determine the least number of operations needed to build an expression whose value equals (a positive integer).
Input
Two positive integers are given: the value of the integer variable () and the target expression value ().
Output
Print the least number of operations required. If it is impossible to obtain as the value of an expression for the given , print NEVAR instead.