Cutting into Monotone Increasing Sequence
Time limit3sMemory limit1024 MB
Insert the fewest commas into a big integer so each piece is at most b, has no leading zero, and the pieces never decrease from left to right.
- Level
Medium6 of 10
- Topics
- Dynamic programming, String, Math
- Solved
- No attempts yet
Problem
A monotone sequence is a sequence of numbers that either consistently increases or consistently decreases as you move along it. In a monotone increasing sequence, each term is greater than or equal to the term before it. For a sequence , it is monotone increasing if and only if for every . For example, the sequence is monotone increasing.
Monotone sequences appear in calculus and analysis. They make it easier to study how a function behaves over a range of values.
One of our problem setters likes big integers. The Taiwan Online Programming Contest has often included problems about them. This problem combines big integers with monotone increasing sequences. Given a big integer , insert commas between its digits so that the result is a monotone increasing sequence, subject to these constraints.
- The last term is no more than .
- Commas cannot be inserted before a zero.
- The number of commas is minimized.
Let have digits, written . For example, with and , inserting commas after and gives .
Compute the minimum number of commas needed to turn into a monotone increasing sequence whose terms are all no more than . If it is impossible, print NO WAY.
Input
The input contains two non-negative integers and .
Output
Print the minimum number of commas needed to turn into a monotone increasing sequence whose terms are all no more than . If there is no way to do so, print NO WAY.
Constraints
- If , has no leading zero.