#P15919. [TOPC 2023] Cutting into Monotone Increasing Sequence
[TOPC 2023] Cutting into Monotone Increasing Sequence
题目描述
A monotone sequence is a sequence of numbers that either consistently increases or consistently decreases as you move along the sequence. In other words, it exhibits a consistent trend in either an upward or downward direction.
In a monotone increasing sequence, each term in the sequence is greater than or equal to the preceding term. Mathematically, for a sequence , it is monotone increasing if and only if for every , . For example, the sequence is a monotone increasing sequence because each term is greater than or equal to the previous term.
Monotone sequences are important in various areas of mathematics, including calculus and analysis, as they often simplify the analysis of functions and their behavior. They provide a clear and consistent trend that makes it easier to understand the behavior of a sequence or a function over a range of values.
One of our problem setters is fond of big integers. Over the past few years, the Taiwan Online Programming Contest has frequently featured problems involving big integers. This time, we have a problem that combines big integers with monotone increasing sequences. Your task is to transform a big integer, denoted as , into a monotone increasing sequence by inserting commas ‘,’ into the gaps between its digits, while adhering to following constraints.
- The last term of the monotone increasing sequence is no more than .
- Commas cannot be inserted before a zero.
- The number of commas is minimized.
Let’s assume that is an integer with digits and is represented as . For instance, if we have and , we can insert commas into gaps after and to convert into the following monotone increasing sequence: .
Please write a program to compute the minimum number of commas required to transform a given big integer into a monotone increasing sequence consisting of numbers no more than a given integer . If there is no way to transform, please print ‘NO WAY’.
输入格式
The input contains two non-negative integers and .
输出格式
Print the minimum number of commas required to transform into a monotone increasing sequence consisting of numbers no more than . If there is no way to transform, please print ‘NO WAY’ without quotes.
654321 1000
2
654321 100
NO WAY
提示
- There is no leading zero if .