Editorial for COCI '07 Regional #4 Jednakost
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
We solve the problem with dynamic programming. Consider the digits in left to right order, deciding where to place addition operators, keeping track of how much of the sum we still need to account for. The state is an ordered pair .
The smallest number of addition operations can be calculated as follows:
- For , ;
- For and , ;
- For and , ;
- Otherwise, where is the number represented by digits through (inclusive).
After calculating and storing the values, the above relation can be used to reconstruct the solution.
Comments