Expression Representation
Time limit2sMemory limit128 MB
Compute the minimum number of 1-symbols needed to build an integer n using +, *, ! and parentheses, using DP over factorial and multiplicative decompositions.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Math, Number theory
- Solved
- No attempts yet
Problem
An expression representation is an expression made only from the symbol 1, the operators +, *, !, and parentheses (, ). It is defined by the following rules.
1is an expression representation.- If
eis an expression representation, then(e)ande!are also expression representations. - If
e1ande2are expression representations, thene1+e2ande1*e2are also expression representations.
For example, expressions with value 18 include (1+1+1)*(1+1+1)! and (1+1+1+1)*(1+1+1)+(1+1+1)!.
Given an integer n, find the minimum number of 1 symbols needed by any expression representation whose value is n.
Input
The first line contains an integer n.
- 1 ≤ n ≤ 10,000
Output
Print the minimum number of 1 symbols needed to make an expression representation with value n.
Hint
18 = (1+1+1)*(1+1+1)!