Cutting into Monotone Increasing Sequence

시간 제한3초메모리 제한1024 MB

요약
큰 정수의 자릿수 사이에 쉼표를 최소한으로 넣어, 각 조각이 b 이하이면서 비감소 수열이 되도록 나눈다.
난이도

어려움10점 중 8점

유형
그리디, 동적 계획법, 문자열
정답자
아직 제출이 없습니다

문제

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 a_1,…,a_na\_1, \dots , a\_n, it is monotone increasing if and only if for every 1≤i<n1≤ i < n, a_i≤a_i+1a\_i ≤ a\_{i+1}. For example, the sequence 1,2,2,4,51,2,2,4,5 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 xx, 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 bb.
  • Commas cannot be inserted before a zero.
  • The number of commas is minimized.

Let's assume that xx is an integer with kk digits and is represented as d_1d_2⋯d_kd\_1d\_2\cdots d\_k. For instance, if we have x=654321=d_1d_2⋯d_6x=654321=d\_1d\_2 \cdots d\_6 and b=1000b=1000, we can insert commas into gaps after d_3d\_3 and d_5d\_5 to convert xx into the following monotone increasing sequence: 6,54,3216,54,321.

Please write a program to compute the minimum number of commas required to transform a given big integer xx into a monotone increasing sequence consisting of numbers no more than a given integer bb. If there is no way to transform, please print 'NO WAY'.

입력

The input contains two non-negative integers xx and bb.

출력

Print the minimum number of commas required to transform xx into a monotone increasing sequence consisting of numbers no more than bb. If there is no way to transform, please print 'NO WAY' without quotes.

제한

  • x≤10100000x≤10^{100000}
  • b<264b<2^{64}
  • There is no leading zero if x>0x>0.

예제2

  1. 예제 1

    입력
    654321 1000
    
    예상 출력
    2
    
  2. 예제 2

    입력
    654321 100
    
    예상 출력
    NO WAY